2013-04-07 49 views
0

在Java中設置輸入對話框文本框的工具提示有沒有好的方法?Java輸入對話框工具提示和粘貼選項?

例如:

String input = JOptionPane.showInputDialog("Enter input:");

是否也可以有這個文本框右鍵單擊粘貼選項?

提前感謝您!

回答

2

我不認爲有可能通過該靜態方法添加工具提示。我建議你創建一個你自己的JOptionPane實例,找到JTextField並設置它的工具提示。

public class Main { 

    public static List<Component> getAllComponents(final Container c) { 
     Component[] comps = c.getComponents(); 
     List<Component> compList = new ArrayList<Component>(); 
     for (Component comp : comps) { 
      compList.add(comp); 
      if (comp instanceof Container) 
       compList.addAll(getAllComponents((Container) comp)); 
     } 
     return compList; 
    } 

    public static void main(String[] args) { 
     JOptionPane optionPane = new JOptionPane(); 
     optionPane.setMessage("What's your name?"); 
     optionPane.setMessageType(JOptionPane.QUESTION_MESSAGE); 
     optionPane.setWantsInput(true); 
     JDialog dialog = optionPane.createDialog("Simple Question"); 
     for (Component c : getAllComponents(dialog)) { 
     if (c instanceof JTextField) { 
      c.setToolTipText("I'm a tooltip!"); 
     } 
     } 
     dialog.setVisible(true); 
     dialog.dispose(); 
    } 
} 

默認情況下右鍵單擊並粘貼。

+0

你應該在'setVisible'後面加上'dialog.dispose()'來釋放資源,否則這些資源會永遠運行。 (: – 2013-04-07 21:05:12

+0

所以,請修正。謝謝:) – 2013-04-08 13:44:11