I am creating a program with a jbutton
and a jtextfield
. I want the textfield to display a message and then make the whole frame disappear after 1 second. I was able to display the message and make the frame disappear using frame.dispose
but I can't see the message because the frame disappears instantly. This is what I've tried. I heard using the swing timer also works but I have no idea how to use the swing timer.
//imports
public class GUIFastCash {
JFrame frame;
static JTextField window;
JButton twenty;
public void go () {
window = new JTextField();
twenty = new JButton("$20");
twenty.addActionListener(new Twenty());
frame = new JFrame();
//code adding button and textfield to frame
}
class Twenty implements ActionListener {
public void actionPerformed (ActionEvent event) {
//code to execute
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
frame.dispose();
}
}
}
First is to create a subclass of TimerTask and override the run method. Then, set the Timer to run this after the set amount of time.
class Dispose extends TimerTask {
@Override
public void run() {
frame.dispose;
}
}
then...
new Timer().schedule(new Dispose(), 1000);
The timer object will dispose the frame after 1000 milliseconds (1s)
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.