gpt4 book ai didi

java - JLabel 上的翻转由网格布局中的图像组成?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:22:30 27 4
gpt4 key购买 nike

我有一个带有网格布局的代码和两个 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.
*/

}

非常感谢您花时间阅读本文,我非常感谢您为帮助其他程序员所做的努力!

最佳答案

先看看 How to Write a Mouse Listener .

基本上,您想要将 MouseListener 附加到每个标签并监视 mouseEnteredmouseExited 事件,根据您的更新标签状态要求

LabelText

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();
}
}

}

}

您可能还想看看 Reading/Loading an Image

关于java - JLabel 上的翻转由网格布局中的图像组成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32110101/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com