I want to put 2 JButtons on my JPanel, one of them being centered and the other being in the top right corner of the JPanel. The JPanel is the same size as the JFrame that contains the JPanel. How can I do this using GridBagLayout and GridBagConstraints?
public class MyPanel extends JPanel {
public MyPanel() {
JButton btnGame1 = new JButton("Game 1");
JButton btnExitFrame = new JButton("Exit");
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.PAGE_START;
c.weighty = 1;
c.gridx = 0;
add(btnGame1, c);
c.gridx = 1;
c.anchor = GridBagConstraints.FIRST_LINE_END;
add(btnExitFrame, c);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MyPanel());
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
MCVE for camickr
public class MyPanel extends JPanel {
static JFrame frame = new JFrame();
public MyPanel() {
JButton btnGame1 = new JButton("Game 1");
JButton btnExitFrame = new JButton("Exit");;
setLayout(new BorderLayout());
JPanel top = new JPanel( new FlowLayout(FlowLayout.RIGHT) );
top.add( btnExitFrame );
JPanel center = new JPanel(new GridBagLayout());
center.add(btnGame1, new GridBagConstraints());
add(top, BorderLayout.PAGE_START);
add(center, BorderLayout.CENTER);
}
public static void main(String[] args) {
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MyPanel());
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
FIXED SOL'N:
public class MyPanel extends JPanel {
static JFrame frame = new JFrame();
public MyPanel() {
JButton btnGame1 = new JButton("Game 1");
JButton btnExitFrame = new JButton("Exit");;
int nOfCells = 3;
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1;
c.weighty = 1;
for(int i = 0; i < nOfCells; i++){
for(int j = 0; j < nOfCells; j++){
c.gridx = i;
c.gridy = j;
if(i == 1 && j == 0) {
c.anchor = c.PAGE_START;
add(btnGame1, c);
c.anchor = c.CENTER;
}
else if(i == 2 && j == 0) {
c.anchor = c.FIRST_LINE_END;
add(btnExitFrame, c);
c.anchor = c.CENTER;
}
else {
c.fill = c.BOTH;
add(Box.createRigidArea(new Dimension(frame.getWidth()/nOfCells, frame.getHeight()/nOfCells)), c);
c.fill = c.NONE;
}
}
}
}
public static void main(String[] args) {
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.getContentPane().add(new MyPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Another option only using GridBagLayout would be to add invisible Boxes as fillers to divide your JPanel into sections, and then substituting the Boxes in your preferred locations with your JButtons.
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1;
for(int i=0; i<nOfCells; i++){
for(int k=0; k<nOfCells; k++){
c.gridx = i;
c.gridy= k;
this.add(Box.createRigidArea(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT)), c);
}
}
GridBagLayout does not allow positions to be fixed in space, they depend on their position relative to the other components of the JFrame.