JButton button1 = new JButton(\"Button 1\");
JButton button2 = new JButton(\"Button 2\");
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new BorderLayout(
Create a container for both.
JPanel south = new JPanel(new AxisLayout(AxisLayout.HORIZONTAL));
south.add(button1);
south.add(button2);
frame.getContentPane().add(south, BorderLayout.SOUTH);
Obs: Sorry dont remember exactly Swing layout manangers, but you will find the AxisLayout to solve this
You can add a jPanel
and then add the two buttons to it, and then call setBounds
on the buttons and specify the position. Then add the jPanel
to the jFrame
.
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JFrame frame = new JFrame();
JPanel p = new JPanel();
p.setLayout(null);
button1.setBounds(10, 400, 100, 40);
p.add(button1);
button2.setBounds(375, 400, 100, 40);
p.add(button2);
frame.getContentPane().add(p);
frame.setSize(500, 500);
frame.setVisible(true);
The bounds are set as (x-coord, y-coord, width, height).
You might want to consider using BoxLayout's horizontalGlue:
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);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
This might get you this, before and after resizing:
Another alternative is to use the GUI builder, and modify the code accordingly.
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JFrame frame = new JFrame();
GroupLayout layout = new GroupLayout(frame.getContentPane());
frame.getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(button1)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 215, Short.MAX_VALUE)
.addComponent(button2)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(256, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(button1)
.addComponent(button2))
.addGap(25, 25, 25))
);
frame.setSize(500, 500);
frame.setVisible(true);