Is it possible to create a pause in the middle of Swing application?

为君一笑 提交于 2021-01-20 12:50:18

问题


I'm working on a project that involves access by reading a String from a physical card, the following code simplifies the main point of my program, however when I try to make a pause for a few seconds after an user has swipe his card, something goes wrong and the behavior is not what I need, the program pauses but the color's pane doesn't change nor the label.

Any suggestion?

This is the code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class B2 extends JFrame implements ActionListener {
JLabel lbl;
JButton btn;
JTextField jtf;
String password = "123";

public B2() {
    super("");

    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    this.setLayout(null);
    this.setVisible(true);

    setBounds(0,0,480, 250);
    getContentPane().setBackground(Color.cyan);

    btn = new JButton("OK");
    lbl = new JLabel("Enter your password");
    jtf = new JTextField(25);

    btn.addActionListener(this);
    lbl.setBounds(50, 100, 200, 25);
    btn.setBounds(150, 180, 110, 25);
    jtf.setBounds(20, 180, 110, 25);

    getContentPane().add(btn);
    getContentPane().add(lbl);
    getContentPane().add(jtf);
 }

public void actionPerformed(ActionEvent e) {

    if(jtf.getText().equals(password)) {
        getContentPane().setBackground(Color.green);
        lbl.setText("Welcome");
    } else {
        getContentPane().setBackground(Color.red);
        lbl.setText("Access Denied");
    }

    try {
        Thread.sleep(3000);
    } catch (InterruptedException e1) {
        e1.printStackTrace();
    }

    getContentPane().setBackground(Color.cyan);
    lbl.setText("Enter your password");
}

  public static void main(String[] args) {

  javax.swing.SwingUtilities.invokeLater(new Runnable() {
         @Override
         public void run() {
             B2 frame = new B2();
         }
      });
}
}

回答1:


Using Thread.sleep() prevents Swing from doing a repaint until the pause has finished. So it will change the color and change it back at the same time and you won't see the effect.

Try to use a Timer instead. Add this to your code and it will work:

if(jtf.getText().equals(password)) {
    getContentPane().setBackground(Color.green);
    lbl.setText("Welcome");

} else {
    getContentPane().setBackground(Color.red);
    lbl.setText("Access Denied");
}

Timer timer = new Timer(3000, new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        getContentPane().setBackground(Color.cyan);
        lbl.setText("Enter your password");
    }
});
timer.setRepeats(false);
timer.start();



回答2:


put last 2 lines in comment

//getContentPane().setBackground(Color.cyan);

//lbl.setText("Enter your password");

you will see effect only when action is performed. when actionPerformed() is executed. hope you got your solution



来源:https://stackoverflow.com/questions/29115514/is-it-possible-to-create-a-pause-in-the-middle-of-swing-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!