单击按钮打开Java Swing表单
我正在用JavaSwing开发一款纸牌触发器游戏(第一次使用JavaSwing)。我使用的是netbeans,我有一个像新游戏一样的菜单。。我想,当用户点击新游戏按钮,然后游戏开始。但我不知道怎么做,比如当用户点击按钮,然后在事件处理操作功能中,是这样的吗单击按钮打开Java Swing表单,java,swing,jframe,jbutton,multiple-instances,Java,Swing,Jframe,Jbutton,Multiple Instances,我正在用JavaSwing开发一款纸牌触发器游戏(第一次使用JavaSwing)。我使用的是netbeans,我有一个像新游戏一样的菜单。。我想,当用户点击新游戏按钮,然后游戏开始。但我不知道怎么做,比如当用户点击按钮,然后在事件处理操作功能中,是这样的吗 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TO
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JFrame myframe = new JFrame();
//and the game functionality here
}
如果您希望在单击按钮时打开一个新窗口,那么您所做的是正确的。在示例代码中,需要使新框架可见
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();
}
}