奇怪的是,我不知道为什么,但我无法从框架中删除我的jpanel。我尝试了所有的东西,但在这个指令之后什么都没有,我仍然继续看到jpanel:
frame.getContentPane().remove(myPanel);
我也尝试过:
frame.remove(...);
frame.add(...);
frame.revalidate();
frame.repaint();
但我仍然继续看到框架中的面板。这是我的代码(我正在开发一个关于学生笔记的小应用程序),现在我想删除我的第一个面板来做一个实验:
package StudApp;
import java.awt.BorderLayout;
public class StudApp {
private JPanel homeFirstRun;
private ArrayList<Corso> corsi = new ArrayList<Corso>();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new StudApp();
}
});
}
/**
* Create the frame.
*/
public StudApp() {
JFrame frame = new JFrame("Student Note");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 450, 300);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu menuHelp = new JMenu("Help");
menuBar.add(menuHelp);
JMenuItem menuIstrStud = new JMenuItem("Istruzioni Student Note");
menuHelp.add(menuIstrStud);
homeFirstRun = new JPanel();
homeFirstRun.setBorder(new EmptyBorder(5, 5, 5, 5));
frame.setContentPane(homeFirstRun);
homeFirstRun.setLayout(null);
JLabel welcomeMessage = new JLabel("Welcome to Student Note");
welcomeMessage.setBounds(5, 5, 424, 18);
welcomeMessage.setForeground(Color.DARK_GRAY);
welcomeMessage.setHorizontalAlignment(SwingConstants.CENTER);
welcomeMessage.setFont(new Font("Verdana", Font.BOLD, 14));
homeFirstRun.add(welcomeMessage);
JLabel welcomeCit = new JLabel(""Software is like sex, it's better when it's free."");
welcomeCit.setFont(new Font("Verdana", Font.ITALIC, 11));
welcomeCit.setHorizontalAlignment(SwingConstants.CENTER);
welcomeCit.setBounds(35, 199, 361, 14);
homeFirstRun.add(welcomeCit);
JTextArea welcomeTextArea = new JTextArea();
welcomeTextArea.setFont(new Font("Verdana", Font.PLAIN, 13));
welcomeTextArea.setText(" I think it's your first time here.nn"
+ " So the first step is to create a new course ton insert your grades.nn");
welcomeTextArea.setEditable(false);
welcomeTextArea.setBounds(27, 34, 381, 184);
homeFirstRun.add(welcomeTextArea);
frame.setVisible(true);
frame.remove(homeFirstRun); //here im doing a test because i wanna see if the homeFirstRun panel go out from the frame
// but not it remains.
}
}
基本上,因为您使用了frame.setContentPane(homeFirstRun);
,所以frame.remove(homeFirstRun);
被委派到内容窗格,所以这就像在说。。。
homeFirstRun.remove(homeFirstRun);
这显然毫无意义。。。
相反,尝试使用类似。。。
frame.add(homeFirstRun);
//...
frame.remove(homeFirstRun);
或者CardLayout
,或者实际上,任何布局管理器。。。