java 向图像添加动作侦听器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5839882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
adding action listener to an image
提问by user650679
I insert an image to a JPanel
. I write this code.
我将图像插入到JPanel
. 我写了这段代码。
public void paint(Graphics g)
{
img1=getToolkit().getImage("/Users/Boaz/Desktop/Piece.png");
g.drawImage(img1, 200, 200,null);
}
I want to add an action listener to that picture, but it does not have an addActionListener()
method. How can I do that without putting the image in a button or label?
我想为该图片添加一个动作侦听器,但它没有addActionListener()
方法。如果不将图像放在按钮或标签中,我该怎么做?
回答by coobird
There are a few options.
有几个选项。
Use a MouseListener
directly in the JPanel
使用MouseListener
在直接JPanel
A simple but dirty way would be to add an MouseListener
directly to the JPanel
in which you overrode the paintComponent
method, and implement a mouseClicked
method which checks if the region where the image exists has been clicked.
一个简单但肮脏的方法是MouseListener
直接添加一个JPanel
你覆盖该paintComponent
方法的方法,并实现一个mouseClicked
方法来检查图像所在的区域是否已被点击。
An example would be something along the line of:
一个例子是:
class ImageShowingPanel extends JPanel {
// The image to display
private Image img;
// The MouseListener that handles the click, etc.
private MouseListener listener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// Do what should be done when the image is clicked.
// You'll need to implement some checks to see that the region where
// the click occurred is within the bounds of the `img`
}
}
// Instantiate the panel and perform initialization
ImageShowingPanel() {
addMouseListener(listener);
img = ... // Load the image.
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}
}
Note: An ActionListener
can't be added to a JPanel
, as a JPanel
itself does not lend itself to create what is considered to be "actions".
注意: AnActionListener
不能添加到 a 中JPanel
,因为 aJPanel
本身不适合创建被认为是“动作”的东西。
Create a JComponent
to display the image, and add a MouseListener
创建一个JComponent
显示图像,并添加一个MouseListener
A better way would be to make a new subclass of JComponent
whose sole purpose is to display the image. The JComponent
should size itself to the size of the image, so that a click to any part of the JComponent
could be considered a click on the image. Again, create a MouseListener
in the JComponent
in order to capture the click.
更好的方法是创建一个新的子类,JComponent
其唯一目的是显示图像。本JComponent
应大小本身对图片的大小,使一点击的任何部分JComponent
可以考虑在图像上点击。再次,建立MouseListener
在JComponent
以捕获点击。
class ImageShowingComponent extends JComponent {
// The image to display
private Image img;
// The MouseListener that handles the click, etc.
private MouseListener listener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// Do what should be done when the image is clicked.
}
}
// Instantiate the panel and perform initialization
ImageShowingComponent() {
addMouseListener(listener);
img = ... // Load the image.
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}
// This method override will tell the LayoutManager how large this component
// should be. We'll want to make this component the same size as the `img`.
public Dimension getPreferredSize() {
return new Dimension(img.getWidth(), img.getHeight());
}
}
回答by handuel
The easiest way is to put the image into a JLabel. When you use the program, it appears to just be the image, you can't tell its in a JLabel. Then just add a MouseListener to the JLabel.
最简单的方法是将图像放入 JLabel 中。当您使用该程序时,它似乎只是图像,您无法在 JLabel 中分辨它。然后只需向 JLabel 添加一个 MouseListener 即可。