I am new to creating a GUI with Java. I tried to use the GridBagLayout for a project but i am stuck here.
I want to make the Button and the two JLabels the same size but the button is bigger.
public class Menu extends JPanel {
public Menu() {
this.setBackground(Color.black);
GridBagLayout gb = new GridBagLayout();
setLayout(gb);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
JButton b1 = new JButton();
b1.setText("Start");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty= 1;
gb.setConstraints(b1, gbc);
add(b1);
JPanel Gap = new JPanel();
gbc.gridx = 1;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty= 1;
gb.setConstraints(Gap, gbc);
add(Gap);
RadioPanel radio = new RadioPanel();
gbc.gridx = 2;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 1;
gb.setConstraints(radio, gbc);
add(radio);
}
}
This is my code and this is what it looks like.
GridLayout, notGridBagLayoutFor three equally sized widgets, use
GridLayoutrather thanGridBagLayout. See Oracle tutorial, How to Use GridLayout.Here is an example of a button with two labels, all the same size.