我一直在尝试找到用于以下代码的最佳计时器(请注意,这是我整个程序的简化版本)。我希望在3秒钟后运行一种方法。
问题出在actionPerformedcheckBlankLoginresetLoginBlank上,并放置了一个计时器,以延迟resetLoginBlankcheckBlankLogin发生后3秒钟内发生。但是我希望类Outerframe中的所有方法能够连续运行。因此checkBlankLogin将继续检查其空白,直到该人输入"Valid Input"的信息并且登录innerframe将关闭。但是我不知道该怎么做...那里还有帮助吗?

     import java.awt.*;
    import java.awt.BorderLayout;
    import java.awt.event.*;
   import javax.swing.*;
   import javax.swing.border.EmptyBorder;
   import javax.swing.event.*;
   import java.io.*;
    import java.io.File;
   import java.util.*;
   import java.io.FileNotFoundException;

  class OuterFrame extends JFrame implements ActionListener
 {
Container pane; // container
JDesktopPane outframe; // outer frame
JInternalFrame login; // login frame
//pieces of login frame
    JLabel loginLBLtitle;
    JPanel loginPanel;
    JLabel loginLBLname;
    JLabel loginBlankName;
    JLabel loginLBLpass;
    JLabel loginBlankPass;
    JTextField loginTXT;
    JPasswordField loginPASS;
    JButton loginBUT;
JInternalFrame apple;



OuterFrame()
{
    //set up for Outer Frame
    super("Application");
    setSize(450,240);
    setLocationRelativeTo(null);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    outframe = new JDesktopPane();
    outframe.setBackground(Color.BLUE);
    //set up for Container
    pane = getContentPane();
    setContentPane(pane);
    pane.add(outframe);


    //Login Inner Frame
    login = new JInternalFrame();
    login.setSize(400,200);
    login.setLocation(20,20);
    login.setTitle("Member Login");
    loginLBLtitle = new JLabel("Sign in with netid and your password.");
    Font loginFontbody = new Font("SansSerif", Font.PLAIN, 12);
    Font loginFonthead = new Font("SansSerif", Font.BOLD, 13);
    loginLBLtitle.setFont(loginFonthead);
    loginLBLname=new JLabel("User Name:");
    loginLBLname.setFont(loginFontbody);
    loginLBLpass=new JLabel("Password:  ");
    loginLBLpass.setFont(loginFontbody);
    loginBUT=new JButton("Login");
    loginBUT.setFont(loginFontbody);
    loginBUT.addActionListener(this);
    loginTXT=new JTextField(20);
    loginPASS=new JPasswordField(20);
    loginBlankName=new JLabel("");
    loginBlankPass=new JLabel("");
    loginPanel=new JPanel();
    loginPanel.add(loginLBLtitle);
    loginPanel.add(loginLBLname);
    loginPanel.add(loginTXT);
    loginPanel.add(loginBlankName);
    loginPanel.add(loginLBLpass);
    loginPanel.add(loginPASS);
    loginPanel.add(loginBlankPass);
    loginPanel.add(loginBUT);
    //panel.add(lblmess);
    login.add(loginPanel);
    login.setVisible(true);
    //Add Login to Outer Frame
    outframe.add(login);
    outframe.setSelectedFrame(login);
    pane.add(outframe, BorderLayout.CENTER);
    setVisible(true);
    loginTXT.requestFocus();

}
public void actionPerformed(ActionEvent e)
{
    //problem area
    if(e.getSource()==loginBUT)
    {
        String uname=loginTXT.getText();
        String passw=new String(loginPASS.getPassword());
        int i=0;
        while(i!=5)
        {
            if(checkBlankLogin(uname,passw,loginBlankName,loginBlankPass))
            {
                resetLoginBlank(loginBlankName,loginBlankPass);
            }
            else
            {

            if(!validateUser("accounts.txt",uname,passw,loginLBLtitle))
                {

            }
        }
    }
}
public void resetLoginBlank(JLabel loginBlankName, JLabel loginBlankPass)
{
    loginBlankName.setText("");
    loginBlankPass.setText("");
}
public void resetLoginTitle(JLabel loginBlankTitle)
{
    loginBlankTitle.setText("Sign in with netid and your password.");
    loginBlankTitle.setForeground(Color.BLACK);
}
public boolean checkBlankLogin(String name, String passw, JLabel loginBlankName, JLabel loginBlankPass)
{
    boolean isBlank=false;
    if(name.length()<1)
    {
        loginBlankMess("User name is required.",loginBlankName);
        isBlank=true;
    }
    if(passw.length()<1)
    {
        loginBlankMess("Password is required.",loginBlankPass);
        isBlank=true;
    }
    return isBlank;
}
public void loginBlankMess(String mess, JLabel lbl)
{
    lbl.setText(mess);
    lbl.setForeground(Color.RED);
}
public boolean validateUser(String filename, String name, String password, JLabel title)
{
        boolean valid = false;

    try
    {
        File file = new File(filename);
        Scanner scanner = new Scanner(file);
        ArrayList<String> fileInfo = new ArrayList<String>();
        while (scanner.hasNextLine())
        {
            String line = scanner.nextLine();
            fileInfo.add(line);
        }
        String fullLogin = name + " " + password;
        if(fileInfo.contains(fullLogin))
        {
            //loginBlankMess("Valid login",namemess);
            valid=true;
        }
        if(!valid)
        {
            loginBlankMess("Please enter valid netid and password.", title);
            resetLoginTitle(title);
        }
    }
    catch(Exception ie)
    {
        System.exit(1);
    }
    return valid;
}
  }

   public class TheProgram
  {
public static void main(String[] args)
{
    new OuterFrame();
}
  }`

最佳答案

我将检查以下资源(假设您将Swing用于UI):

How to Use Swing Timers (Oracle)

10-08 01:47