如果鼠标指针悬停在 Windows XP 中的开始按钮上,它会突出显示。我想在 Java 中做同样的事情。谁能帮我吗?
最佳答案
一个可能的解决方案是为按钮创建一个图标,以便在鼠标悬停时显示,然后通过其 setRolloverIcon 将其添加到按钮。鼠标的模型将执行显示此图标所需的所有操作。
另一个解决方案是将 ChangeListener 添加到按钮的模型中。在监听器中,如果 isRollover() 返回 true,则更改显示。
E.G.第一种技术。
import javax.swing.*;
import java.net.URL;
import java.awt.Image;
import javax.imageio.ImageIO;
class ButtonRollover {
public static void main(String[] args) throws Exception {
URL imageUrl2 = new URL("http://pscode.org/media/stromlo2.jpg");
URL imageUrl1 = new URL("http://pscode.org/media/stromlo1.jpg");
final Image image2 = ImageIO.read(imageUrl2);
final Image image1 = ImageIO.read(imageUrl1);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JButton button = new JButton("Hover Me!");
button.setIcon(new ImageIcon(image2));
button.setRolloverIcon(new ImageIcon(image1));
JOptionPane.showMessageDialog(null, button);
}
});
}
}
关于java - 在Java中当鼠标指针悬停在按钮上时如何突出显示按钮(就像XP中的“开始”按钮突出显示)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6242533/