JOptionsPane 在“选项”中显示组件参数为 Component.toString()
我试图弹出一个对话框,以允许用户选择两种颜色之一作为背景颜色。为了使其看起来特别漂亮,我希望以相关颜色显示两个选择,即:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Java API 对于这一点还不太清楚。顶部描述了如何解释
选项
,但选项
是用户可以选择的“是”、“否”、“取消”...可能性,绘制在按钮行中。您正在谈论selectionValues
,然后是 API(转到名为showInputDialog
的最后一个方法)很清楚:selectionValues
- 提供可能选择的对象数组由 UI 决定如何最好地表示选择值,但通常会使用 JComboBox、JList 或 JTextField。
根据我的经验,在
selectionValues
中传递的对象是使用toString()
显示的,结果显示在JComboBox
或JList
,因此您无法通过自定义绘画显示选择值;您需要为此实现自己的对话框。您可以将
消息
作为组件
传递,这样您就可以向用户提供有关selectionValues
的图例,您可以在其中显示带有背景颜色的标签指示每种可用颜色,从而帮助从selectionValues
中选择值。The Java API is slighly unclear about this point. At the top describes how to interpret the
options
, butoptions
are the YES, NO, CANCEL... possibilities the user can choose, painted in the buttons row. You are talking about theselectionValues
, and then the API (go to last method namedshowInputDialog
) is clear:selectionValues
- an array of Objects that gives the possible selectionsIt 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 usingtoString()
and the result is shown in aJComboBox
or aJList
, 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 aComponent
so you can provide a legend to the user about theselectionValues
, where you can show labels with background colors to indicate each color available and thus provide assitance selecting a value fromselectionValues
.showInputDialog 中应该是字符串数组而不是 jbutton 数组(可能),但这样您就不会具有背景颜色。我认为不存在任何方法可以在 showInputDialog() 中实现此类颜色选择器
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()