【发布时间】:2015-02-09 11:26:20
【问题描述】:
我有两节课。主类有一个 JFrame 并持有一个按钮,单击该按钮后,它会从不同的类加载一个面板并将其定位在框架的按钮中。
我可以做到,但我必须重新调整框架大小,然后附加面板才会出现,否则它会被隐藏。
这是我的代码:
public class PanelLoader {
public static void main(String[] args) {
// TODO code application logic here
JFrame frame = new JFrame("Layout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JPanel mainPanel = new JPanel(new BorderLayout());
JButton press = new JButton("press");
press.addActionListener(new ActionListener(){ //ActionListener, on click it opens a file choser,
@Override //selects a directory then gives it to srcDirField
public void actionPerformed(java.awt.event.ActionEvent event){
JPanel mine = ClassWhereStored.newJPanel();
mainPanel.add(mine, BorderLayout.SOUTH);
}
});
mainPanel.add(press, BorderLayout.NORTH);
frame.add(mainPanel);
frame.setVisible(true);
}
}
二等:
public class ClassWhereStored {
public static JPanel newJPanel(){
JPanel panel = new JPanel();
panel.setBackground(Color.yellow);
JLabel test= new JLabel("Some Text");
panel.add(test);
return panel;
}
}
那么这个只有在我重新调整框架大小时才显示附加面板的框架有什么问题?
【问题讨论】: