0

我一直在尝试找到用于以下代码的最佳计时器(注意这是我整个程序的简化版本)。我希望在 3 秒后运行一个方法。问题在于actionPerformed,checkBlankLogin和 ,并在发生 3 秒后resetLoginBlank设置一个计时器来延迟发生。但我希望类中的所有方法都能连续运行。因此,将继续检查其是否为空白,直到该人输入 a 的信息并且登录将关闭。但我不知道该怎么做......那里还有什么帮助吗?resetLoginBlankcheckBlankLoginOuterframecheckBlankLogin"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();
} 
  }`
4

2 回答 2

3

我会查看以下资源(假设您在 UI 中使用 Swing):

如何使用摆动计时器 (Oracle)

于 2013-06-25T18:42:22.907 回答
2

在您的情况下,摆动计时器是最简单的。您让您的类实现 ActionListener,并创建一个计时器对象。计时器到期时会调用 actionPerformed 方法。

import javax.swing.Timer;

class OuterFrame extends JFrame implements ActionListener{
   Timer timer = null;

   public void actionPerformed(ActionEvent e) {
        if(e.getSource()==loginBUT){
            //If the action came from the login button
            if (checkBlankLogin()){
               timer = new Timer(3000, this);
               timer.setRepeats(false);
               timer.setInitialDelay(3000);
               timer.start(); 
            } else if (timer != null){
               timer.stop();
            }
        }else if(e.getSource()==timer){
            //If the action came from the timer
            resetLoginBlank(namemess,passwmess));
        }
    }
}
于 2013-06-25T18:41:14.947 回答