【问题标题】:Accessing JOption Pane's button to add MouseListener to them访问 JOption 窗格的按钮以将 MouseListener 添加到它们
【发布时间】:2011-04-16 21:34:08
【问题描述】:

是的,我认为通过在组件上使用.getComponents() 会相对简单,这将返回JOptionPaneJPanel,然后他们通过再次使用该方法和JPanel 检索JButtons但是我面临困难。

我想在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);
    }
}

【问题讨论】:

  • @Andrew:您应该将此作为答案发布,而不是评论。
  • 我将如何使用 JDialog 执行此操作?我不是要代码,只是一些有用的使用方法或如何做的概念。
  • @HFOE 完成。 @unleashed 请确保在以后的帖子中使用代码格式。要使用它,请选择代码示例并单击{} 按钮。检查代码是否按预期显示在消息发布表单下方的预览区域中。
  • 虽然你不是要代码,但我加了一些。有时我发现编写示例代码比描述过程更容易和更快。 ;)

标签: java swing joptionpane


【解决方案1】:

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

使用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 添加该功能。

【讨论】:

  • 感谢 Andrew 提供的简单示例,现在可以实现我所需要的。
  • @unleashed,如果这个答案有帮助,那么你应该接受这个答案。
  • +1 非常好的答案。 @unleashed 继续接受它。您不会为您的问题找到更好的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-10
  • 2019-05-17
相关资源
最近更新 更多