简体   繁体   English

动态JFrame-更改JFrame内容

[英]Dynamic JFrame - Changing JFrame contents

I've recently been developing software applications which have mostly been very basic, and I have reached a problem. 我最近一直在开发软件应用程序,这些应用程序大多数都是非常基础的,但是我遇到了一个问题。

The application I am developing now has many different menus and screens, which I would like the JFrame to alternate between displaying upon clicks of a button. 我正在开发的应用程序现在有许多不同的菜单和屏幕,我希望JFrame在单击按钮时在显示和显示之间切换。

I can see surprisingly little information on this given that this is a feature that most applications seem to implement, which makes me wonder if my approach is completely off, however is some example code illustrating this approach. 鉴于这是大多数应用程序似乎都实现的功能,因此我几乎看不到任何信息,这使我想知道我的方法是否完全不可行,但是有一些示例代码说明了这种方法。

My question therefore is, a) what is the best way to go about achieving this, and b) what is wrong with my current code? 因此,我的问题是, a)实现此目标的最佳方法是什么, b)当前代码有什么问题? The former question is the most important however. 但是,前一个问题是最重要的。

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();
    }
}

Use this as your code..... 使用它作为您的代码.....

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();
    }
}

hoping it helped.... 希望能有所帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM