最初のウィンドウで1つのボタンを押すと、自分のJavaプログラムが新しいウィンドウを開きます。それから私は最初のウィンドウを閉じる必要があります。 System.exit(0);
で最初のウィンドウを閉じると、2番目のウィンドウが閉じます。私はsetVisible(false);
を試して、最初のウィンドウが閉じない。助けてください!JAVA - 2つのウィンドウがあり、1つのウィンドウを閉じる必要があります
全体コード:
public class NameChooser extends JFrame implements ActionListener {
public NameChooser() {
RunNC();
}
public final void RunNC() {
JPanel panel = new JPanel();
JLabel label = new JLabel("Enter your name that will be shown in game!");
label.setBounds(20, 10, 500, 25);
panel.add(label);
JLabel error = new JLabel("");
error.setForeground(Color.red);
panel.add(error);
JTextField name = new JTextField(30);
name.setBounds(50, 40, 180, 25);
panel.add(name);
JButton playButton = new JButton(new AbstractAction("Play") {
@Override
public void actionPerformed(ActionEvent e) {
String enteredname = name.getText();
if("".equals(enteredname)) {
error.setVisible(true);
error.setText("Invalid name!");
error.setBounds(105, 95, 100, 25);
System.out.println("Invalid name!");
}
else if(enteredname.length() > 10) {
error.setVisible(true);
error.setText("Name cant have more than 10 characters!");
error.setBounds(25, 95, 600, 25);
System.out.println("Name cant have more than 10 characters!");
}
error.setVisible(false);
GameWindow game = new GameWindow();
game.StartGame();
// I need to close window on this line!
}
});
playButton.setBounds(110, 70, 60, 25);
panel.add(playButton);
}
任意のヘルプ?
あなたのコードはどこですか?何か試しましたか? – Edwin