【发布时间】:2011-11-26 05:39:46
【问题描述】:
我正在尝试将两个 JButtons 并排放置在 JFrame 的中心,当 JFrame 重新调整大小时,按钮不会重新调整大小。
为此,我将两个按钮放置在带有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命名约定并遵守它们