访问 JOption Pane 的按钮以向其添加 MouseListener

发布于 2024-11-01 23:48:44 字数 1515 浏览 0 评论 0原文

是的,我认为通过在组件上使用 .getComponents() 会相对简单,它将返回 JOptionPaneJPanel 并检索它们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 JButtons 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 技术交流群。

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

发布评论

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

评论(1

誰認得朕 2024-11-08 23:48:44

有没有更简单的方法来实现这一点?

使用JDialog。长期的经验告诉我,虽然 JOptionPane 是一个强大的&方便的组件,一旦需要自定义它,您最好只使用 JDialog


代码

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

class CustomDialog {

    public static void main(String[] args) {

        Runnable r = new Runnable() {
            public void run() {
                JFrame frame = new JFrame("Show Custom Dialog");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(400,400);
                frame.setLocationRelativeTo(null);

                final JDialog dialog = new JDialog(frame, "Dialog", true);

                JPanel mainGui = new JPanel(new BorderLayout());
                mainGui.setBorder(new EmptyBorder(20,20,20,20));
                mainGui.add( new JLabel("Contents go here"), BorderLayout.CENTER );

                JPanel buttonPanel = new JPanel(new FlowLayout());
                mainGui.add(buttonPanel, BorderLayout.SOUTH);

                JButton close = new JButton("Close");
                close.addActionListener( new ActionListener(){
                    public void actionPerformed(ActionEvent ae) {
                        dialog.setVisible(false);
                    }
                } );

                buttonPanel.add(close);

                frame.setVisible(true);

                dialog.setContentPane(mainGui);
                dialog.pack();
                dialog.setLocationRelativeTo(frame);
                dialog.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

屏幕截图

在此处输入图像描述


请注意,此示例尚未将所有功能内置到 中JOptionPane

例如,如果 JOptionPane 打开并且用户按转义键,则该对话框将被关闭。您可以使用 KeyListenerActionMap 添加该功能。

Is there a simpler way of achieving this?

Use a JDialog. Long experience tells me that while a JOptionPane is a powerful & handy component, once it comes to customizing it, you are better off simply using a JDialog.


Code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

class CustomDialog {

    public static void main(String[] args) {

        Runnable r = new Runnable() {
            public void run() {
                JFrame frame = new JFrame("Show Custom Dialog");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(400,400);
                frame.setLocationRelativeTo(null);

                final JDialog dialog = new JDialog(frame, "Dialog", true);

                JPanel mainGui = new JPanel(new BorderLayout());
                mainGui.setBorder(new EmptyBorder(20,20,20,20));
                mainGui.add( new JLabel("Contents go here"), BorderLayout.CENTER );

                JPanel buttonPanel = new JPanel(new FlowLayout());
                mainGui.add(buttonPanel, BorderLayout.SOUTH);

                JButton close = new JButton("Close");
                close.addActionListener( new ActionListener(){
                    public void actionPerformed(ActionEvent ae) {
                        dialog.setVisible(false);
                    }
                } );

                buttonPanel.add(close);

                frame.setVisible(true);

                dialog.setContentPane(mainGui);
                dialog.pack();
                dialog.setLocationRelativeTo(frame);
                dialog.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

Screen Shot

enter image description here


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 a KeyListener or ActionMap.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文