Swing - 带有actionPerformed()问题的revalidate()

时间:2015-06-10 23:04:39

标签: java swing

我正在玩swing并且我做了一个登录屏幕但是在用户输入正确并且如果他试图输入错误的凭据后返回,则警告消息显示超过1次。我认为它与revalite()在actionPerformed()中有关。请帮助!

这是包含所有GUI的类:

package Login;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class loginScreen implements ActionListener{

JFrame loginFrame = new JFrame("Welcome");

//login()components
JTextField userName = new JTextField();
JTextField password = new JTextField();
JButton login = new JButton("Login");
//login()components
//welcome()components
JLabel message = new JLabel("Welcome user!");
JButton back = new JButton("Get Back");
//welcome()components
//ArrayLists
ArrayList<String> users = new ArrayList<String>(Arrays.asList("1", "user1",    "user2"));
ArrayList<String> passwords = new ArrayList<String>(Arrays.asList("1", "pass1", "pass2"));
//ArrayLists

public loginScreen(){

    loginFrame.setSize(200, 180);
    loginFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    loginFrame.setVisible(true);
    loginFrame.setLayout(new GridLayout(1, 1));
    loginFrame.add(login());

}
public void refresh(JPanel panel){

    loginFrame.getContentPane().removeAll();
    loginFrame.getContentPane().add(panel);
    loginFrame.getContentPane().revalidate();
    //loginFrame.getContentPane().repaint();

}
public boolean testCredentials(String userName, String password){
    boolean isCorrect = false;
    for (int i = 0; i < users.size(); i++) {
        if (userName.equals(users.get(i))) {
            if (password.equals(passwords.get(i))) {
                isCorrect = true;
            }
        }
    }
    return isCorrect;
}
public JPanel login(){

    JPanel panel = new JPanel();

    panel.setLayout(null);

    //userName
    userName.setSize(150, 25);
    userName.setLocation(20, 20);
    //userName
    //password
    password.setSize(150, 25);
    password.setLocation(20, 60);
    //password
    //login
    login.setSize(100, 30);
    login.setLocation(50, 100);
    login.addActionListener(this);
    //login

    panel.add(userName);
    panel.add(password);
    panel.add(login);

    return panel;
}
public JPanel welcome(){

    JPanel panel = new JPanel();

    panel.setLayout(null);

    //message
    message.setSize(100, 30);
    message.setLocation(50, 30);
    //message
    //back
    back.setSize(100, 30);
    back.setLocation(50, 90);
    back.addActionListener(this);
    //back

    panel.add(message);
    panel.add(back);

    return panel;
}
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == login) {
        if (testCredentials(userName.getText(), password.getText()) == true) {
            refresh(welcome());
        } else {
            JOptionPane.showMessageDialog(null, "Wrong username or password!");
        }
    }
    if (e.getSource() == back) {
        refresh(login());
    }
}
}

此类包含main():

package Login;

public class Main {

public static void main(String[] args) {

    loginScreen l = new loginScreen();

}
}

2 个答案:

答案 0 :(得分:1)

我的“怀疑”是,每当您“返回”登录屏幕时,您正在调用login方法,该方法将loginScreen注册为另一个ActionListener login按钮。

您应该将您的功能分离为单独的工作单元,使他们只能完成他们设计的单一工作。

例如,loginScreen不应该做出导航决定,而应该通知某种观察者登录状态已经过/失败,并允许它做出关于它应该做什么的决定例

您可能希望了解Model-View-Controller范例

答案 1 :(得分:0)

也许是因为每次拨打action listener时都要添加相同的refresh ...

你可以只添加一次action listener ......就像这样:

public void initialize(){
        login.addActionListener(this);
        back.addActionListener(this);
}

并在构造函数后调用它......

 public static void main(String[] args) {
        loginScreen l = new loginScreen();
        l.initialize();
 }