2015-11-07 51 views
1

我正在开发Java Swing(第一次使用java swing)的卡触发器游戏。我使用netbeans,我有一个像新游戏一样的菜单。我希望当用户点击新的游戏按钮时,游戏就开始了。但我不知道如何做到这一点,就像用户点击按钮时,然后在事件处理动作函数中,是这样吗?爪哇Swing打开窗体上的按钮单击

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
    // TODO add your handling code here: 
    JFrame myframe = new JFrame(); 
    //and the game functionality here 

}           

回答

3

如果您希望在点击按钮后打开一个新窗口,您正在做正确的事情。在您的示例代码中,您需要使新框架可见。

public class NewGame { 

public static void main(String[] args) { 
    JFrame frame = new JFrame("Start up frame"); 
    JButton newGameButton = new JButton("New Game"); 
    frame.setLayout(new FlowLayout()); 
    frame.add(newGameButton); 
    frame.setVisible(true); 

    newGameButton.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      JFrame newGameWindow = new JFrame("A new game!"); 
      newGameWindow.setVisible(true); 
      newGameWindow.add(new JLabel("Customize your game ui in the new window!")); 
      newGameWindow.pack(); 
     } 
    }); 
    frame.pack(); 
} 
}