JLabel中获取鼠标单击图像时的X和Y位置
我使用 ImageIcon 功能在 JLabel 内显示图像。
JLabel 包含在 JPanel 内。
单击图像时,我想获取鼠标相对于图像的位置。
我试图通过将鼠标侦听器添加到包含以下内容的 JLabel 来解决此问题 图像。
显然,即使我将 JLabel 的大小设置为图像的宽度和高度,JLabel 似乎也会自动拉伸到 JPanel 的大小。
因此,当下面的代码运行时,如果图像的大小小于 JPanel,获取 X 和 Y 位置将返回不需要的值。
有没有一种方法可以专门限制 JLabel 的大小,以便获取鼠标的 X 位置和 Y 位置可以正常工作?
或者更好的方法来获取鼠标相对于图像的位置? (这样会更好)
ImageIcon icon = new ImageIcon(image);
JLabel imageLabel = new JLabel(icon);
imageLabel.setSize(image.getWidth(null), image.getHeight(null));
imageLabel.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(imageLabel, e.getX()+" "+e.getY());
}
});
imageLabel.validate();
panel.add(imageLabel);
panel.validate();
I have a Image displayed inside JLabel using ImageIcon feature.
The JLabel is contained inside a JPanel.
When image is clicked, I want to get the mouse position relative to the image.
I am trying to solve this problem by adding mouse listener to the JLabel which contains the
image.
Apparently, even if I set the size of JLabel with the width and height of the image, it seems that JLabel automatically stretches out to the size of JPanel.
So when below code runs, if the size of image is smaller than the JPanel, getting X and Y position returns the unwanted value.
Is there a way that I can specifically limit the size of the JLabel so that getting X position and Y position of the mouse will work fine?
Or better way to get a mouse position relative to the image? (which would be better)
ImageIcon icon = new ImageIcon(image);
JLabel imageLabel = new JLabel(icon);
imageLabel.setSize(image.getWidth(null), image.getHeight(null));
imageLabel.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(imageLabel, e.getX()+" "+e.getY());
}
});
imageLabel.validate();
panel.add(imageLabel);
panel.validate();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这都是关于布局的。
请参阅上图的示例。
看来这个用例需要
GridBagLayout
或BoxLayout
。It is all about the layout.
See this example for the above image.
It would seem this use-case requires a
GridBagLayout
orBoxLayout
.您可能会遇到问题,因为布局管理器正在控制标签的大小,而不是 setSize() 调用。
我可能会创建一个 JPanel 派生类(而不是 JLabel),它通过绘制图像来实现 PaintComponent。然后我将重写 JPanel 的 getPreferredSize() 和 getMaximumSize() 以返回图像的尺寸。
使用标签会带来麻烦,因为会有填充、偏移等。
Your probably seeing problems because the Layout manager is controlling the size of your label, not the setSize() call.
I'd probably create my a JPanel derived class (instead of a JLabel) that implements paintComponent by drawing the image. Then i'd override getPreferredSize() and getMaximumSize() of the JPanel to return the dimensions of the image.
Using the label will cause troubles because there's going to be padding, offsets and such.
看起来 JLabel 会自动拉伸到 JPanel 的大小。
问题就在这里,
如果只有一个
JLabel
,那么是否有多个带有 Image/IconImage 的 JLabel 以及 查找 GridLayout
编辑
应该更好地在所有情况下(对于真实图片)
,然后再执行任何操作1)您必须确定图片比例 3:2、4:3 16:10 ...,确定 CropRatio 然后您必须:
setPreferredSize(new Dimension(x/cropRatio, y/cropRatio)) for JLabel
通过使用 paintComponent g2d.fillRect(0, 0, x/cropRatio, y/cropRatio);
2)将JLabel放入JScrollPane,但随后会被按住原始像素数
it seems that JLabel automatically stretches out to the size of JPanel.
problem is here
if is there only one
JLabel
, thenif is there more that one JLabel with Image/IconImage and should be better look for GridLayout
EDIT
in all cases (for reall picture) before anything
1) you have to determine picture ratio 3:2, 4:3 16:10 ..., determine CropRatio then you have to :
setPreferredSize(new Dimension(x/cropRatio, y/cropRatio)) for JLabel
by using paintComponent g2d.fillRect(0, 0, x/cropRatio, y/cropRatio);
2) put JLabel to the JScrollPane, but then will be hold original amout of pixels
您可能会遇到问题,因为布局管理器正在控制标签的大小,而不是 setSize() 调用。
我可能会创建一个 JPanel 派生类(而不是 JLabel),它通过绘制图像来实现 PaintComponent。然后我将重写 JPanel 的 getPreferredSize() 和 getMaximumSize() 以返回图像的尺寸。
使用标签会带来麻烦,因为会有填充物等。
但最终,导致我们的标签扩展的是布局管理器。最喜欢做你想做的事情的最简单方法是通过选择一个将组件保持在其首选大小的布局管理器来确保布局管理器不会扩展标签。
Your probably seeing problems because the Layout manager is controlling the size of your label, not the setSize() call.
I'd probably create my a JPanel derived class (instead of a JLabel) that implements paintComponent by drawing the image. Then i'd override getPreferredSize() and getMaximumSize() of the JPanel to return the dimensions of the image.
Using the label will cause troubles because there's going to be padding and such.
But in the end, what's causing our label to expand is the layout manager. Most like the easiest way to do what you want is to make sure your layout manager isn't expanding the label, by picking a layout manager that keeps the component at it's preferred size.