如何使用 JButton 打开一个新的 JPanel?

How to open a new JPanel with a JButton?

我正在尝试编写具有多个屏幕的程序,但是,我不想使用选项卡式窗格。我已经研究过将多个 JPanel 与卡片布局一起使用,但这些方法根本不起作用。我需要做的是在单击按钮时加载新的 JPanel。这是我的代码:

import java.awt.BorderLayout;
import java.awt.EventQueue;

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

public class IA extends JFrame {

    private JPanel contentPane;
    private JPanel home;
    private JPanel clients;

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

    /**
     * Create the frame.
     */
    public IA() {
        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));

        JPanel home = new JPanel();
        contentPane.add(home, "name_714429679706141");
        home.setLayout(null);

        JButton btnClients = new JButton("Clients");
        btnClients.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                home.setVisible(false);
                clients.setVisible(true);
            }
        });
        btnClients.setBounds(160, 108, 89, 23);
        home.add(btnClients);

        JPanel clients = new JPanel();
        contentPane.add(clients, "name_714431450350356");
        clients.setLayout(null);

        JButton btnHome = new JButton("Home");
        btnHome.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                clients.setVisible(false);
                home.setVisible(true);
            }
        });
        btnHome.setBounds(169, 107, 89, 23);
        clients.add(btnHome);
    }

}

我会看一下这个 post,但是我觉得您需要使用 actionlistener 来完成它... Java Swing. Opening a new JPanel from a JButton and making the buttons pretty 我会把它留作评论,但显然你需要 50 个代表...

这个 link 可能更有帮助.. How to open a new window by clicking a button

问题是您有重复的变量 homeclients .

以下是您修改后的代码以修复该问题,并对更改的行进行注释(共五行):

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.JPanel;
import javax.swing.border.EmptyBorder;

public class IA extends JFrame {

    private final JPanel contentPane;
    //   private final JPanel home;  // REMOVED
    //   private JPanel clients;  // REMOVED

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

    /**
     * Create the frame.
     */
    public IA() {
        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 home = new JPanel();
        contentPane.add(home, "name_714429679706141");
        home.setLayout(null);

        final JPanel clients = new JPanel();  // MOVED UP
        contentPane.add(clients, "name_714431450350356");  // MOVED UP
        clients.setLayout(null);  // MOVED UP

        JButton btnClients = new JButton("Clients");
        btnClients.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent e) {
                home.setVisible(false);
                clients.setVisible(true);
            }
        });
        btnClients.setBounds(160, 108, 89, 23);
        home.add(btnClients);

        JButton btnHome = new JButton("Home");
        btnHome.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent e) {
                clients.setVisible(false);
                home.setVisible(true);
            }
        });
        btnHome.setBounds(169, 107, 89, 23);
        clients.add(btnHome);
    }

}

调用以下代码时,clients 变量等于 null。

JButton btnClients = new JButton("Clients");
btnClients.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        home.setVisible(false);
        clients.setVisible(true);
    }
});

这样写:

JPanel clients = new JPanel();
contentPane.add(clients, "name_714431450350356");
clients.setLayout(null);
JButton btnHome = new JButton("Home");
btnHome.setBounds(169, 107, 89, 23);
clients.add(btnHome);

在添加动作侦听器之前