JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new BorderLayout());
button2.setLayout(new FlowLayout(FlowLayout.RIGHT));
button1.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.getContentPane().add(button1,BorderLayout.SOUTH);
frame.getContentPane().add(button2,BorderLayout.SOUTH);
frame.setSize(500,500);
frame.setVisible(true);
我试图在左下角制作按钮1,在右下角制作按钮2
__________________________
| |
| |
| |
| |
| |
| |
|Button1 Button2 |
|________________________|
答案 0 :(得分:1)
您可能需要考虑使用BoxLayout
' 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);
}
}
这可能会在调整大小之前和之后得到这个:
答案 1 :(得分:0)
为两者创建一个容器。
JPanel south = new JPanel(new AxisLayout(AxisLayout.HORIZONTAL));
south.add(button1);
south.add(button2);
frame.getContentPane().add(south, BorderLayout.SOUTH);
Obs:抱歉不记得Swing布局管理员,但你会发现AxisLayout来解决这个问题
答案 2 :(得分:0)
另一种方法是使用GUI构建器,并相应地修改代码。
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);
答案 3 :(得分:-2)
您可以添加jPanel
,然后将两个按钮添加到其中,然后在按钮上调用setBounds
并指定位置。然后将jPanel
添加到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);
边界设置为(x-coord,y-coord,width,height)。