如何创建一个 JPanel 矩形?
我有图片作为 JLabel
,我想做的是当我单击 JLabel
时,会有另一个矩形形式的 Jlabel
会出现。
我尝试使用 paintComponent
或 paint
,不幸的是,它不适用于我的程序。所以我不得不认为除了 paintComponent
和/或 paint
之外还有其他方法。
目的是用名称标记图像的特定部分,就像 Facebook 中的那样。
I have picture as a JLabel
and what I want to do is when I click the JLabel
, there would be another Jlabel
in a form of rectangle would appear.
I tried using paintComponent
or paint
and unfortunately, it doesn't work for my program. So I have to think that there are other ways, other than paintComponent
and/or paint
.
The aim is tag to a certain part of an image with a name, like in Facebook.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哇。
根据 Pace 对问题的描述,您将必须执行以下操作(我认为):
图片必须直接绘制在 JPanel 上。这样,当有人单击图片时,您可以通过 JPanel 操作侦听器获取鼠标的 x 和 y 坐标。
然后,您将创建一个带有透明 JPanel 的 JDialog,该 JPanel 具有边框、名称文本框和“确定”按钮。 (不妨将所有内容放在一个对话框窗口中。)JDialog 将是可移动的,但您必须创建一个 JDialog 侦听器来跟踪左上角边缘或中心的 x 和 y 坐标。 JDialog 中的透明 JPanel。
JDialog JPanel 不会真正透明。您必须通过记下 JDialog JPanel 在图片 JPanel 上的位置,并将图像的一部分从图片 JPanel 复制到 JDialog JPanel 来创建透明的幻觉。
与让 JDialog 正常工作相比,其余的应该相当简单。
编辑添加:这是 JPanel 的扩展,它将直接在 JPanel 上绘制图片并处理鼠标按下事件。
Wow.
Based on Pace's description of the problem, you're going to have to do the following (I think):
The picture will have to be painted directly on a JPanel. This is so when someone clicks on the picture, you can get the mouse x and y coordinates through the JPanel action listener.
You will then create a JDialog with a transparent JPanel that has a border, a text box for the name, and an OK button together. (Might as well put everything together in one dialog window.) The JDialog will be movable, but you're going to have to create a JDialog listener that keeps track of the x and y coordinates of the top left edge or the center of the transparent JPanel in the JDialog.
The JDialog JPanel won't really be transparent. You'll have to create the illusion of transparency by noting the position of the JDialog JPanel on the picture JPanel, and copying the part of the image from the picture JPanel to the JDialog JPanel.
The rest should be rather straightforward, compared to getting the JDialog to work properly.
Edited to add: Here's an extension of JPanel that will draw a picture directly on the JPanel and process mouse pressed events.