java swing-从框架中删除jpanel

java swing-从框架中删除jpanel,java,swing,Java,Swing,奇怪的是,我不知道为什么,但我无法从我的框架中删除我的jpanel。 我尝试了所有的方法,但没有尝试。在这之后,我仍然继续看到jpanel: frame.getContentPane().remove(myPanel) 我还试图做到: frame.remove(...); frame.add(...); frame.revalidate(); frame.repaint(); 但我仍然继续看到框架中的面板。 这是我的代码(我正在开发一个关于学生笔记的小应用程序),现在我想删除我的第一个面板,只

奇怪的是,我不知道为什么,但我无法从我的框架中删除我的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.\n\n"
                            + " So the first step is to create a new course to\n insert your grades.\n\n");
        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.
    }
}
package-StudApp;
导入java.awt.BorderLayout;
公共类StudApp{
私人JPanel homeFirstRun;
private ArrayList corsi=new ArrayList();
/**
*启动应用程序。
*/
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
新StudApp();
}
});
}
/**
*创建框架。
*/
公共应用程序(){
JFrame=新JFrame(“学生笔记”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
机架立根(100450300);
JMenuBar menuBar=新的JMenuBar();
frame.setJMenuBar(菜单栏);
JMenu菜单帮助=新JMenu(“帮助”);
菜单栏。添加(菜单帮助);
JMenuItem menuIstrStud=新的JMenuItem(“Istruzioni学生笔记”);
menuHelp.add(menuIstrStud);
homeFirstRun=新JPanel();
setboorder(新的EmptyBorder(5,5,5,5));
frame.setContentPane(homeFirstRun);
homeFirstRun.setLayout(null);
JLabel welcomeMessage=新的JLabel(“欢迎来到学生便笺”);
welcomeMessage.setBounds(5,5424,18);
welcomeMessage.setForeground(颜色:深灰色);
welcomeMessage.setHorizontalAlignment(SwingConstants.CENTER);
welcomeMessage.setFont(新字体(“Verdana”,Font.BOLD,14));
homeFirstRun.add(welcomeMessage);
JLabel welcomeCit=newjlabel(“\”软件就像性一样,免费时更好。\”;
welcomeCit.setFont(新字体(“Verdana”,Font.ITALIC,11));
welcomeCit.setHorizontalAlignment(SwingConstants.CENTER);
welcomeCit.挫折(35199361434);
homeFirstRun.add(welcomeCit);
JTextArea welcomeTextArea=新的JTextArea();
setFont(新字体(“Verdana”,Font.PLAIN,13));
welcomeTextArea.setText(“我想这是你第一次来这里。\n\n”
+“因此,第一步是创建一个新课程以\n插入您的成绩。\n\n”);
welcomeTextArea.setEditable(false);
welcomeTextArea.setBounds(27,34,381,184);
homeFirstRun.add(welcomeTextArea);
frame.setVisible(true);
frame.remove(homeFirstRun);//我正在做一个测试,因为我想看看homeFirstRun面板是否从框架中退出
//但它并没有消失。
}
}

基本上,因为您使用了
frame.setContentPane(homeFirstRun)
帧。删除(homeFirstRun)被委托给内容窗格,所以这就像说

homeFirstRun.remove(homeFirstRun);
这显然毫无意义

而不是尝试使用像

frame.add(homeFirstRun);
//...
frame.remove(homeFirstRun);

或者一个
CardLayout
,或者实际上,任何布局管理器…

你都可以尝试
myPanel.setVisible(false)
我不需要它,因为这个面板必须第一次(第一次打开程序时)出现在框架上,如果你第二次打开程序,你会看到另一个东西。。。所以我认为隐藏面板不是一个好主意,我想永远移除它。它可以工作!!!谢谢你,我失去了两个小时(谷歌在这里无处不在,啊…我永远不会忘记)=)谢谢你。哈哈,你是墨尔本人吗?我在澳大利亚呆了6个月。。。我非常想念这个神奇的国家!我还需要出去看更多的…墨尔本的雨。。。如往常一样;)