2012-07-15 16 views
8

Ich hatte eine JOptionPane des Typs showInputDialog erstellt. Wenn es geöffnet wird, zeigt es mir zwei Tasten: OK und Abbrechen. Ich würde gerne mit der Aktion umgehen, wenn ich auf Abbrechen Knopf, aber ich weiß nicht, wie man es erreicht. Wie kann ich es bekommen?So behandeln Sie die Schaltfläche Abbrechen in JOptionPane

+0

http://stackoverflow.com/a/10966330/829571 – assylias

Antwort

21

Zum Beispiel:

int n = JOptionPane.showConfirmDialog(
          frame, "Would you like green eggs and ham?", 
          "An Inane Question", 
          JOptionPane.YES_NO_OPTION); 
if (n == JOptionPane.YES_OPTION) { 

} else if (n == JOptionPane.NO_OPTION) { 

} else { 

} 

Alternativ mit showOptionDialog:

Object[] options = {"Yes, please", "No way!"}; 
int n = JOptionPane.showOptionDialog(frame, 
       "Would you like green eggs and ham?", 
       "A Silly Question", 
       JOptionPane.YES_NO_OPTION, 
       JOptionPane.QUESTION_MESSAGE, 
       null, 
       options, 
       options[0]); 
if (n == JOptionPane.YES_OPTION) { 

} else if (n == JOptionPane.NO_OPTION) { 

} else { 

} 

Siehe How to Make Dialogs für weitere Details.

EDIT: showInputDialog

String response = JOptionPane.showInputDialog(owner, "Input:", ""); 
if ((response != null) && (response.length() > 0)) { 

} 
+1

Ich brauche showInputDialog, die mir ein String-Objekt – Mazzy

6

Der showMessageDialog sollte keine zwei Schaltflächen anzeigen, daher stimmt etwas mit Ihrem Code oder Ihrer Interpretation nicht überein. Unabhängig davon, ob Sie dem Benutzer eine Auswahlmöglichkeit geben möchten und diese Wahl treffen möchten, verwenden Sie keinen showMessageDialog, sondern einen showConfirmDialog, und rufen Sie den int zurück und testen Sie, ob es sich um JOptoinPane.OK_OPTION handelt.

+1

ich einen Fehler gemacht. .. ich meine showInputDialog. Ich habe als Rückgabe ein String-Objekt – Mazzy

1

Dies ist eine alte Frage, und ich bin ein Neuling Java so könnte es bessere Lösungen sein, aber ich wollte das gleiche wissen, und vielleicht kann es anderen helfen, was ich war zu überprüfen, ob die Antwort null war.

Dieser arbeitete für mich:

//inputdialog 
    JOptionPane inpOption = new JOptionPane(); 

    //Shows a inputdialog 
    String strDialogResponse = inpOption.showInputDialog("Enter a number: "); 

    //if OK is pushed then (if not strDialogResponse is null) 
    if (strDialogResponse != null){ 

     (Code to do something if the user push OK) 

    } 
    //If cancel button is pressed 
    else{ 

     (Code to do something if the user push Cancel) 

    } 
+0

zurückgeben Aber Ihr Code nicht behandelt eine Situation, wenn Sie Ihre Eingabe leer ist und drücken Sie die OK-Taste. Du bekommst einen Fehler. – frank17

-1

dies Ihre Antwort sein:

package Joptionpane; 

import javax.swing.JOptionPane; 

public class Cancle_on_JOptionPane { 

    public static void main(String[] args) { 
     String s; 
     int i; 
     for(i=0;i<100;i++){ 
     s = JOptionPane.showInputDialog(null,"What is your favorite fruit ?"); 
         try{ 
           if(s.equals("")) { 
                  JOptionPane.showMessageDialog(null," Enter your answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE); 
                  i=2; 
                 } 
           else { 
              JOptionPane.showMessageDialog(null," s = "+s," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE); 
              i=100; 
             } 
          } 
     catch(Exception e) 
       { 
       JOptionPane.showMessageDialog(null,"Cancle answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE); 
       i=100; 
       } 
    } 
    } 
} 
+0

Während dieser Codeblock die Frage beantworten kann, wäre es am besten, wenn Sie eine kleine Erklärung dafür liefern könnten, warum dies der Fall ist. Bitte [bearbeiten] Sie Ihre Antwort mit einer solchen Beschreibung. –

0
package Joptionpane; 

import javax.swing.JOptionPane; 

public class Cancle_on_JOptionPane { 

    public static void main(String[] args) { 
     String s; 
     int i; 
     for (i=0;i<100;i++){ 
      s = JOptionPane.showInputDialog(null,"What is your favorite fruit ?"); 
      try { 
       if (s.equals("")) { 
        JOptionPane.showMessageDialog(null," Enter your answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE); 
        i=2; 
       } else { 
        JOptionPane.showMessageDialog(null," s = "+s," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE); 
        i=100; 
       } 
      } 
      catch (Exception e) { 
       JOptionPane.showMessageDialog(null,"Cancle answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE); 
       i=100; 
      } 
     } 
    } 
} 
+0

Bitte fügen Sie eine Erklärung hinzu, wie der Code funktioniert. –

Verwandte Themen