简体   繁体   中英

How to move a JTextField or JButton to the left

Lets say you wan't get rid of a button if you just setVisible to false it will simply disappear, but it doesn't look to good, so i figured that it would be cool if the button/textfield would swiftly move to the left and when it hits the corner it would disappear. How could i do that?

If the componenet is in a JFrame or JPanel you can simply remove it with the remove method:

frame.remove(button1);
panel.remove(button1);
repaint();

Or get the content Pane and use the remove method.

Else if you want to move it out of the window to have that "disappearing" effect, as far for my short experience in swing is that you might will need to set the layout to null and use:

button1.setLocation(x,y);

or

button1.setBounds(x,y,width,height);

But if you want to see the button slide slowly to the left or w/e direction you want to go, you may wanna look into timers and threads,see whichever suits your need to simulate this kind of effect, watch out for memory leaks though by writing good code. =/

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