Java:showInputDialog中的自定义按钮 [英] Java: Custom Buttons in showInputDialog

查看:195
本文介绍了Java:showInputDialog中的自定义按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将自定义文本添加到JOptionPane.showInputDialog的按钮?

How do you add custom text to the buttons of a JOptionPane.showInputDialog?

我知道这个问题带有自定义按钮的JOptionPane showInputDialog ,但它没有回答问题,它只是将它们引用到JavaDocs,后者没有回答它。

I know about this question JOptionPane showInputDialog with custom buttons, but it doesn't answer the question asked, it just references them to JavaDocs, which doesn't answer it.

代码到目前为止:

对象[] options1 = {试试这个号码,
选择随机数,
退出};

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

我想在此添加一个文本字段。

I would like to add a text field to this.

推荐答案

您可以使用自定义组件而不是字符串消息,例如:

You can use custom component instead of a string message, for example:

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

这篇关于Java:showInputDialog中的自定义按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆