根据光标悬停在哪个按钮更改JLabel中的图像?

时间:2015-11-04 23:05:44

标签: java image swing icons jbutton

我创建了一个Swing界面,当光标悬停在按钮上时,我希望图标显示在JLabel中。图标将根据光标悬停在哪个按钮上而改变。

以下是我目前使用的编码:

public void iconchange()
    {
        if(btnPlay.isRolloverEnabled() == true)
        {
            lblIcon.setIcon(new ImageIcon("Images/Play-icon.png"));
        }
    }

我知道上面的编码是错误的,那么我该怎样才能实现上面提到的功能呢?

2 个答案:

答案 0 :(得分:2)

将鼠标滑块添加到btnPlay,然后使用鼠标退出并输入方法

btnPlay.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseEntered(java.awt.event.MouseEvent evt) {
        lblIcon.setIcon(new ImageIcon("Images/Play-icon.png"));
    }

    public void mouseExited(java.awt.event.MouseEvent evt) {
        //I assume you want to remove or change the icon afterward
    }
});

答案 1 :(得分:2)

使用ChangeListener JButton上的ButtonModel来监控对模型状态的更改,例如,根据模型的状态更新UI。

public class ChangeHandler implements ChangeListener {

    private JLabel label;
    private Icon armedIcon;

    public ChangeHandler(JLabel label, Icon armedIcon) {
        this.armedIcon = armedIcon;
        this.label = label;
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        ButtonModel model = (ButtonModel) e.getSource();
        if (model.isRollover()) {
            label.setIcon(armedIcon);
        } else {
            label.setIcon(null);
        }
    }

}

这将更新提供的JLabel,并在其附加的icon位于icon时将其更改为指定的ButtonModel rollOver

这可以用这样的东西......

Rollover

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class RollOver {

    public static void main(String[] args) {
        new RollOver();
    }

    public RollOver() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel label;

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            // I'm doing this, because I don't have blank icon of 128x128 and my
            // icons are both 128x128
            label = new JLabel() {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(128, 128);
                }
            };
            add(label, gbc);
            try {
                JButton btn = new JButton("Bunnies");
                btn.getModel().addChangeListener(new ChangeHandler(label, new ImageIcon(ImageIO.read(getClass().getResource("/Bunny.png")))));
                add(btn);
                btn = new JButton("Zomnies");
                btn.getModel().addChangeListener(new ChangeHandler(label, new ImageIcon(ImageIO.read(getClass().getResource("/Zombi.png")))));
                add(btn);
            } catch (IOException exp) {
                exp.printStackTrace();
            }
        }

        public class ChangeHandler implements ChangeListener {

            private JLabel label;
            private Icon armedIcon;

            public ChangeHandler(JLabel label, Icon armedIcon) {
                this.armedIcon = armedIcon;
                this.label = label;
            }

            @Override
            public void stateChanged(ChangeEvent e) {
                ButtonModel model = (ButtonModel) e.getSource();
                if (model.isRollover()) {
                    label.setIcon(armedIcon);
                } else {
                    label.setIcon(null);
                }
            }

        }

    }

}

仔细查看How to Use Buttons, Check Boxes, and Radio Buttons了解更多详情