package enter;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Enter {
private JPasswordField passwordField;
private JTextField textField;
private JFrame frame;
/**
* Launch the application
*
* @param args
*/
public static void main(String args[]) {
try {
Enter window = new Enter();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the application
*/
public Enter() {
initialize();
}
/**
* Initialize the contents of the frame
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("键盘确认测试程序");
frame.getContentPane().setLayout(null);
frame.setBounds(100, 100, 323, 245);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JLabel usernameLabel = new JLabel();
usernameLabel.setText("用户名:");
usernameLabel.setBounds(59, 83, 60, 15);
frame.getContentPane().add(usernameLabel);
final JLabel passwordLabel = new JLabel();
passwordLabel.setText("密 码:");
passwordLabel.setBounds(59, 117, 60, 15);
frame.getContentPane().add(passwordLabel);
final JLabel enterTestLabel = new JLabel();
enterTestLabel.setFont(new Font("", Font.PLAIN, 22));
enterTestLabel.setText("键盘按键确认测试程序");
enterTestLabel.setBounds(40, 21, 231, 33);
frame.getContentPane().add(enterTestLabel);
textField = new JTextField();
textField.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
// 跳到下一个文本输入框内等待输入
view();
}
}
});
textField.setBounds(139, 80, 112, 21);
frame.getContentPane().add(textField);
passwordField = new JPasswordField();
passwordField.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
view();
}
}
});
passwordField.setBounds(139, 112, 112, 25);
frame.getContentPane().add(passwordField);
final JButton okButton = new JButton();
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
view();
}
});
okButton.setText("提交");
okButton.setBounds(59, 164, 83, 25);
frame.getContentPane().add(okButton);
final JButton aboutButton = new JButton();
aboutButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
About abo = new About();
abo.show();
}
});
aboutButton.setText("关于");
aboutButton.setBounds(168, 164, 83, 25);
frame.getContentPane().add(aboutButton);
}
public void view() {
String user=textField.getText();
char[] pass1=passwordField.getPassword();
String pass=String.copyValueOf(pass1);
// System.out.println(pass);
if (!user.equals("")&& !pass.equals("")) {
JOptionPane.showMessageDialog(null, "输入正确!", "提示",
JOptionPane.INFORMATION_MESSAGE);
// System.exit(0);
} else {
System.out.print(passwordField.getPassword());
JOptionPane.showMessageDialog(null, "错了没有呀?文本框都要有内容的哦!", "提示",
JOptionPane.INFORMATION_MESSAGE);
}
}
}