Java 在鼠标悬停时重新绘制组件。

标签 java swing user-interface mouselistener jmenu

我正在尝试制作我的第一个正确定制的 GUI,但我很难更改为组件绘制的图像。基本上,对于我的 exitButton(一个 JMenu),我覆盖了 paint 方法,然后添加了一个鼠标监听器,但我不确定如何在鼠标进入方法和鼠标退出方法中的 mouseListener 界面内重新绘制图像。本质上,我正在寻找一种重新绘制图像的方法,但我不知道我能做什么。任何帮助将不胜感激。

这是相关的代码片段:

exitBtn = new JMenu(){
       @Override
       protected void paintComponent(Graphics g) {
        super.paintComponent(g);
           ImageIcon exitBtnImg = new ImageIcon("src/images/userInterface/exitBtn.gif");
           g.drawImage(exitBtnImg.getImage(), 0, 5, null);
      }
  };
  exitBtn.setOpaque(false);
  exitBtn.setEnabled(false);
  exitBtn.setPreferredSize(new Dimension(43, 18));
  exitBtn.addMouseListener(new MouseListener() {
        @Override
        public void mousePressed(MouseEvent me) {
        }
        @Override
        public void mouseClicked(MouseEvent me) {
            System.exit(0);
        }
        @Override
        public void mouseEntered(MouseEvent me) {
            //ImageIcon exitBtnImg = new ImageIcon("src/images/exitBtn_hover.gif"); //The ImageIcon for the Image I want to use
            System.out.println("mouse entered");

        }
        @Override
        public void mouseExited(MouseEvent me) {
            // ImageIcon exitBtnImg = new ImageIcon("src/images/exitBtn.gif"); 

System.out.println("鼠标退出");//原始图像的图像图标 } @覆盖 public void mouseReleased(MouseEvent me) { } });

最佳答案

I am attempting to make my first properly customised GUI

您应该先阅读 Swing 教程。我不太确定您要做什么,但您的方法肯定是错误的。

您可以从 How to Use Menus 开始它展示了如何使用 ActionListener 来处理鼠标点击。鼠标点击通常在菜单项上处理,而不是菜单。您通常会有类似"file"菜单的内容,其中包含“退出”菜单项。

然后我还会查看 JMenu API 的各种方法,这些方法允许您在鼠标悬停或选择菜单时更改图标。也许 setRolloverEnabled()、setRolloverIcon() 就是您要找的。

如果您仍然有问题,请发布 SSCCE这说明了问题。

更新:

如 Hovercraft 所述,滚动支持不适用于菜单或菜单项。有两个问题。首先,这些组件使用不同的 MouseListener。监听器不监听 mouseEntered 和 mouseExited 事件。第二个问题是两个组件的 UI 已经自定义,自定义 Icon 绘制代码没有考虑按钮的滚动状态。

添加 MouseListener 很容易。自定义 UI(这是正确的解决方案)以正确支持滚动更复杂。

对于似乎有效的简单 hack,我只是更新 MouseListener 中的图标,而不是让 UI 确定要绘制哪个图标。我建议您忘记此要求并使用不会更改菜单和菜单项图标的普通 UI。使用以下内容需要您自担风险:

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

public class ButtonRollover extends JFrame
{
    Icon normal;
    Icon rollover;
    Icon selected;

    public ButtonRollover()
    {
        MouseListener ml = new RolloverButtonListener();

        normal = new ColorIcon(Color.GREEN, 10, 10);
        rollover = new ColorIcon(Color.RED, 10, 10);
        selected = new ColorIcon(Color.BLUE, 10, 10);

        setLayout( new FlowLayout() );

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar( menuBar );

        JMenu menu = (JMenu)createButton(new JMenu(), "Menu");
        menu.addMouseListener( ml );
        menuBar.add( menu );

        JMenuItem menuItem = (JMenuItem)createButton(new JMenuItem(), "MenuItem");
        menuItem.addMouseListener( ml );
        menu.add( menuItem );

        JButton button = (JButton)createButton(new JButton(), "Button");
        add( button );

        JCheckBox checkBox = (JCheckBox)createButton(new JCheckBox(), "CheckBox");
        add( checkBox );

        JRadioButton radioButton = (JRadioButton)createButton(new JRadioButton(), "RadioButton");
        add( radioButton );
    }


    public AbstractButton createButton(AbstractButton button, String text)
    {
        button.setText( text );
        button.setIcon( normal );
        button.setSelectedIcon( selected );
        button.setRolloverIcon( rollover );
        button.setRolloverSelectedIcon( rollover );

        System.out.println( text );
        MouseListener[] mls = button.getMouseListeners();

        for (MouseListener ml: mls)
        {
            System.out.println( "\t" + ml);
        }

        return button;
    }

    class RolloverButtonListener extends MouseAdapter
    {
        private Icon normal;

        public void mouseEntered(MouseEvent e)
        {
            AbstractButton b = (AbstractButton) e.getSource();
            ButtonModel model = b.getModel();

            if (b.isRolloverEnabled() && !SwingUtilities.isLeftMouseButton(e))
            {
                normal = b.getIcon();
                b.setIcon(b.getRolloverIcon());
                model.setRollover(true);
            }
        }

        public void mouseExited(MouseEvent e)
        {
            AbstractButton b = (AbstractButton) e.getSource();
            ButtonModel model = b.getModel();

            if(b.isRolloverEnabled())
            {
                b.setIcon( normal );
                model.setRollover(false);
            }
        };

    }

    public class ColorIcon implements Icon
    {
        private Color color;
        private int width;
        private int height;

        public ColorIcon(Color color, int width, int height)
        {
            this.color = color;
            this.width = width;
            this.height = height;
        }

        public int getIconWidth()
        {
            return width;
        }

        public int getIconHeight()
        {
            return height;
        }

        public void paintIcon(Component c, Graphics g, int x, int y)
        {
            g.setColor(color);
            g.fillRect(x, y, width, height);
        }
    }

    public static void main(String[] args)
    {
        try
        {
//          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) { }

        ButtonRollover frame = new ButtonRollover();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.setSize(400, 200);
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}

关于Java 在鼠标悬停时重新绘制组件。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6404344/

相关文章:

java - iso 日期未成功转换为 12 小时格式的时间

Java 外观 (L&F)

java - java中的配置文件

java - 用于 C++ 应用程序的 Java GUI - 这是一个好的设计吗?

c# - 在 XNA 中将 HTML/CSS 用于 UI?

python - 您使用哪种 IDE(如果有)构建 python GUI 项目?

java - BorderLayout的使用

java - 将 int 文字分配给 java 中的 long 包装类

java - Java中的泛型+可选参数不能组合

java - 什么是系统负载?