Advertisement
Guest User

Untitled

a guest
Apr 19th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. package com.wy.FrameClass;
  2.  
  3.  
  4.  
  5. import javax.swing.*;
  6. import java.awt.*;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9.  
  10. /**
  11. * 测试输入密码和用户名后打印输入的密码和用户名。
  12. * Created by Administrator on 2016/4/18.
  13. */
  14. public class PasswordField extends JFrame {
  15. private JTextField userName;
  16. private JPasswordField userPassword;
  17. private JButton logonButton;
  18. private JButton exitButton;
  19. public PasswordField(){
  20. super("密码验证");
  21. Container container=getContentPane();
  22. container.setLayout(new GridLayout(3,2,2,2));
  23. userName=new JTextField(16);
  24. userPassword=new JPasswordField(16);
  25. logonButton=new JButton("登录");
  26. exitButton=new JButton("退出");
  27. logonButton.addActionListener(new AbstractAction() {
  28. @Override
  29. public void actionPerformed(ActionEvent e) {
  30. char[] pw=userPassword.getPassword();
  31. String message="你的用户名是:"+userName.getText()+"你的密码是:"+new String(pw);
  32. JOptionPane.showMessageDialog(PasswordField.this,message);
  33. }
  34. });
  35. exitButton.addActionListener(new ActionListener() {
  36. @Override
  37. public void actionPerformed(ActionEvent e) {
  38. System.exit(-1);
  39. }
  40. });
  41. container.add(new JLabel(" 用户名:"));
  42. container.add(userName);
  43. container.add(new JLabel(" 密码:"));
  44. container.add(userPassword);
  45. container.add(logonButton);
  46. container.add(exitButton);
  47. setResizable(false);
  48. setSize(300,200);
  49. setVisible(true);
  50. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  51. }
  52. public static void main(String []args){
  53. new PasswordField();
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement