访问 JOption Pane 的按钮以向其添加 MouseListener
是的,我认为通过在组件上使用 .getComponents()
会相对简单,它将返回 JOptionPane
的 JPanel
并检索它们JButton
再次使用该方法与 JPanel
但是我遇到了困难。
我想在 JOptionPane
按钮上使用鼠标侦听器,以便可以在鼠标悬停时更改按钮的颜色。有没有更简单的方法来实现这一目标?
这是我到目前为止的课..
package rsapp.gui;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
public class RSJPaneComponent extends JOptionPane {
/**
*
*/
private static final long serialVersionUID = 13453253L;
private JOptionPane j=this;
final Color WHITE = Color.WHITE;
public RSJPaneComponent(){
UIManager.put("OptionPane.background",WHITE);
UIManager.put("Panel.background",WHITE);
UIManager.put("Button.background",WHITE);
UIManager.put("Button.foreground",new Color(85,153,187));
UIManager.put("activeCaption", WHITE);
}
protected String initJPaneInput(final JFrame f, final String message){
return j.showInputDialog(f,message);
}
public int generateDialog(int error_code, String title_message, String message, final JFrame f){
return JOptionPane.showConfirmDialog(
f,
message,
"Error "+error_code+": "+title_message,
JOptionPane.YES_NO_OPTION);
}
}
Right, I thought this would be relatively simple by using .getComponents()
on the Component which would return the JPanel
of the JOptionPane
and them retrieve the JButton
s by using that method again with the JPanel
however I am facing difficulties.
I want to use a mouse listener on the JOptionPane
buttons so that I can change the color of the button on rollover. Is there a simpler way of achieving this?
This is my class so far ..
package rsapp.gui;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
public class RSJPaneComponent extends JOptionPane {
/**
*
*/
private static final long serialVersionUID = 13453253L;
private JOptionPane j=this;
final Color WHITE = Color.WHITE;
public RSJPaneComponent(){
UIManager.put("OptionPane.background",WHITE);
UIManager.put("Panel.background",WHITE);
UIManager.put("Button.background",WHITE);
UIManager.put("Button.foreground",new Color(85,153,187));
UIManager.put("activeCaption", WHITE);
}
protected String initJPaneInput(final JFrame f, final String message){
return j.showInputDialog(f,message);
}
public int generateDialog(int error_code, String title_message, String message, final JFrame f){
return JOptionPane.showConfirmDialog(
f,
message,
"Error "+error_code+": "+title_message,
JOptionPane.YES_NO_OPTION);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
JDialog
。长期的经验告诉我,虽然JOptionPane
是一个强大的&方便的组件,一旦需要自定义它,您最好只使用JDialog
。代码
屏幕截图
请注意,此示例尚未将所有功能内置到
中JOptionPane
。例如,如果
JOptionPane
打开并且用户按转义键,则该对话框将被关闭。您可以使用KeyListener
或ActionMap
添加该功能。Use a
JDialog
. Long experience tells me that while aJOptionPane
is a powerful & handy component, once it comes to customizing it, you are better off simply using aJDialog
.Code
Screen Shot
Note that this example does not yet have all the functionality built-in to a
JOptionPane
.For example, if a
JOptionPane
is open and the user presses the escape key, the dialog will be dismissed. You might add that functionality using aKeyListener
orActionMap
.