Я создаю графический интерфейс, который будет иметь несколько разных компонентов (например, jbuttons, jtextfields и т. Д., Хотя сначала я тестирую его с помощью Jbuttons) в сетке с разными размерами, и я создал метод для организации без ручного настройки Gridbagconstrastrastrants Gridx, Gridys, Gridwidhs и Gridheights. Код жалуется на то, как мне не разрешают использовать объекты в качестве аргумента в SetConstraints и Panel.add, потому что они принимают только компоненты, несмотря на то, что объекты являются компонентом (Jbutton). Я не понимаю, как это неправильно или что я должен делать вместо этого, так как массивы объектов, кажется, являются единственным способом включить как Jbuttons, так и другие компоненты, такие как JTextFields, и содержать то, что я должен вкладывать в SetConstraints и Panel.add. < /P>
// 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
Подробнее здесь: https://stackoverflow.com/questions/796 ... d-array-of