if (selected == 0) { // OK
// tu, was nötig ist...
}
Die Methode showOptionDialog liefert "an integer indicating the option chosen by the user, or CLOSED_OPTION if the user closed the dialog". Sprich: entweder CLOSED_OPTION oder 0 für "OK" bzw. 1 für "Nein". Das kannst Du einfach z. B. per if-Statement abfragen:
Java:if (selected == 0) { // OK // tu, was nötig ist... }
Was heißt "von vorne beginnen"?So nebenbei kann ich mit JoptionPane eigentlich auch beim Drücken des Buttons das Programm von vorne beginnen
Hallo,
finde nicht heraus wie ich den Button ermittle, welcher bei meinem OptionDialog betätigt wurde. Möchte 2 unterschiedliche Ereignisse auslösen.
import java.util.Random;
import javax.swing.JOptionPane;
public class Ausgabe {
final static Random rnd = new Random(System.currentTimeMillis());
final static int ABORT_INPUT = Integer.MIN_VALUE;
public static void main(String[] args) {
int cntError = 0; // Fehlversuche
int cntCorrect = 0; // Richtige
int cnt = 0; // Anzahl der gestellten Aufgaben
do {
RndMathCalculation calc = new RndMathCalculation();
String question = "Was ist das Ergebnis aus " + calc.getText() + "?";
int eingabe = inputResult(question);
if (eingabe == ABORT_INPUT)
end(cnt, cntCorrect, cntError);
if (eingabe == calc.getResult()) {
JOptionPane.showMessageDialog(null, "Glückwunsch " + eingabe + " ist richtig!");
cntCorrect++;
} else {
JOptionPane.showMessageDialog(null, "Leider ist " + eingabe + " falsch!");
cntError++;
}
cnt++;
} while (true);
}
private static void end(int cnt, int cntCorrect, int cntError) {
String statistic = "Aufgaben: " + cnt + "\n" + "Richtige: " + cntCorrect + "\n" + "Fehler: " + cntError;
JOptionPane.showMessageDialog(null, statistic + "\nAuf Wiedersehen!");
System.exit(0);
}
private static int inputResult(String question) {
int eingabe = 0;
boolean ok = false;
do {
try {
String input = JOptionPane.showInputDialog(null, question, "Rechenoperation",
JOptionPane.QUESTION_MESSAGE);
if (input == null)
return ABORT_INPUT;
eingabe = Integer.parseInt(input);
ok = true;
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Bitte nur ganze Zahlen eingeben!");
}
} while (!ok);
return eingabe;
}
}
So dass bei Betätigen des Knopfes das Programm quasi wieder von vorne gestartet wird.Was heißt "von vorne beginnen"?
Ich möchte ja nicht merken, aber "das Programm quasi wieder von vorne gestartet wird." ist einer eher schlechte Erklärung für "von vorne beginnen"So dass bei Betätigen des Knopfes das Programm quasi wieder von vorne gestartet wird.Was heißt "von vorne beginnen"?