JOptionPane

Battleju

Aktives Mitglied
Hi,
ich will eine Abfrage erstellen, dass bevor das Programm geschlossen werden soll, noch einmal nachgefragt werden soll. Ich habe es mit der Methode JOptionPane versucht:
Code:
public void schliessen(){
    if(JOptionPane.showOptionDialog(null, "Willst du wirklich das Programm beendem?","Beenden",
    JOptionPane.YES_NO_OPTION,
    JOptionPane.QUESTION_MESSAGE, null,
    new String[]{"Ja", "Nein"}, "Nein") == JOptionPane.YES_OPTION){
      System.exit(0);
    }
  }
Nun ist es so, dass wenn man "Ja" drückt, alles nach Plan läuft. Nur wenn man "Nein" drückt, wird das erschienene Fenster und meine Swing GUI unsichtbar,also das Programm läuft noch im Hintergrund weiter. Ich weiß aber nicht warum😵. Kann mir da jemand weiterhelfen?
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class Aufgabe2bGUI3 extends JFrame implements ActionListener{
JLabel erklaerung = new JLabel("Wähle ein Modus aus:");
JButton minimum = new JButton("kleinste Zahl nennen");
JButton maximum = new JButton("größte Zahl nennen");
boolean gui1 = false;
boolean gui2 = false;
boolean gui3 = true;
public static void main(String[] arg){
new Aufgabe2bGUI3();
}
public Aufgabe2bGUI3(){
super(" Zahlenprüfer Menü");
setLayout(new FlowLayout());
add(erklaerung);
add(minimum);
add(maximum);
minimum.addActionListener(this);
maximum.addActionListener(this);
setSize(300,200);
setVisible(true);
addWindowListener(new FensterAufsicht());
}
class FensterAufsicht extends WindowAdapter {
public void windowClosing(WindowEvent e) {
schliessen();
}
}
public void actionPerformed (ActionEvent e){
if (e.getSource() == minimum) {
if (gui2) {
setVisible(false);
Aufgabe2bGUI2 min = new Aufgabe2bGUI2();
min.on();
} else{

setVisible(false);
new Aufgabe2bGUI2();
gui2 = true;
}
}else{
if (gui1) {
off();
Aufgabe2bGUI1 max = new Aufgabe2bGUI1();
max.on();
} else{
off();
new Aufgabe2bGUI2();
gui1 = true;
}
}
}
public void off(){
setVisible(false);
}
public void on(){
setVisible(true);
}
public void schliessen(){
if(JOptionPane.showOptionDialog(null, "Willst du wirklich das Programm beendem?","Beenden",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null,
new String[]{"Ja", "Nein"}, "Nein") == JOptionPane.YES_OPTION){
System.exit(0);
}
}
}

Lg bttl
 
Füg mal deinem JFrame zusätzlich noch setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); hinzu. Dann sollte es gehen.
Gruß Robert
 
Kommt er dann überhaupt noch zu seiner Abfrage ?
Sollte eigentlich..
Wenn man sich den JavaDoc zu der Methode anschaut:

JavaDoc setDefaultCloseOperation(..) hat gesagt.:
* Sets the operation that will happen by default when
* the user initiates a "close" on this frame.
* You must specify one of the following choices:
* <br><br>
* <ul>
* <li><code>DO_NOTHING_ON_CLOSE</code>
* (defined in <code>WindowConstants</code>):
* Don't do anything; require the
* program to handle the operation in the <code>windowClosing</code>
* method of a registered <code>WindowListener</code> object.

..
 

Neue Themen


Zurück
Oben