带有可编辑文本字段和列表的 JOptionPane 对话框

发布于 2024-09-19 11:05:24 字数 70 浏览 3 评论 0原文

是否可以创建一个对话框,用户可以在其中从列表中选择值,但也可以编辑文本? (类似于带有可编辑 JComboBox 的对话框。)

Is it possible to create a dialog where the user can select values from a list, but also can edit the text?
(Something like a dialog with an editable JComboBox.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

日裸衫吸 2024-09-26 11:05:24
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();
    }
}
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();
    }
}
巴黎盛开的樱花 2024-09-26 11:05:24

JDialog 通常用于向用户显示通知。我认为如果您想要这样的行为,您将必须创建一个简单的 JFrame 来满足您的需要。

JDialogs are usually used to display notifications to the user. I think that if you want such behaviour you will have to create a simple JFrame that does what you need.

三人与歌 2024-09-26 11:05:24

不确定“选择后编辑文本”是什么意思。

但是,您可以使用 JOptionPane 轻松创建简单的对话框。您可以将 JPanel 添加到选项窗格。请查看对话框焦点了解更多信息。

Not sure what you mean by "edit the text after selection".

However, you can easily create a simple dialog using a JOptionPane. You can add a JPanel to the option pane. Check out Dialog Focus for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文