Java 按钮上有图像的JOptionPane?
如何创建按钮上除了标签之外还有图像的JOptionPane?例如,如果我想在OK按钮上打勾,在cancel按钮上打x图标?如果不从头开始将整个对话框创建为JFrame/JPanel,这是可能的吗?Java 按钮上有图像的JOptionPane?,java,image,swing,jbutton,joptionpane,Java,Image,Swing,Jbutton,Joptionpane,如何创建按钮上除了标签之外还有图像的JOptionPane?例如,如果我想在OK按钮上打勾,在cancel按钮上打x图标?如果不从头开始将整个对话框创建为JFrame/JPanel,这是可能的吗?JOptionPane.showOptionDialog()有一个参数options,它是一个组件的数组。 您可以向其传递一组自定义按钮: JOptionPane.showOptionDialog( parent, question, title, JOptionPane.YES_NO_OPTIO
JOptionPane.showOptionDialog()
有一个参数options
,它是一个组件的数组。
您可以向其传递一组自定义按钮:
JOptionPane.showOptionDialog( parent, question, title,
JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE
new Component[]{ new JButton("OK", myIcon),
new JButton("cancel", myOtherIcon)
}
);
从作业窗格
的文档中:
选项-指示用户可能选择的对象数组
能使;如果对象是组件,则会正确渲染它们
或者,您可以将JOptionPane
子类化,并直接更改组件及其布局。JOptionPane.showOptionDialog()
有一个参数options
,它是组件的数组。
您可以向其传递一组自定义按钮:
JOptionPane.showOptionDialog( parent, question, title,
JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE
new Component[]{ new JButton("OK", myIcon),
new JButton("cancel", myOtherIcon)
}
);
从作业窗格
的文档中:
选项-指示用户可能选择的对象数组
能使;如果对象是组件,则会正确渲染它们
或者,您可以将JOptionPane
子类化,并直接更改组件及其布局。我在上找到了一个看起来稍微混乱的解决方案,该解决方案似乎可以实际工作,并响应按钮点击和操作侦听器:
JFrame frame = new JFrame();
JOptionPane optionPane = new JOptionPane();
optionPane.setMessage("I got an icon and a text label");
optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
Icon icon = new ImageIcon("yourFile.gif");
JButton jButton = getButton(optionPane, "OK", icon);
optionPane.setOptions(new Object[] { jButton });
JDialog dialog = optionPane.createDialog(frame, "Icon/Text Button");
dialog.setVisible(true);
}
public static JButton getButton(final JOptionPane optionPane, String text, Icon icon) {
final JButton button = new JButton(text, icon);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
// Return current text label, instead of argument to method
optionPane.setValue(button.getText());
System.out.println(button.getText());
}
};
button.addActionListener(actionListener);
return button;
}
我在上找到了一个看起来有点混乱的解决方案,该解决方案似乎可以实际工作,并通过按钮点击和操作侦听器进行响应:
JFrame frame = new JFrame();
JOptionPane optionPane = new JOptionPane();
optionPane.setMessage("I got an icon and a text label");
optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
Icon icon = new ImageIcon("yourFile.gif");
JButton jButton = getButton(optionPane, "OK", icon);
optionPane.setOptions(new Object[] { jButton });
JDialog dialog = optionPane.createDialog(frame, "Icon/Text Button");
dialog.setVisible(true);
}
public static JButton getButton(final JOptionPane optionPane, String text, Icon icon) {
final JButton button = new JButton(text, icon);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
// Return current text label, instead of argument to method
optionPane.setValue(button.getText());
System.out.println(button.getText());
}
};
button.addActionListener(actionListener);
return button;
}
我也有同样的问题。使用此操作侦听器解决:
JButton ok = new JButton("OK");
ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Container parent = ok.getParent();
while (parent != null && !(parent instanceof JDialog)) {
parent = parent.getParent();
}
parent.setVisible(false);
}
});
我也有同样的问题。使用此操作侦听器解决:
JButton ok = new JButton("OK");
ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Container parent = ok.getParent();
while (parent != null && !(parent instanceof JDialog)) {
parent = parent.getParent();
}
parent.setVisible(false);
}
});
在大多数情况下,这似乎是在正确的轨道上的解决方案,然而,这不是一个完全为我工作。看起来我还需要指定几个参数(可能不是所有的java版本都相同?我使用的是java 7),我已经为这些参数添加了null,但是,当您按OK或Cancel时,什么都不会发生。自定义按钮是否需要动作监听器来触发“ok”动作?在大多数情况下,这似乎是正确的解决方案,但对我来说,这并不是一个完全有效的解决方案。看起来我还需要指定几个参数(可能不是所有的java版本都相同?我使用的是java 7),我已经为这些参数添加了null,但是,当您按OK或Cancel时,什么都不会发生。自定义按钮是否需要操作侦听器来触发“确定”操作?一旦您开始自定义JOptionPane
实例,它就会变得混乱,正如您所发现的那样。通常情况下,最好是避开它并使用JDialog
+1为了找到解决方案,尽管也要查看Action
API以代替ActionListener
。一旦您开始自定义JOptionPane
实例,它就会变得混乱,正如您所发现的那样。通常情况下,最好是避开它并使用JDialog
+1要找到解决方案,还需要查看Action
API以代替ActionListener
。