我创建了一个Swing界面,当光标悬停在按钮上方时,我希望在该图标中出现JLabel
图标。图标将根据光标悬停在哪个按钮上而改变。
这是我目前正在使用的编码:
public void iconchange()
{
if(btnPlay.isRolloverEnabled() == true)
{
lblIcon.setIcon(new ImageIcon("Images/Play-icon.png"));
}
}
我知道上面的编码是错误的,那么如何实现上面提到的功能?
最佳答案
在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
可以这样使用...
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以获取更多详细信息