如何向 JOptionPane 按钮添加鼠标侦听器?

我想更改 JOptionPane.ShowMessageDialog 上按钮的外观。我已经设法更改按钮标题


UIManager.put("OptionPane.okButtonText", "Text I want");

现在,我的下一个目标是使 Button 与我的应用程序其余部分中的按钮一样工作。也就是说,当鼠标悬停在其上时,它会更改背景和字体颜色。在我的其余按钮上,我添加了鼠标侦听器,如下所示:


    //setting change color on hover

        private final MouseListener mouseAction = new MouseAdapter() {

            @Override

            public void mouseEntered(MouseEvent e) {

                JButton rollOver = (JButton)e.getSource();

                if (rollOver.isEnabled()) {

                    rollOver.setBackground(new Color(163, 184, 204));

                    rollOver.setForeground(Color.WHITE);

                    rollOver.setFont(b);

                }

            };


            @Override

            public void mouseExited(MouseEvent e) {

                JButton rollOver = (JButton)e.getSource();

                if (rollOver.isEnabled()) {

                    rollOver.setBackground(new Color(230, 230, 230));

                    rollOver.setForeground(Color.BLACK);

                    rollOver.setFont(f);

                }

            };

        };

以前在代码中我设置了字体变量:


    Font f = new Font("System", Font.PLAIN, 12);

    Font b = new Font("System", Font.BOLD, 12);

我可以从头开始创建新的对话框并实现这种行为,但这太过分了。


有没有办法访问 JOptionPane 上的 Button 并向其添加鼠标侦听器?


墨色风雨
浏览 148回答 2
2回答

慕妹3242003

UIManager.put("OptionPane.okButtonText", "Text I want");上面的代码将更改您创建的所有 JOptionPanes 上所有“确定”按钮的文本。如果您想要更改特定 JOptionPane 上单个按钮上的文本。有没有办法访问 JOptionPane 上的 Button 并向其添加鼠标侦听器?当您使用静态showXXX(...)方法时,会创建模态 JDialog,因此您无法访问该对话框或其组件,直到对话框关闭为止,但为时已晚。因此,您需要手动创建JOptionPane并将其添加到JDialog. 通过阅读JOptionPane API和查看标题为 的部分可以找到执行此操作的基础知识"Direct Use"。创建后JOptionPane(在使对话框可见之前),您可以在选项窗格中搜索按钮并向MouseListener每个按钮添加 。为了帮助您完成此操作,您可以使用Swing Utils类。它将对选项窗格进行递归搜索,并将按钮以List. 然后您可以迭代List并添加MouseListener.使用此帮助程序类的基本代码是:JOptionPane optionPane = new JOptionPane(    "Are you sure you want to exit the application",    JOptionPane.QUESTION_MESSAGE,    JOptionPane.YES_NO_CANCEL_OPTION);List<JButton> buttons = SwingUtils.getDescendantsOfType(JButton.class, optionPane, true);for (JButton button: buttons){    System.out.println( button.getText() );}

呼唤远方

如果你想在所有OptionPanel中看到相同的效果,我认为覆盖BasicOptionPaneUI是一个很好的解决方案这是一个最小的例子public class MyOptionPaneUI extends BasicOptionPaneUI {&nbsp; &nbsp; @SuppressWarnings({"MethodOverridesStaticMethodOfSuperclass", "UnusedDeclaration"})&nbsp; &nbsp; public static ComponentUI createUI(JComponent c) {&nbsp; &nbsp; &nbsp; &nbsp; return new MyOptionPaneUI();&nbsp; &nbsp; }&nbsp; &nbsp; private static final MyMouseListener m = new MyMouseListener();&nbsp; &nbsp; @Override&nbsp; &nbsp; public void update(Graphics g, JComponent c) {&nbsp; &nbsp; &nbsp; &nbsp; super.update(g, c);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void installListeners() {&nbsp; &nbsp; &nbsp; &nbsp;JButton button = (JButton) getButtons()[0];&nbsp; &nbsp; &nbsp; &nbsp; button.addMouseListener(m);&nbsp; &nbsp; &nbsp; &nbsp; super.installListeners();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void uninstallListeners() {&nbsp; &nbsp; &nbsp; &nbsp; JButton button = (JButton) getButtons()[0];&nbsp; &nbsp; &nbsp; &nbsp; button.removeMouseListener(m);&nbsp; &nbsp; &nbsp; &nbsp; super.uninstallListeners();&nbsp; &nbsp; }&nbsp; &nbsp; public static class MyMouseListener extends MouseAdapter{&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void mouseEntered(MouseEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JButton rollOver = (JButton)e.getSource();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (rollOver.isEnabled()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rollOver.setBackground(new Color(163, 184, 204));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rollOver.setForeground(Color.WHITE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void mouseExited(MouseEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JButton rollOver = (JButton)e.getSource();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (rollOver.isEnabled()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rollOver.setBackground(new Color(230, 230, 230));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rollOver.setForeground(Color.BLACK);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; }}在你的框架你的主类中,你可以添加此代码来加载 UIDefoult 中的类static{&nbsp; &nbsp;UIManager.put("OptionPaneUI", MyOptionPaneUI.getClass().getCanonicalName());}因为getButtons()[0],因为我在里面看到了这段代码BasicOptionPaneUIelse if (type == JOptionPane.OK_CANCEL_OPTION) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; defaultOptions = new ButtonFactory[2];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; defaultOptions[0] = new ButtonFactory(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UIManager.getString("OptionPane.okButtonText",l),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getMnemonic("OptionPane.okButtonMnemonic", l),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (Icon)DefaultLookup.get(optionPane, this,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "OptionPane.okIcon"), minimumWidth);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; defaultOptions[1] = new ButtonFactory(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UIManager.getString("OptionPane.cancelButtonText",l),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getMnemonic("OptionPane.cancelButtonMnemonic", l),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (Icon)DefaultLookup.get(optionPane, this,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "OptionPane.cancelIcon"), minimumWidth);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; defaultOptions = new ButtonFactory[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; defaultOptions[0] = new ButtonFactory(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UIManager.getString("OptionPane.okButtonText",l),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getMnemonic("OptionPane.okButtonMnemonic", l),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (Icon)DefaultLookup.get(optionPane, this,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "OptionPane.okIcon"), minimumWidth);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }方法内部protected Object[] getButtons()如果您想要效果鼠标悬停在按钮上,我正在研究这个库并提供了鼠标悬停的解决方案。如果您可以使用此常量对库内的 DefaultButton 进行个性化设置UIManager.put("Button[Default].background", new Color(163, 184, 204));UIManager.put("Button[Default].foreground", Color.WHITE);UIManager.put("Button[Default].mouseHoverColor", new Color(230, 230, 230));ps:这仅是您需要在项目中添加鼠标悬停时的信息
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java