首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >动态JFrame -更改JFrame内容

动态JFrame -更改JFrame内容
EN

Stack Overflow用户
提问于 2015-04-16 09:45:51
回答 1查看 4.8K关注 0票数 0

我最近一直在开发软件应用程序,这些应用程序大多是非常基础的,而且我遇到了一个问题。

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

考虑到这是大多数应用程序似乎正在实现的特性,因此我可以看到令人惊讶的很少信息,这让我怀疑我的方法是否完全失效,但是有一些示例代码说明了这种方法。

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

代码语言:javascript
运行
复制
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();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-16 09:53:33

用这个作为你的代码..。

代码语言:javascript
运行
复制
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();
    }
}

希望能帮上忙..。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29671024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档