JOptionsPane 在“选项”中显示组件参数为 Component.toString()

发布于 2024-10-25 23:00:13 字数 1316 浏览 1 评论 0原文

我试图弹出一个对话框,以允许用户选择两种颜色之一作为背景颜色。为了使其看起来特别漂亮,我希望以相关颜色显示两个选择,即:

import java.awt.Color;
import java.awt.Label;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class JOptionPaneTest extends JFrame{

    public static void main(String[] args) {
        new JOptionPaneTest();
    }

    public JOptionPaneTest() {
        Object[] possibilities = new Object[2];
        JButton black = new JButton("Black");
        JButton white = new JButton("White");
        black.setBackground(Color.black);
        white.setBackground(Color.white);
        black.setForeground(Color.white);
        white.setForeground(Color.black);
        possibilities[0] = black;
        possibilities[1] = white;

        JButton l = (JButton)JOptionPane.showInputDialog(this, 
                "Please specify the background color", "Background check",
                JOptionPane.QUESTION_MESSAGE, null, possibilities,
                possibilities[0]);
        System.out.println("" + l);
    }
}

但是,这不起作用 - 它在下拉列表中显示 JButton.toString() 返回值,而不是J 按钮。我还尝试了 JLabel 和 Label。根据 API,JButtons 应按原样添加到对话框中它们是组件。如果我将 JButton 添加到“消息”参数,它会按预期显示。

知道我做错了什么吗?

I'm trying to pop up a dialog to allow the user to select one of two colors as a background color. To make it look especially spiffy, I'd like to two choices to be displayed in the color in question, i.e.:

import java.awt.Color;
import java.awt.Label;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class JOptionPaneTest extends JFrame{

    public static void main(String[] args) {
        new JOptionPaneTest();
    }

    public JOptionPaneTest() {
        Object[] possibilities = new Object[2];
        JButton black = new JButton("Black");
        JButton white = new JButton("White");
        black.setBackground(Color.black);
        white.setBackground(Color.white);
        black.setForeground(Color.white);
        white.setForeground(Color.black);
        possibilities[0] = black;
        possibilities[1] = white;

        JButton l = (JButton)JOptionPane.showInputDialog(this, 
                "Please specify the background color", "Background check",
                JOptionPane.QUESTION_MESSAGE, null, possibilities,
                possibilities[0]);
        System.out.println("" + l);
    }
}

However, this doesn't work - it displays the JButton.toString() return values in a drop down instead of the JButton. I also tried JLabel and Label for the heck of it. According to the API, the JButtons should be added to the dialog as is since they're Components. If I add the JButton to the 'message' parameter it does display as expected.

Any idea what I'm doing wrong?

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

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

发布评论

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

评论(2

去了角落 2024-11-01 23:00:13

Java API 对于这一点还不太清楚。顶部描述了如何解释选项,但选项是用户可以选择的“是”、“否”、“取消”...可能性,绘制在按钮行中。您正在谈论 selectionValues,然后是 API(转到名为 showInputDialog 的最后一个方法)很清楚:

selectionValues - 提供可能选择的对象数组
由 UI 决定如何最好地表示选择值,但通常会使用 JComboBox、JList 或 JTextField。

根据我的经验,在 selectionValues 中传递的对象是使用 toString() 显示的,结果显示在 JComboBoxJList,因此您无法通过自定义绘画显示选择值;您需要为此实现自己的对话框。

您可以将消息作为组件传递,这样您就可以向用户提供有关selectionValues的图例,您可以在其中显示带有背景颜色的标签指示每种可用颜色,从而帮助从 selectionValues 中选择值。

The Java API is slighly unclear about this point. At the top describes how to interpret the options, but options are the YES, NO, CANCEL... possibilities the user can choose, painted in the buttons row. You are talking about the selectionValues, and then the API (go to last method named showInputDialog) is clear:

selectionValues - an array of Objects that gives the possible selections
It is up to the UI to decide how best to represent the selectionValues, but usually a JComboBox, JList, or JTextField will be used.

From my experience, the objects passed in the selectionValues are displayed using toString()and the result is shown in a JComboBox or a JList, so you can not show selection values with custom painting; you need to implement you own dialog for that.

You can pass the message as a Component so you can provide a legend to the user about the selectionValues, where you can show labels with background colors to indicate each color available and thus provide assitance selecting a value from selectionValues.

余生一个溪 2024-11-01 23:00:13

showInputDialog 中应该是字符串数组而不是 jbutton 数组(可能),但这样您就不会具有背景颜色。我认为不存在任何方法可以在 showInputDialog() 中实现此类颜色选择器

String[] str = {"aaa", "bbb"};

JButton l = (JButton)JOptionPane.showInputDialog(this, 
            "Please specify the background color", "Background check",
            JOptionPane.QUESTION_MESSAGE, null, str, str[0]);

Should be array of strings not array of jbutton (possibilites) in showInputDialog, but in this way you won't have background color. I don't think it exists any way to implement such colors chooser in showInputDialog()

String[] str = {"aaa", "bbb"};

JButton l = (JButton)JOptionPane.showInputDialog(this, 
            "Please specify the background color", "Background check",
            JOptionPane.QUESTION_MESSAGE, null, str, str[0]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文