2010-09-12 26 views

回答

2
import java.awt.*; 
import javax.swing.*; 
import javax.swing.event.*; 

class ShowBothInputs { 

    public static void main(String[] args) { 

     Runnable r = new Runnable() { 

      public void run() { 
       String[] items = { 
        "Apple", 
        "Banana", 
        "Grape", 
        "Cherry" 
       }; 

       // what was requested 
       EditableListPanel elp = new EditableListPanel(items); 
       JOptionPane.showMessageDialog(null, elp); 
       System.out.println("EditableListPanel value: " + elp.getValue()); 

       // probably what this UI actually needs 
       JComboBox jcb = new JComboBox(items); 
       jcb.setEditable(true); 
       JOptionPane.showMessageDialog(null, jcb); 
       System.out.println("JComboBox value: " + jcb.getSelectedItem()); 
      } 
     }; 
     SwingUtilities.invokeLater(r); 
    } 
} 

class EditableListPanel extends JPanel { 

    private JTextField value; 

    EditableListPanel(String[] items) { 
     super(new BorderLayout(5,5)); 

     final JList list = new JList(items); 
     list.addListSelectionListener(new ListSelectionListener(){ 
       public void valueChanged(ListSelectionEvent lse) { 
        value.setText((String)list.getSelectedValue()); 
       } 
      }); 
     add(list, BorderLayout.CENTER); 

     value = new JTextField("", 20); 
     add(value, BorderLayout.NORTH); 
    } 

    public String getValue() { 
     return value.getText(); 
    } 
} 
+0

第二個對話框正是我要找的。 – user 2010-09-12 14:36:31

0

JDialogs通常用於向用戶顯示通知。我認爲,如果你想要這樣的行爲,你將不得不創建一個簡單的JFrame來滿足你的需求。

0

不確定「選擇後編輯文本」是什麼意思。

但是,您可以使用JOptionPane輕鬆創建一個簡單的對話框。您可以將JPanel添加到選項窗格。查看Dialog Focus以獲取更多信息。

+0

我的意思是你可以從下拉列表中選擇一個值,但是,你也可以編輯它 – user 2010-09-12 14:06:24

相關問題