I have a code with grid layout and two JLabel images. I wan't some text to appear everytime I roll over each image. I am familiar on how to do this when the image is not a JLabel, but have searched all over the web to find how to do this while it is an unnamed JLabel. The two images I wan't to have, with separate roll over messages are:
我有一个网格布局和两个JLabel图像的代码。每次我翻看每张图片时,我都不会出现一些文字。我很熟悉如何在图像不是JLabel的情况下执行此操作,但是在整个网络上搜索以查找如何在未命名的JLabel中执行此操作。我不想拥有的两个图像,以及单独的翻转消息:
ImageIcon(getClass().getResource("giraffe.png"));
Icon windows = new ImageIcon(getClass().getResource("windows.png"));
Here is my code:
这是我的代码:
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.
*/
}
Thank you so much for the time you are taking for reading this, I really appreciate the effort you are putting into helping a fellow programmer!
非常感谢您花时间阅读本文,我非常感谢您为帮助其他程序员而付出的努力!
2 个解决方案
#1
4
Start by having a look at How to Write a Mouse Listener.
首先看看如何编写鼠标侦听器。
Basically, you want to attach a MouseListener
to each label and monitor the mouseEntered
and mouseExited
events, updating the label state as per your requirements
基本上,您希望将MouseListener附加到每个标签并监视mouseEntered和mouseExited事件,根据您的要求更新标签状态
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
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 {
public TestPane() {
try {
JLabel left = new JLabel(new ImageIcon(ImageIO.read(...))));
left.setVerticalTextPosition(JLabel.BOTTOM);
left.setHorizontalTextPosition(JLabel.CENTER);
left.setHorizontalAlignment(JLabel.CENTER);
left.setVerticalAlignment(JLabel.CENTER);
left.setText(" ");
JLabel right = new JLabel(new ImageIcon(ImageIO.read(...))));
right.setVerticalTextPosition(JLabel.BOTTOM);
right.setHorizontalTextPosition(JLabel.CENTER);
right.setHorizontalAlignment(JLabel.CENTER);
right.setVerticalAlignment(JLabel.CENTER);
right.setText(" ");
setLayout(new GridLayout(1, 2));
add(left);
add(right);
left.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
left.setText("I'm on the left");
}
@Override
public void mouseExited(MouseEvent e) {
left.setText(" ");
}
});
right.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
right.setText("I'm on the right");
}
@Override
public void mouseExited(MouseEvent e) {
right.setText(" ");
}
});
} catch (IOException exp) {
exp.printStackTrace();
}
}
}
}
You might also want to have a look at Reading/Loading an Image
您可能还想看一下阅读/加载图像
#2
3
You can also create a generic listener by doing something like:
您还可以通过执行以下操作来创建通用侦听器:
ImageIcon giraffe = new ImageIcon(...);
giraffe.setDescription("Giraffe Description");
Then your listener might look something like:
然后你的听众可能看起来像:
MouseListener ml = new MouseAdapter()
{
@Override
public void mouseEntered(MouseEvent e)
{
JLabel label = (JLabel)e.getSource();
ImageIcon icon = (ImageIcon)label;
label.setText( icon.getDescription() );
}
@Override
public void mouseExited(MouseEvent e)
{
JLabel label = (JLabel)e.getSource();
label.setText(" ");
}
}
And you add the listener to the label:
然后将监听器添加到标签:
JLabel giraffeLabel = new JLabel( giraffe );
giraffe.addMouseListener( ml );
The same listener can be used for your other JLabel because the event code knows with component generated the event.
同一个侦听器可以用于其他JLabel,因为事件代码知道组件生成了事件。
#1
4
Start by having a look at How to Write a Mouse Listener.
首先看看如何编写鼠标侦听器。
Basically, you want to attach a MouseListener
to each label and monitor the mouseEntered
and mouseExited
events, updating the label state as per your requirements
基本上,您希望将MouseListener附加到每个标签并监视mouseEntered和mouseExited事件,根据您的要求更新标签状态
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
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 {
public TestPane() {
try {
JLabel left = new JLabel(new ImageIcon(ImageIO.read(...))));
left.setVerticalTextPosition(JLabel.BOTTOM);
left.setHorizontalTextPosition(JLabel.CENTER);
left.setHorizontalAlignment(JLabel.CENTER);
left.setVerticalAlignment(JLabel.CENTER);
left.setText(" ");
JLabel right = new JLabel(new ImageIcon(ImageIO.read(...))));
right.setVerticalTextPosition(JLabel.BOTTOM);
right.setHorizontalTextPosition(JLabel.CENTER);
right.setHorizontalAlignment(JLabel.CENTER);
right.setVerticalAlignment(JLabel.CENTER);
right.setText(" ");
setLayout(new GridLayout(1, 2));
add(left);
add(right);
left.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
left.setText("I'm on the left");
}
@Override
public void mouseExited(MouseEvent e) {
left.setText(" ");
}
});
right.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
right.setText("I'm on the right");
}
@Override
public void mouseExited(MouseEvent e) {
right.setText(" ");
}
});
} catch (IOException exp) {
exp.printStackTrace();
}
}
}
}
You might also want to have a look at Reading/Loading an Image
您可能还想看一下阅读/加载图像
#2
3
You can also create a generic listener by doing something like:
您还可以通过执行以下操作来创建通用侦听器:
ImageIcon giraffe = new ImageIcon(...);
giraffe.setDescription("Giraffe Description");
Then your listener might look something like:
然后你的听众可能看起来像:
MouseListener ml = new MouseAdapter()
{
@Override
public void mouseEntered(MouseEvent e)
{
JLabel label = (JLabel)e.getSource();
ImageIcon icon = (ImageIcon)label;
label.setText( icon.getDescription() );
}
@Override
public void mouseExited(MouseEvent e)
{
JLabel label = (JLabel)e.getSource();
label.setText(" ");
}
}
And you add the listener to the label:
然后将监听器添加到标签:
JLabel giraffeLabel = new JLabel( giraffe );
giraffe.addMouseListener( ml );
The same listener can be used for your other JLabel because the event code knows with component generated the event.
同一个侦听器可以用于其他JLabel,因为事件代码知道组件生成了事件。