【发布时间】:2017-02-26 10:43:07
【问题描述】:
运行以下代码,它只会打开一个没有任何按钮和子面板的空窗口。谁能帮帮我?
public class TestPanels extends JFrame{
public TestPanels(){
//Create first panel
JPanel panel1= new JPanel();
panel1.setLayout(new GridLayout(4, 3));
for(int i=1; i<=9; i++){
panel1.add(new JButton( ""+i));
}
// Create Second panel
JPanel panel2=new JPanel(new BorderLayout());
panel2.add(new JTextField("Time to Display Here"), BorderLayout.NORTH);
panel2.add(panel1, BorderLayout.CENTER);
}
public static void main(String[] args){
TestPanels test= new TestPanels();
test.setTitle("Test Panel");
}
}
【问题讨论】:
-
您没有将面板添加到框架中。另外,我认为这段代码不会显示任何内容,框架甚至都不可见。
-
@Berger “我认为这段代码不会显示任何内容,框架甚至都不可见。” 好点:为了尽快获得更好的帮助,请发布minimal reproducible example 或 Short, Self Contained, Correct Example 的实际代码,而不是一些假装是示例的无意义代码。
标签: java user-interface panel layout-manager