这个 JPanel 代码好吗?单击按钮时不显示其他 JPanel

Is this JPanel code good? Not showing the other JPanel when clicked on the button

如何让第二个 JPanel 在有人单击按钮时显示? 该代码给出错误 - 未知来源。我错过了什么?

package Com.global;

import java.awt.BorderLayout;

public class Govinda extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JPanel panel;
    private JPanel panel_1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Govinda frame = new Govinda();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Govinda() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new CardLayout(0, 0));

        final JPanel panel = new JPanel();
        contentPane.add(panel, "name_273212774632866");
        panel.setLayout(null);

        JButton btnNewButton = new JButton("New button");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                panel_1.setVisible(true);
                panel.setVisible(true);

            }
        });
        btnNewButton.setBounds(126, 105, 89, 23);
        panel.add(btnNewButton);

        JPanel panel_1 = new JPanel();
        contentPane.add(panel_1, "name_273214471684839");
        panel_1.setLayout(null);

        JLabel lblHaiiiiiiiii = new JLabel("HAIIIIIIIII");
        lblHaiiiiiiiii.setBounds(159, 129, 46, 14);
        panel_1.add(lblHaiiiiiiiii);
    }
}

在您的 actionPerformed() 方法中,您调用 panel_1.setVisible(true)

但是,您在构造函数中声明并实例化 panel_1。 一旦完成构造函数的执行,就会超出范围并且 panel_1 引用将丢失(panel_1 可用于垃圾回收)。

因此,当您调用 actionPerformed() 方法时,panel_1 不存在。

JPanel panel_1; 的声明与其他声明一起移动到 class 的顶部,然后才在构造函数中实例化 panel_1panel_1 = new JPanel();

Is this JPanel code good?

不是真的(抱歉),你应该使用 CardLayout 来让它工作,见 How to Use CardLayout

您还通过在构造函数中重新声明变量(panelpanel_1)来隐藏变量。

您还应避免使用 null 布局,像素完美布局是现代 ui 设计中的一种错觉。影响组件个体大小的因素太多,none 是您可以控制的。 Swing 旨在与核心的布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正

你也永远不会将 panel_1 添加到 contentPane 所以调用 setVisible 它(假设变量阴影是固定的)不会做任何事情

通常也不鼓励直接从 JFrame 或其他顶级容器进行扩展,您不会向 class 添加任何新功能并且将自己锁定在一个用例中

所以你可以做一些更像...

import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new Govinda());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class Govinda extends JPanel {

        /**
         *
         */
        private static final long serialVersionUID = 1L;
        private JPanel panel;
        private JPanel panel_1;

        /**
         * Create the frame.
         */
        public Govinda() {
            setBorder(new EmptyBorder(5, 5, 5, 5));
            setLayout(new CardLayout(0, 0));

            JPanel panel = new JPanel();
            add(panel, "name_273212774632866");

            JButton btnNewButton = new JButton("New button");
            btnNewButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    ((CardLayout) getLayout()).show(Govinda.this, "name_273214471684839");
                }
            });
            panel.add(btnNewButton);

            JPanel panel_1 = new JPanel();
            add(panel_1, "name_273214471684839");

            JLabel lblHaiiiiiiiii = new JLabel("HAIIIIIIIII");
            panel_1.add(lblHaiiiiiiiii);
        }
    }
}

您也可以考虑使用更多的 Model-View-Controller approach to the navigation, so the logic that is used to determine which view is next would be made by a controller, based on the requirements of a model, for example, have a look at Listener Placement Adhering to the Traditional (non-mediator) MVC Pattern