私の簡単なログインウィンドウでは、ログインボタンにアクションリスナーを追加しました。ボタンを押すと、プログラムはユーザーが入力した情報をチェックし、間違った情報を3回入力すると、プログラムがシャットダウンした後にユーザーに通知する新しいJDialogを作成します。Java SwingでSystem.exit()の前に情報JDialogを表示するにはどうすればよいですか? (クローズ)
私の問題は、JDialogが空白になってプログラムがシャットダウンすることです。ここで
は私のコードは
btnLogIn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
char[] pass = pssPassWord.getPassword();
String pss = new String(pass);
if (!pss.toString().equals("admin") || !txtUserName.getText().equals("Adminuser")){
if (attempts == 2){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Inform("You entered the wrong info 3 times, the program will shut down");
try {
Thread.sleep(1000);
System.exit(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}else {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Info("Wrong info, please check the info");
}
});
attempts += 1;
}
}else{
}
}
});
ですこれは私のクラスに通知されます。
public class Inform extends JDialog{
private JPanel pnlInform;
private JLabel lblInform;
private JButton btnOK;
public Inform(String info){
setContentPane(pnlInform);
lblInform.setText(info);
pack();
setVisible(true);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
btnOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
dispose();
}
});
}
}
私のIDEはのIntelliJ IDEAであり、私はGUIを作成するために、フォームクラスを使用し、そうあり要素の宣言はありません。 pnlInform
が構築され、resolvs null
にし、それがcontentPane cannot be set to null.
ソリューションを介しますされていないので、あなたがInform
ダイアログに任意のコンポーネントを追加didntの
すぐに終了しますか?それでは、 'Info'に表示する時間を与える必要があるかもしれません。 –
なぜ 'actionPerformed()'の 'invokeLater()'ですか?あなたはすでにEDTで走っていませんか? –
@KevinWallis Thread.sleep(1000)を使用したので、1秒後に終了します。その前に、Info JDialogは表示されますが空白です。 –