在左下角/右下角创建两个按钮 [英] Creating two buttons at bottom left/right corner
本文介绍了在左下角/右下角创建两个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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
__________________________|||||||||||||按钮 1 按钮 2 ||________________________|
解决方案
您可能需要考虑使用
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);
I'm trying to make Button 1 on the bottom left corner and Button 2 on the bottom right corner
__________________________
| |
| |
| |
| |
| |
| |
|Button1 Button2 |
|________________________|
解决方案
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:
这篇关于在左下角/右下角创建两个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文