所以目前我的程序只显示GUI右下角的一个按钮。但我想在右下角显示两个按钮。有任何想法如何将两个按钮设置到右上角?到目前为止,这是我的代码:
import javax.swing.*;
import java.awt.*;
public class Other extends JFrame{
private static final long serialVersionUID = 1L;
public Other() {
super("Buttons");
final Container mainPanel = getContentPane();
mainPanel.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BorderLayout());
JPanel inputPanel = new JPanel();
inputPanel.add(new JLabel("RANDOM TEXT HERE"));
inputPanel.add(new JLabel("RANDOM TEXT HERE"));
inputPanel.add(new JLabel("RANDOM TEXT HERE"));
JButton s = new JButton("first");
JButton l = new JButton("second");
buttonPanel.add(s,BorderLayout.LINE_END);
buttonPanel.add(l,BorderLayout.LINE_END); //<-- not working
mainPanel.add(inputPanel,BorderLayout.PAGE_START);
mainPanel.add(buttonPanel,BorderLayout.PAGE_END);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args){
Other o = new Other();
}
}
答案 0 :(得分:6)
buttonPanel.setLayout(new FlowLayout(FlowLayout.TRAILING));
虽然BorderLayout
每个布局区域只接受一个组件,但FlowLayout
将显示添加的数量(在可查看范围内)。
答案 1 :(得分:0)