Warum werden Gridbaglayout.SetConstraints und Panel.Add mein indiziertes Array verschiedener Komponenten nicht akzeptier
Posted: 14 Jul 2025, 12:54
Ich erstelle eine GUI, die mehrere verschiedene Komponenten (wie Jbuttons, JTextFields usw. usw. hat, obwohl ich sie zuerst mit Jbuttons teste) in einem Raster mit unterschiedlichen Größen, und ich habe eine Methode zum Organisieren der Gridbag -Konstrainer -Gridys, Gridwidhidhts und Gridhirts und Gridhieththiries, Widwidhts und Gridheighirts, Widwidhts und Wutheighirts und Wut und Gridhichs, erstellt. Der Code beschwert sich darüber, wie ich Objekte nicht als Argument in SetConstraints und Panel.ADD verwenden darf, da sie nur Komponenten akzeptieren, obwohl Objekte eine Komponente sind (Jbutton). Ich verstehe nicht, wie falsch das ist oder was ich stattdessen tun soll, da Objektarrays der einzige Weg zu sein scheinen, sowohl Jbuttons als auch andere Komponenten wie JTextFields einzubeziehen und das zu enthalten, was ich in die SetConstraints und Panel.Add -Methoden einfügen soll. < /P>
Code: Select all
// Defines some components for use in all classes and methods.
private JFrame frame = new JFrame();
private JPanel panel = new JPanel();
private GridBagLayout gbl = new GridBagLayout();
private GridBagConstraints gcon = new GridBagConstraints();
// The GUI method, where all the work to create the GUI happens.
public GUI() {
// Defines 15 different buttons.
JButton btn1 = new JButton("Btn 1");
JButton btn2 = new JButton("Btn 2");
JButton btn3 = new JButton("Btn 3");
JButton btn4 = new JButton("Btn 4");
JButton btn5 = new JButton("Btn 5");
JButton btn6 = new JButton("Btn 6");
JButton btn7 = new JButton("Btn 7");
JButton btn8 = new JButton("Btn 8");
JButton btn9 = new JButton("Btn 9");
JButton btn10 = new JButton("Btn 10");
JButton btn11 = new JButton("Btn 11");
JButton btn12 = new JButton("Btn 12");
JButton btn13 = new JButton("Btn 13");
JButton btn14 = new JButton("Btn 14");
JButton btn15 = new JButton("Btn 15");
// // JTextField diceTypeField = new JTextField(5);
// Sets the border and layout of the panel.
panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
panel.setLayout(gbl);
// Sets the gcon settings that apply to all components, like weight and how it'll fill the screen.
gcon.weightx = 1;
gcon.weighty = 1;
gcon.fill = GridBagConstraints.BOTH;
// Creates an array with lots of buttons and the spaces they occupy. if the same button occupies 2 indexes, it's because it's a larger button. In a later version of this program, the buttons will be switched with different components. It is then sent into a method which adds them to the panel in a formated way according to how they've been placed in the array.
Object[] buttonArray = {btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn8, btn9, btn10, btn10, btn11, btn12, btn13, btn14, btn14, btn14, btn15, btn15, btn15, btn15, btn15, btn15};
componentFormater(buttonArray, 3);
// Adds the panel to the frame and changes some frame settings before setting it to be visible.
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Dice Roller");
// // frame.pack();
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
// A method for organising components in a grid when some components are wider and taller than others. Only works when components are wider before they are taller. It's very specific to the planned layout.
private void componentFormater(Object[] objects, int gridW) {
int objH;
int objW;
// Will go through every item in the array and add it to the panel with the gbl settings determined by how many times it occurs at once in the array.
int occurances = 0;
for (int i = 0; i