2012-10-26 2 views
0

나는 테트리스 게임을 쓰고있다. 응용 프로그램이 Jlabel을 시작하면 "재생"버튼이 열립니다. 기존 Jframe 내에서 다른 레이블 (Board)로 전환하려면 어떻게합니까?하나의 JFrame 내에서 JPanels로 전환하는 방법은 무엇입니까?

이렇게하면 게임이 직접 열립니다.하지만 먼저 ButtonPage 클래스를 사용하여 단추 대신 환영 화면을 표시 한 다음 게임을 호출하고 싶습니다.

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 클래스입니다.

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 메소드를 사용.

미리 감사드립니다.

답변

0

테트리스의 actionPerformed()에서 주, 다음 줄에서 intanciated된다

Tetris game = new Tetris(); 

두 번째 테트리스를 instanciates, 당신이 무엇을 원하는거야 정말?

프레임에 여러 패널을 추가하려면 한 번에 하나씩 만 표시하려면 CardLayout을 사용하십시오.

+0

하지 난 그냥 내가 버튼 패널을 사용 해달라고 경우에만 있지만 문제없이 게임을 실행할 수 있어요 아니면 내가 .. –

+0

감사합니다, CardLayout을 패널을 사용하지만 새로운 프레임을 열 것을 보여주기 위해 노력했다 없음을 그것을하는 적당한 방법 인 것처럼 보인다. 그러나 두 번째 패널로 전환하기 전에 게임을 클릭하기 전에 게임이 시작된다는 문제가 있습니다. –

+0

누군가를 돕는 것이 즐거움입니다. 'board.start()'는 무엇을하고 있습니까? – Aubin

관련 문제