简体   繁体   English

摇摆:单击第一个JPanel时,启动第二个JPanel

[英]Swing: Start second JPanel, when the first JPanel is clicked

I'm writing a simple java game and I'm facing this problem: 我正在编写一个简单的Java游戏,并且遇到了这个问题:

My different layouts are in different JPanels (1 JPanel for the welcoming page, where I have to press 'start game' and another one with the actuall functionallity) 我的不同布局位于不同的JPanels中(欢迎页面使用1个JPanel,我必须按“开始游戏”,而另一个则具有实际功能)

I start the game from a JFrame 我从JFrame开始游戏

import javax.swing.JFrame;

public class RType extends JFrame {

    public RType() {

        add(new Welcome());//first panel
        add(new Board());//panel with the game

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(100, 100);
        setResizable(false);
        setVisible(true);

    }

    public static void main(String[] args) {
        new RType();
    }
}

obviuosly, this launches the second panel right after the first, and I cant see the first one. 显然,这会在第一个面板之后启动第二个面板,而我看不到第一个面板。 I've tried some stuff, trying to invoke the second panel in the main method, when the first panel is clicked that way: 我已经尝试了一些方法,试图以这种方式单击第一个面板时在main方法中调用第二个面板:

RType rt=new RType();
rt.add(new Board()); //in this case add(new Board()); is removed from constructor

but it's doing nothing. 但它什么也没做。 how can I solve it? 我该如何解决?

As @nachokk has said, you should be using a CardLayout instead. 正如@nachokk所说,您应该改用CardLayout It lets you do things like tabs in a browser, but you don't need to make the tabs visible for your game if you don't want to. 它可以让您在浏览器中执行选项卡之类的操作,但是如果您不想这样做,则无需使这些选项卡对您的游戏可见。 You make your welcome "card" visible at first, then when the user clicks you switch to the Board "card". 首先,您要显示欢迎的“卡片”,然后在用户单击时切换到Board “卡片”。

在此处输入图片说明

I don't recommend to add directly to the JFrame components, instead use another container as JPanel . 我不建议直接添加到JFrame组件中,而是使用另一个容器作为JPanel JFrame default layout is BorderLayout , when you add in the way you are adding it always put in the center. JFrame的默认布局是BorderLayout ,当您以添加方式添加时,它总是放在中间。

Make something like this: 做这样的事情:

JFrame frame = new JFrame();
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new CardLayout());
mainPanel.add(new Welcome(), "Welcome");
mainPanel.add(new Board(),"Board");
frame.add(mainPanel);

Here is a tutorial How to use CardLayout 这是一个教程如何使用CardLayout

on first panel of welcome add a button, and apply actionperformed like 在欢迎的第一个面板上,添加一个按钮,然后执行如下操作

    JButton myButton = new JButton("Add Component ");
    myButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            frame.remove(welcome);
            frame.add(Board, BorderLayout.CENTER);
            frame.revalidate();
            frame.repaint();
            }
   });

By default, both panels will fill up the entire Frame's area. 默认情况下,两个面板都将填满整个“框架”区域。 To fix this, you will need to use another layout, such as a GridLayout to structure the areas in which the panels will take up the Frame's area. 要解决此问题,您将需要使用另一种布局,例如GridLayout来构造面板将占据Frame区域的区域。

You can also go with no layout to hard code the pixel values of where you want the panels to fit in your frame. 您也可以不使用布局来硬编码希望面板适合框架的位置的像素值。

EDIT: Based on what you're looking to do, the CardLayout is probably what you'll want to use for your Frame's layout. 编辑:根据您要执行的操作,CardLayout可能是您要用于框架布局的内容。

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

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