访问JOption窗格的按钮以向其中添加MouseListener

|| 是的,我认为在Component上使用ѭ0会比较简单,它将返回
JOptionPane
JPanel
,然后他们再次使用that1ѭ使用该方法检索method3ѭ,但是我遇到了困难。 我想在
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);
    }
}
    
已邀请:
  有没有更简单的方法来实现这一目标? 用a7ѭ。长期的经验告诉我,虽然
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
,并且用户按下退出键,则对话框将消失。您可以使用
KeyListener
ActionMap
添加该功能。     

要回复问题请先登录注册