将JPanel类放入另一个类的JFrame中

时间:2023-01-28 16:01:53

I have to insert into a JFrame, a JPanel of another class. I have a jMenuItem into the JFrame and i want that, when i click on the JMenuItem, the JPanel will appear.

我必须插入JFrame,另一个类的JPanel。我有一个jMenuItem到JFrame,我想要,当我点击JMenuItem时,JPanel将出现。

 private void searchStudMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                                   
        searchStud s = new searchStud();
        s.setVisible(true);
        changePanel(s);
    }                                                  

    private void changePanel(JPanel panel) {
        getContentPane().removeAll();
        getContentPane().add(panel);
        getContentPane().doLayout();
        update(getGraphics());
    }

searchStud is the class that contain the JPanel. When i execute the program and i click on the JMenuItem, nothing happens... I tried searching online but what I find does not work.

searchStud是包含JPanel的类。当我执行程序并且我点击JMenuItem时,没有任何反应......我尝试在线搜索但我发现的东西不起作用。

1 个解决方案

#1


2  

The actual component - a JFrame I suppose - was changed, it must be revalidated:

实际组件 - 我想是一个JFrame - 已更改,必须重新验证:

private void changePanel(JPanel panel) {
    getContentPane().removeAll();
    getContentPane().add(panel);
    revalidate();
}

Just tested with this minimal code:

刚用这个最小的代码测试:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


@SuppressWarnings("serial")
public class Test extends JFrame {

    public static void main(String[] args) {
        new Test();
    }


    private static class searchStud extends JPanel {
        searchStud() {
            add(new JLabel("SEARCH STUD"));
        }
    }


    private Test() {
        SwingUtilities.invokeLater(this::initGUI);
    }

    private void initGUI() {
        JButton button = new JButton("Search");
        button.addActionListener(this::searchStudMenuItemActionPerformed);

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        add(button);
        setSize(300, 200);
        validate();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void searchStudMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                                   
        searchStud s = new searchStud();
        s.setVisible(true);
        changePanel(s);
    }                                                  

    private void changePanel(JPanel panel) {
        getContentPane().removeAll();
        getContentPane().add(panel);
        revalidate();
    }
}

#1


2  

The actual component - a JFrame I suppose - was changed, it must be revalidated:

实际组件 - 我想是一个JFrame - 已更改,必须重新验证:

private void changePanel(JPanel panel) {
    getContentPane().removeAll();
    getContentPane().add(panel);
    revalidate();
}

Just tested with this minimal code:

刚用这个最小的代码测试:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


@SuppressWarnings("serial")
public class Test extends JFrame {

    public static void main(String[] args) {
        new Test();
    }


    private static class searchStud extends JPanel {
        searchStud() {
            add(new JLabel("SEARCH STUD"));
        }
    }


    private Test() {
        SwingUtilities.invokeLater(this::initGUI);
    }

    private void initGUI() {
        JButton button = new JButton("Search");
        button.addActionListener(this::searchStudMenuItemActionPerformed);

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        add(button);
        setSize(300, 200);
        validate();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void searchStudMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                                   
        searchStud s = new searchStud();
        s.setVisible(true);
        changePanel(s);
    }                                                  

    private void changePanel(JPanel panel) {
        getContentPane().removeAll();
        getContentPane().add(panel);
        revalidate();
    }
}