简体   繁体   English

在Java中为GUI使用哪个计时器

[英]Which timer to use for GUI in Java

I have been trying to find the best timer to use for the following code (note this is a simplified version of my overall program). 我一直在尝试找到用于以下代码的最佳计时器(请注意,这是我整个程序的简化版本)。 My hope is to run a method after 3 seconds. 我希望在3秒钟后运行一种方法。 The problem is with the actionPerformed , checkBlankLogin , and resetLoginBlank and putting a timer to delay resetLoginBlank from happening 3 seconds after checkBlankLogin has happened. 问题出在actionPerformedcheckBlankLoginresetLoginBlank并放置了一个计时器来延迟resetLoginBlankcheckBlankLogin发生3秒后才发生。 But I want all methods in the class Outerframe to continuously run. 但是我希望Outerframe类中的所有方法Outerframe连续运行。 So checkBlankLogin will keep checking if its blank until the person inputs the information for a "Valid Input" and the Login innerframe will close. 因此, checkBlankLogin将继续检查其是否为空白,直到该人输入"Valid Input"的信息,并且Login innerframe将关闭。 But I don't know how to do that... Any help there also? 但是我不知道该怎么做...那里还有帮助吗?

     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();
} 
  }`

I would check out the following resource (assuming you using Swing for your UI): 我将检查以下资源(假设您将Swing用于UI):

How to Use Swing Timers (Oracle) 如何使用Swing计时器(Oracle)

Swing timers are the easiest in your case. 在您的情况下,摆动计时器是最简单的。 You make your class implement ActionListener, and create a timer object. 您使您的类实现ActionListener,并创建一个计时器对象。 The timer will call the actionPerformed method when it expires. 计时器将在过期时调用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));
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM