如何让点击图片时出现JTextField?

发布于 2024-12-13 19:11:05 字数 224 浏览 5 评论 0原文

所以我正在尝试制作这个程序,允许用户使用 java 来标记照片(有点像 Facebook 标记)。我已经加载了图像,并在用户单击图像的某个区域时制作了鼠标侦听器。

当用户单击照片的特定区域时,如何使 JTextField 出现?

我认为 JTextField 在某种程度上可以是用户可以输入他/她的名字作为照片标签的框。

另外,您认为我应该将 JTextField 代码放在哪里?主要?

So I'm trying to make this program that allows the user to tag photos using java (kinda like Facebook tagging). I have already done loading the image, and making mouselistener when the user clicks an area of the image.

How do I make a JTextField appear when the user clicks a certain area of the photo?

I'm thinking that the JTextField can somewhat be the box where the user can enter his/her name as a tag for the photo.

Also, where do you think I should put the JTextField code? In main?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠檬 2024-12-20 19:11:05

您可以获得 X 和 Y 坐标(如 Daggeto 所说)。然后你可以使用 setVisible(true) 显示你的文本字段

You can get the X and Y co-ordinates (as said by Daggeto). And then you can show your text field with setVisible(true)

眼藏柔 2024-12-20 19:11:05

只需在 JTextField 对象上使用 setVisible() 函数,并在用户访问图像的某个部分时设置其值。

just use the setVisible() function on the JTextField object and set its value whenever the user a certain portion of the image.

捂风挽笑 2024-12-20 19:11:05

MouseEvent.getX()MouseEvent.getY() 返回事件相对于源组件的水平 x 和垂直 y 位置。

然后,如果您将图像区域描述为 x1,x2,y1,y2,您可以通过以下“if”检查该区域中的点击位置:

int x0 = MouseEvent.getX();
int y0 = MouseEvent.getY();

if(x0>x1 && x0<x2 && y0>y1 && y0<y2){
    JTextField.setVisible(true);
}

MouseEvent.getX() and MouseEvent.getY() returns the horizontal x ant vertical y position of the event relative to the source component.

Then if you your image area described as x1,x2,y1,y2 you can check is clicked position in this area by this 'if':

int x0 = MouseEvent.getX();
int y0 = MouseEvent.getY();

if(x0>x1 && x0<x2 && y0>y1 && y0<y2){
    JTextField.setVisible(true);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文