【发布时间】:2015-11-13 14:45:23
【问题描述】:
我有一个带有网格布局和两个 JLabel 图像的代码。每次滚动每张图像时,我都不希望出现一些文本。当图像不是 JLabel 时,我很熟悉如何执行此操作,但是已经在整个网络上搜索以找到如何在它是未命名的 JLabel 时执行此操作。我不想拥有的两张带有单独滚动消息的图像是:
ImageIcon(getClass().getResource("giraffe.png"));
Icon windows = new ImageIcon(getClass().getResource("windows.png"));
这是我的代码:
public class giraffe implements ActionListener{
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(null,
"Press ok, and see the amazing giraffe outside a window!");
JDialog giraffewindow = new JDialog();
Icon giraffe = new ImageIcon(getClass().getResource("giraffe.png"));
Icon windows = new ImageIcon(getClass().getResource("windows.png"));
giraffewindow.setLayout(new GridLayout(1, 2, 0, 0));
giraffewindow.add(new JLabel (windows));
giraffewindow.add(new JLabel (giraffe));
giraffewindow.pack();
giraffewindow.setTitle("GIRAFFE!");
giraffewindow.setVisible(true);
giraffewindow.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
/*
* I want to have a rollover on EACH IMAGE so that when they rollover the image you see different text.
*/
}
非常感谢您花时间阅读本文,非常感谢您为帮助其他程序员所做的努力!
【问题讨论】:
-
我猜你需要
MouseListener来跟踪mouseEntered和mouseExited事件并相应地更改标签的文本。从How to Write a Mouse Listener开始 -
I am familiar on how to do this when the image is not a JLabel- 你会怎么做? -
@MadProgrammer 他不是必须创建自己的扩展 JLabel 的类,然后从那里实现 MouseListener 吗?
-
我会使用 .setRolloverIcon 之类的东西...虽然那将是一个翻转图像,所以我会使用 .setToolTipText 之类的东西。虽然我在这种情况下完全迷失了。
-
@MadProgrammer 这将是我解决他问题的方式,不是最好的吗?