2012-11-11 12 views
11

Come si aggiunge testo personalizzato ai pulsanti di un JOptionPane.showInputDialog?Java: Pulsanti personalizzati in showInputDialog

Conosco questa domanda JOptionPane showInputDialog with custom buttons, ma non risponde alla domanda posta, fa semplicemente riferimento a JavaDocs, che non risponde.

Codice So Far:
Object[] options1 = {"Try This Number", "Choose A Random Number", "Quit"};

JOptionPane.showOptionDialog(null, 
       "Enter a number between 0 and 10000", 
       "Enter a Number", 
       JOptionPane.YES_NO_CANCEL_OPTION, 
       JOptionPane.PLAIN_MESSAGE, 
       null, 
       options1, 
       null); 

How I want it to look

vorrei aggiungere un campo di testo a questo.

+1

Potrebbe essere anche ampliata sulle tue esigenze un po 'di più. Inoltre, cosa hai finora? – Whymarrh

+0

Il riferimento ai javadoc (in questo caso almeno) è la risposta corretta. Non c'è bisogno di scrivere qui ciò che altri hanno già scritto. – vainolo

risposta

18

È possibile utilizzare componente personalizzato al posto di un messaggio di stringa, per esempio:

import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

public class TestDialog { 

    public static void main(String[] args) { 
     Object[] options1 = { "Try This Number", "Choose A Random Number", 
       "Quit" }; 

     JPanel panel = new JPanel(); 
     panel.add(new JLabel("Enter number between 0 and 1000")); 
     JTextField textField = new JTextField(10); 
     panel.add(textField); 

     int result = JOptionPane.showOptionDialog(null, panel, "Enter a Number", 
       JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, 
       null, options1, null); 
     if (result == JOptionPane.YES_OPTION){ 
      JOptionPane.showMessageDialog(null, textField.getText()); 
     } 
    } 
} 

enter image description here

+0

e come si ottiene il valore dell'input? – ZuluDeltaNiner

+0

@ZuluDeltaNiner dal campo di testo che si trova nel pannello. Si prega di vedere l'ultima modifica. – tenorsax

+0

Se era solo un prompt e non c'era un campo di testo, come si poteva ottenere il risultato? –

8

Dai un'occhiata allo How to Make Dialogs: Customizing Button Text.

Ecco un esempio dato:

enter image description here

Object[] options = {"Yes, please", 
        "No, thanks", 
        "No eggs, no ham!"}; 
int n = JOptionPane.showOptionDialog(frame,//parent container of JOptionPane 
    "Would you like some green eggs to go " 
    + "with that ham?", 
    "A Silly Question", 
    JOptionPane.YES_NO_CANCEL_OPTION, 
    JOptionPane.QUESTION_MESSAGE, 
    null,//do not use a custom Icon 
    options,//the titles of buttons 
    options[2]);//default button title 
Problemi correlati