java 图形编程_Java图形界面编程

1 importjava.awt.GridLayout;2 importjava.awt.event.ActionEvent;3 importjava.awt.event.ActionListener;4 importjavax.swing.JButton;5 importjavax.swing.JFrame;6 importjavax.swing.JLabel;7 importjavax.swing.JOptionPane;8 importjavax.swing.JPasswordField;9 importjavax.swing.JTextField;10

11 /**

12 * java图形界面编程演示,以登录为例13 *14 *@author

15 *16 */

17 public class LoginFrame extends JFrame implementsActionListener {18

19 private JTextField username; //用户名

20 private JPasswordField password; //密码

21 private JButton login; //登录

22 private JButton cancel; //取消

23

24 publicLoginFrame(String title) {25 setTitle(title);26 //设置大小

27 setSize(200, 100);28 //设置居中

29 setLocationRelativeTo(null);30 //退出应用程序

31 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);32 //不允许修改窗口大小

33 setResizable(false);34

35 //生成界面

36 generateInterface();37

38 //注册监听器:匿名内部类39 //registerListener();40

41 //设置窗体可见

42 setVisible(true);43 }44

45 /**

46 * 生成界面47 */

48 private voidgenerateInterface() {49 username = newJTextField();50 password = newJPasswordField();51 login = new JButton("登录");52

53 /*注册监听器的方式*/

54

55 //普通类,实现XxxListener接口56 //login.addActionListener(new Listener(username, password));57

58 //成员内部类,实现XxxListener接口59 //login.addActionListener(new Listener());//成员内部类60

61 //当前类实现XxxListener接口

62 login.addActionListener(this);63

64 cancel = new JButton("取消");65 cancel.addActionListener(this);66

67 JLabel uname = new JLabel("用户名:");68 JLabel upwd = new JLabel("密 码:");69

70 setLayout(new GridLayout(3, 2));71

72 this.add(uname);73 this.add(username);74 this.add(upwd);75 this.add(password);76 this.add(login);77 this.add(cancel);78 }79

80 /**

81 * 成员内部类实现ActionListener82 *83 *@author

84 *85 */

86 class Listener implementsActionListener {87 @Override88 public voidactionPerformed(ActionEvent e) {89 String uname =username.getText();90 String upwd = newString(password.getPassword());91 if ("admin".equals(uname) && "123456".equals(upwd)) {92 //消息框

93 JOptionPane.showMessageDialog(null, "登录成功");94 } else{95 //消息框

96 JOptionPane.showMessageDialog(null, "用户名或密码错误");97 }98 }99 }100

101 /**

102 * 匿名内部类实现ActionListener103 */

104 private voidregisterListener() {105 //匿名内部类

106 login.addActionListener(newActionListener() {107 @Override108 public voidactionPerformed(ActionEvent e) {109 String uname =username.getText();110 String upwd = newString(password.getPassword());111 if ("admin".equals(uname) && "123456".equals(upwd)) {112 //消息框

113 JOptionPane.showMessageDialog(null, "登录成功");114 } else{115 //消息框

116 JOptionPane.showMessageDialog(null, "用户名或密码错误");117 }118 }119 });120 }121

122 /**

123 * 本类实现ActionListener124 */

125 @Override126 public voidactionPerformed(ActionEvent e) {127 JButton btn =(JButton) e.getSource();128 if ("登录".equals(btn.getText())) {129 JOptionPane.showMessageDialog(null, "你点击了登录");130 } else{131 JOptionPane.showMessageDialog(null, "你点击了取消");132 }133 }134

135 public static voidmain(String[] args) {136 LoginFrame frame = new LoginFrame("登录");137 }138 }139

140 /**

141 * 外部类实现ActionListener142 *143 *@author

144 *145 */

146 class Listener implementsActionListener {147

148 privateJTextField username;149 privateJPasswordField password;150

151 publicListener(JTextField username, JPasswordField password) {152 super();153 this.username =username;154 this.password =password;155 }156

157 @Override158 public voidactionPerformed(ActionEvent e) {159 String uname =username.getText();160 String upwd = newString(password.getPassword());161 if ("admin".equals(uname) && "123456".equals(upwd)) {162 //消息框

163 JOptionPane.showMessageDialog(null, "登录成功");164 } else{165 //消息框

166 JOptionPane.showMessageDialog(null, "用户名或密码错误");167 }168 }169 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值