Below is the code, which i got from Creating two buttons at bottom left/right corner and i have included my attempt,(adding jpanel2)
import java.awt.BorderLayout;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ButtonsLeftAndRight {
private JFrame frame;
private JPanel pane;
private JButton button1;
private JButton button2;
public static void main(String[] args) {
SwingUtilities.invokeLater(new ButtonsLeftAndRight()::createAndShowGui);
}
public void createAndShowGui() {
frame = new JFrame(getClass().getSimpleName());
pane = new JPanel();
pane.setLayout(new BoxLayout(pane, BoxLayout.LINE_AXIS));
button1 = new JButton("Button1");
button2 = new JButton("Button2");
pane.add(button1);
pane.add(Box.createHorizontalGlue());
pane.add(button2);
frame.add(pane, BorderLayout.SOUTH);
JButton b1 = new JButton ("A");
JButton b2 = new JButton ("B");
JButton b3 = new JButton ("C");
JPanel panel2 = new JPanel();
panel2.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel2.add(b1);
panel2.add(Box.createVerticalGlue());
panel2.add(b2);
panel2.add(Box.createVerticalGlue());
panel2.add(b3);
frame.add(panel2, BorderLayout.CENTER);
frame.setSize(500, 500);
frame.setVisible(true);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
The problem is that BoxLayout can't be shared but i need to somehow do it so that i can get the vertical boxes
I'm trying to create BoxLayout
GUI which involves using buttons and JLabels
. In the below ASCII B
represents buttons an J
represents JLabel
. Also im only keeping a bit space between buttons and JLabels
__________________________
| |
| B J |
| B J |
| B J |
| J |
| |
|Button1 Button2 |
|________________________|