简体   繁体   中英

How to make a JTextField appear when clicking an image?

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?

You can get the X and Y co-ordinates (as said by Daggeto). And then you can show your text field with 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);
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM