动态 JFrame - 更改 JFrame 内容

Dynamic JFrame - Changing JFrame contents

我最近一直在开发大部分非常基础的软件应用程序,但遇到了一个问题。

我正在开发的应用程序现在有许多不同的菜单和屏幕,我希望 JFrame 在单击按钮时交替显示。

鉴于这是大多数应用程序似乎实现的功能,我可以看到关于此的令人惊讶的信息很少,这让我想知道我的方法是否完全关闭,但是有一些示例代码说明了这种方法。

因此,我的问题是,a)实现此目标的最佳方法是什么,b)我当前的代码有什么问题?然而,前一个问题是最重要的。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class Demo {
    JFrame frame;
    JButton nextButton = new JButton ("Next Screen");

    public void setup() {
        frame = new JFrame();
        frame.setVisible(true);
        frame.add(new PanelOne());
        frame.pack();
    }


    public class PanelOne extends JPanel { {
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.add(new JLabel("Label One"));
        this.add(new JLabel("Label Two"));
        this.add(new JLabel("Label Three"));
        this.add(new JLabel("Label Four"));
        this.add(new JLabel("Label Five"));
        JButton button = new JButton("Next Screen");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                swapPanel();
            }
        });
        this.add(button);
    } }
    public class PanelTwo extends JPanel {{
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.add(new JButton("Button One"));
        this.add(new JButton("Button Two"));
        this.add(new JButton("Button Three"));
        this.add(new JButton("Button Four"));
        this.add(new JButton("Button Five"));
    }}


    protected void swapPanel() {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {

                frame.removeAll();
                frame.add(new PanelTwo());
                frame.invalidate();
                frame.revalidate();

            }

        });

    }

    public static void main (String[] args) {
        Demo demo = new Demo();
        demo.setup();
    }
}

将此用作您的代码......

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class Demo {
    JFrame frame;
    JButton nextButton = new JButton ("Next Screen");
    PanelOne p = new PanelOne();
    public void setup() {
        frame = new JFrame();
        frame.setVisible(true);

        frame.add(p);
        frame.pack();
    }


    public class PanelOne extends JPanel { {
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.add(new JLabel("Label One"));
        this.add(new JLabel("Label Two"));
        this.add(new JLabel("Label Three"));
        this.add(new JLabel("Label Four"));
        this.add(new JLabel("Label Five"));
        JButton button = new JButton("Next Screen");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                swapPanel();
            }
        });
        this.add(button);
    } }
    public class PanelTwo extends JPanel {{
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.add(new JButton("Button One"));
        this.add(new JButton("Button Two"));
        this.add(new JButton("Button Three"));
        this.add(new JButton("Button Four"));
        this.add(new JButton("Button Five"));
    }}


    protected void swapPanel() {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {


                frame.remove(p);
                frame.add(new PanelTwo());
                frame.invalidate();
                frame.revalidate();

            }

        });

    }

    public static void main (String[] args) {
        Demo demo = new Demo();
        demo.setup();
    }
}

希望对您有所帮助....