Avevo creato un JOptionPane
di tipo showInputDialog
. Quando si apre, mi mostra due pulsanti: OK e Annulla. Mi piacerebbe gestire l'azione quando spingo il pulsante Annulla, ma non so come raggiungerlo. Come posso averlo?Come gestire il pulsante Annulla in JOptionPane
risposta
Ad esempio:
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 {
}
alternativa con 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 {
}
Vedere How to Make Dialogs per maggiori dettagli.
EDIT: showInputDialog
String response = JOptionPane.showInputDialog(owner, "Input:", "");
if ((response != null) && (response.length() > 0)) {
}
Ho bisogno di usare showInputDialog che mi restituisce un oggetto stringa – Mazzy
ShowMessageDialog, non dovrebbe mostrare due pulsanti, quindi qualcosa non va bene con il tuo codice o la tua interpretazione di esso. Indipendentemente da ciò, se si desidera dare all'utente una scelta e si desidera rilevare tale scelta, non utilizzare showMessageDialog ma piuttosto showConfirmDialog e ottenere l'int restituito e verificarlo per verificare se si tratta di JOptoinPane.OK_OPTION.
ho commesso un errore. .. Voglio dire showInputDialog. Ho come risultato un oggetto stringa – Mazzy
Questa è una vecchia questione, e io sono un newbie Java quindi ci potrebbero essere soluzioni migliori, ma volevo sapere lo stesso, e forse può aiutare gli altri, quello che ho è stato per verificare se la risposta era nullo.
questo ha funzionato per me:
//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)
}
Ma il tuo codice non gestisce una situazione, se il tuo input è vuoto e premi OK. Hai un errore. – frank17
Questa può essere la risposta:
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;
}
}
}
}
Anche se questo blocco di codice può rispondere alla domanda, sarebbe meglio se fosse possibile fornire una piccola spiegazione del motivo per cui lo fa. Per favore [modifica] la tua risposta per includere una tale descrizione. –
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;
}
}
}
}
Si prega di aggiungere una spiegazione di come funziona il codice. –
http://stackoverflow.com/a/10966330/829571 – assylias