满意答案
jslwz007
推荐于 2018.03.09
采纳率:46% 等级:12
已帮助:6219人
焦点事件必须是在4102confirm输入之后调用才能起作用
import java.awt.FlowLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class PwdValidation {
public static void main(String[] args) {
1653JFrame f = new JFrame("Validation");
JLabel pwd = new JLabel("Input");
JLabel confirm = new JLabel("Confirm");
final JTextField password = new JTextField(15);
final JTextField cfmPwd = new JTextField(15);
f.add(pwd);
f.add(password);
f.add(confirm);
f.add(cfmPwd);
cfmPwd.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent e) {}
public void focusLost(FocusEvent e) {
if(!password.getText().equals(cfmPwd.getTreeLock())){
JOptionPane.showMessageDialog(null, "Passowrd not same, please input again!","Warning",JOptionPane.WARNING_MESSAGE);
password.setText("");
cfmPwd.setText("");
}
}
});
f.setLayout(new FlowLayout());
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
01分享举报