切换JPanel [英] Switching JPanels

查看:58
本文介绍了切换JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图编写一个基本程序来了解有关Java的更多信息,并且在切换屏幕时遇到了麻烦.我希望有一个显示类,可以在其他类中调用它来处理所有面板,等等,然后创建一个类来构建每个面板.目前,我想做的是使用显示菜单中的方法,在我的startmenu类中使用一个按钮将一个面板更改为另一个面板.

这是 startmenu 类中的代码:

  public void actionPerformed(ActionEvent e){display.switchPanel("Start");} 

这是我的展示课:

公共班级展示{JFrame框架;StartMenu开始=新的StartMenu();MainMenu main =新的MainMenu();JPanel面板=新的JPanel();JPanel startPanel = start.createPanel();JPanel mainPanel = main.createPanel();CardLayout卡=新的CardLayout();BorderLayout border =新的BorderLayout();公共无效createDisplay(){frame = new JFrame("Insert Name");frame.setPreferredSize(new Dimension(800,600));frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.getContentPane().add(panel,BorderLayout.CENTER);panel.setLayout(border);panel.add(startPanel);panel.add(mainPanel);mainPanel.setVisible(false);startPanel.setVisible(true);frame.add(panel);frame.pack();frame.setVisible(true);frame.setResizable(false);}公共无效switchPanel(String x){字符串p = x;if(p.equals(开始")){mainPanel.setVisible(true);startPanel.setVisible(false);}}}

解决方案

使用 CardLayout ,这就是它的设计目的,例如...

 公共类显示{public static final String START_VIEW =开始";public static final String MAIN_VIEW ="main";JFrame框架;StartMenu开始=新的StartMenu();MainMenu main =新的MainMenu();JPanel面板=新的JPanel();JPanel startPanel = start.createPanel();JPanel mainPanel = main.createPanel();CardLayout卡=新的CardLayout();公共无效createDisplay(){frame = new JFrame("Insert Name");frame.setPreferredSize(new Dimension(800,600));frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.getContentPane().add(panel,BorderLayout.CENTER);panel.setLayout(card);panel.add(startPanel,START_VIEW);panel.add(mainPanel,MAIN_VIEW);mainPanel.setVisible(false);startPanel.setVisible(true);frame.add(panel);frame.pack();frame.setVisible(true);frame.setResizable(false);}公共无效switchPanel(String x){card.show(panel,x);}} 

然后您可能会使用类似...

  public void actionPerformed(ActionEvent e){display.switchPanel(Display.START_VIEW);} 

在视图之间切换

有关更多详细信息,请参见如何使用CardLayout >

So I'm trying to make a basic program to learn more about java, and I'm having trouble switching screens. I wanted to have a display class that I could call in other classes to handle all the panels and such, and then make a class to build each panel. What I'm trying to do at the moment is use a button in my startmenu class to change from one panel to another using a method in the display class.

Here's the code in the startmenu class:

public void actionPerformed(ActionEvent e)
{
    display.switchPanel("Start");

}

And here is my display class:

public class Display 
{

    JFrame frame;

    StartMenu start = new StartMenu();
    MainMenu main = new MainMenu();


    JPanel panel = new JPanel();

    JPanel startPanel = start.createPanel();
    JPanel mainPanel = main.createPanel();


    CardLayout card = new CardLayout();
    BorderLayout border = new BorderLayout();

    public void createDisplay()
    {
        frame = new JFrame("Insert Name");
        frame.setPreferredSize(new Dimension(800,600));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(panel, BorderLayout.CENTER);

        panel.setLayout(border);
        panel.add(startPanel);
        panel.add(mainPanel);

        mainPanel.setVisible(false);
        startPanel.setVisible(true);

        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
        frame.setResizable(false);
    }

    public void switchPanel(String x)
    {
        String p = x;

        if(p.equals("Start"))
        {
            mainPanel.setVisible(true);
            startPanel.setVisible(false);
        }

    }
}

解决方案

Use a CardLayout, it's what it's designed for, for example...

public class Display {

    public static final String START_VIEW = "start";
    public static final String MAIN_VIEW = "main";

    JFrame frame;

    StartMenu start = new StartMenu();
    MainMenu main = new MainMenu();

    JPanel panel = new JPanel();

    JPanel startPanel = start.createPanel();
    JPanel mainPanel = main.createPanel();

    CardLayout card = new CardLayout();

    public void createDisplay() {
        frame = new JFrame("Insert Name");
        frame.setPreferredSize(new Dimension(800, 600));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(panel, BorderLayout.CENTER);

        panel.setLayout(card);
        panel.add(startPanel, START_VIEW);
        panel.add(mainPanel, MAIN_VIEW);

        mainPanel.setVisible(false);
        startPanel.setVisible(true);

        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
        frame.setResizable(false);
    }

    public void switchPanel(String x) {
        card.show(panel, x);
    }
}

Then you might use something like...

public void actionPerformed(ActionEvent e)
{
    display.switchPanel(Display.START_VIEW);
}

to switch between the views

See How to Use CardLayout for more details

这篇关于切换JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆