"取消侧翻"显示JOptionPane时的JButton [英] "Un-rollover" a JButton when a JOptionPane is displayed

查看:161
本文介绍了"取消侧翻"显示JOptionPane时的JButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到需要在点击JButton后显示JOptionPane的情况。 JButton有一个默认图标和一个翻转图标(显示鼠标滚过按钮的时间)。但是,单击按钮并显示JOptionPane后,翻转图标不会更改回原始图标,并继续保持此状态,直到用户在选择适当的JOptionPane选项后将鼠标带回JButton的框架。如果单击JButton并显示JOptionPane,我将如何取消翻转它?

I have a situation where I need to display a JOptionPane after clicking on a JButton. The JButton has a default icon, and a rollover icon (which displays when, well, the mouse rolls-over the button). However, once the button is clicked and a JOptionPane appears, the rollover icon does not change back to the original, and continues to remain so until the user brings the mouse back to the JButton's frame after selecting an appropriate JOptionPane choice. How would I "un-rollover" the JButton when it is clicked and the JOptionPane is displayed?

TL; DR :即使单击并显示JOptionPanel,JButton也会显示翻转图标。我不喜欢。

TL;DR: JButton displays rollover icon even when being clicked and JOptionPanel is displayed. Me no likey.

这是 SSCCE

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;


public class ButtonUnrollover {

  public static void main(String[] args) {
    JFrame f = new JFrame();
    final JPanel p = new JPanel();
    JButton b = new JButton();
    b.setIcon(UIManager.getIcon("OptionPane.informationIcon"));
    b.setRolloverIcon(UIManager.getIcon("OptionPane.errorIcon"));
//    b.setSelectedIcon(UIManager.getIcon("OptionPane.informationIcon"));
//    b.setRolloverSelectedIcon(UIManager.getIcon("OptionPane.informationIcon"));
//    b.setPressedIcon(UIManager.getIcon("OptionPane.informationIcon"));
    p.add(b);
    b.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {
        JOptionPane jOP = new JOptionPane("Dummy message");
        JDialog dialog = jOP.createDialog(p, null);
        dialog.setVisible(true);
      }
    });

    f.add(p);
    f.pack();
    f.setVisible(true);
  }

}

注意:我找到了几个类似的问题。但是,这个问题并不重复,因为这些问题属于与此问题略有不同的问题(例如按钮保持按下,而不是翻转)。其中一些问题(实际上我能找到的所有问题)都是:

NB: I have found several similar questions to this one. However, this question is not a duplicate because those questions pertain to an issue slightly different from this one (such as the button staying pressed, not rolled-over). A few of these questions (well, actually all of them I could find) are:

  • JButton stays pressed when focus stolen by JOptionPane
  • JButton stays pressed after a JOptionPane is displayed
  • JButton "stay pressed" after click in Java Applet

推荐答案

翻转状态由 ButtonModel 。您可以通过模型的 setRollover(布尔b)方法重置翻转标志,该方法将图标设置回非翻转状态图标。在您的示例中实现 ActionListener

The rollover state is managed by the ButtonModel. You can reset the rollover flag via the model's setRollover(boolean b) method, which will set the Icon back to the non-rollover state Icon. Implemented in your example ActionListener:

b.addActionListener(new ActionListener() {

  @Override
  public void actionPerformed(ActionEvent e) {
    b.getModel().setRollover(false);//reset the rollover flag
    JOptionPane jOP = new JOptionPane("Dummy message");
    JDialog dialog = jOP.createDialog(p, null);
    dialog.setVisible(true);
  }
});

您可能还希望检查鼠标是否仍位于 JButton上方关闭对话框以重置翻转标志(如有必要) - 您可以通过 MouseInfo ,通过转换从 MouseInfo检索到的屏幕坐标来检查JButton是否包含该点。使用转换为组件坐标= nofollow的> SwingUtilities.convertPointFromScreen

You might also wish to check if the Mouse is still located over the JButton after the dialog is closed to reset the rollover flag (if necessary) - you can do so via MouseInfo, checking if the JButton contains the point by converting the Screen coordinates retrieved from MouseInfo.getPointerInfo().getLocation() to component coordinates using SwingUtilities.convertPointFromScreen.

这篇关于"取消侧翻"显示JOptionPane时的JButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆