0

Java中是否有一种为输入对话框的文本字段设置工具提示的好方法?

例如:

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

是否也可以为此文本字段提供右键单击粘贴选项?

先感谢您!

4

1 回答 1

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();
    }
}

默认情况下,右键单击并粘贴即可。

于 2013-04-07T20:46:54.097 回答