发布
社区首页 >问答首页 >如何在一个JPanels内切换到JFrame?

如何在一个JPanels内切换到JFrame?
EN

Stack Overflow用户
提问于 2012-10-26 21:50:59
回答 2查看 167关注 0票数 0

我在写俄罗斯方块游戏。当应用程序启动时,用按钮"Play“打开Jlabel。如何在现有的Jframe中切换到不同的标签(板)?

就这样,它直接打开游戏。但是首先,我想使用ButtonPage类来显示一些欢迎屏幕,而不是一个按钮,然后调用游戏。

代码语言:javascript
代码运行次数:0
复制
    public class Tetris extends JFrame {

    public Tetris(){

        // JFrame Properties
        setSize(198, 409);  
        setResizable(false);
        setTitle("Tetris");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

//        ButtonPage buttons = new ButtonPage();
//        add(buttons);
//        buttons.setOpaque(true);

        Board board = new Board(this);
        add(board);
        board.start();

    } // end of constructor 
    public static void main(String[] args){

        Tetris game = new Tetris();
        game.setLocationRelativeTo(null);        
        game.setVisible(true);        
        game.setLayout(null);
    } // end of main

} // end of class

这里是ButtonPage类.

代码语言:javascript
代码运行次数:0
复制
public class ButtonPage extends JPanel implements ActionListener{

    JButton buttonPLAY = new JButton();
    JLabel backgroundImage = new JLabel();

    public ButtonPage(){  

        setLayout(null);

        ImageIcon buttonIcon = new ImageIcon(getClass().getResource("PlayButton.png"));
        ImageIcon buttonIconHover = new ImageIcon(getClass().getResource("PlayButtonHover.png"));
        ImageIcon buttonIconClicked = new ImageIcon(getClass().getResource("PlayButtonClicked.png"));
        int buttonHeight = buttonIcon.getIconHeight();
        int buttonWidth = buttonIcon.getIconWidth();


        buttonPLAY.addActionListener(this);
        buttonPLAY.setActionCommand("Play"); 
        buttonPLAY.setIcon(buttonIcon);
        buttonPLAY.setRolloverIcon(buttonIconHover);
        buttonPLAY.setPressedIcon(buttonIconClicked);
        buttonPLAY.setBorderPainted(false);        

        add(buttonPLAY);


        Dimension size2 = getSize();         
        Dimension size = buttonPLAY.getPreferredSize();
        buttonPLAY.setBounds((192 - buttonWidth)/2, 100 ,buttonWidth, buttonHeight);


    }// end of constructor

    @Override
    public void actionPerformed(ActionEvent e) {
        if ("Play".equals(e.getActionCommand())) {

        Tetris game = new Tetris();        
        // opens the window in the middle of the screen
        game.setLocationRelativeTo(null);
        // set the tetris window visible, unless its true - its invisible DUH!
        game.setVisible(true);        
        game.setLayout(null);

        }
    } // end of actionPerformed

}// end of class

使用actionPerformed方法,我可以打开游戏在一个新的框架,但我不知道如何切换面板。

谢谢您的提示!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-26 21:56:11

俄罗斯方块来自main,下面一行来自actionPerformed():

代码语言:javascript
代码运行次数:0
复制
Tetris game = new Tetris();

实例化第二个俄罗斯方块,你到底想要什么?

若要将多个面板添加到一个框架中,一次只能看到一个面板,请使用CardLayout

票数 0
EN

Stack Overflow用户

发布于 2012-10-26 21:56:38

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

https://stackoverflow.com/questions/13094932

复制
相关文章

相似问题

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