我试图将两个JButton
彼此相邻放置在JFrame
的中心,当JFrame
重新调整大小时,将不会重新调整按钮的大小。BorderLayout中心命令不会中心
要做到这一点,我把两个按钮放在一个面板FlowLayout
,然后放置在一个中心的面板BorderLayout
。
但是,以下代码不会在BorderLayout
的中心显示所选面板。
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class test extends JFrame {
private JPanel panel1 = new JPanel();
private JPanel panel2 = new JPanel();
private JButton button1 = new JButton("one");
private JButton button2 = new JButton("two");
public test() {
panel1.setLayout(new FlowLayout());
panel1.add(button1);
panel1.add(button2);
panel2.setLayout(new BorderLayout());
panel2.add(panel1, BorderLayout.CENTER);
this.add(panel2);
}
public static void main(String[] args) {
test frame = new test();
frame.setVisible(true);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
请学习java的命名约定并坚持到他们 – kleopatra