1 package com.cilinmengye.HouseWork5;
2
3 import javax.swing.*;
4 import java.awt.*;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7
8 //继承JFrame实现界面
9 public class GUIClass01 extends JFrame implements ActionListener {
10 //事件源:
11 private JButton readyButton, exitButton;
12 private JLabel messageLabel;
13 private JTextField name;
14 private JPasswordField passWord;
15
16 public GUIClass01() {
17 //设置标题
18 super("这是我第一个简单GUI页面");
19
20 //设置相关布局
21 setSize(500, 100);
22 setBackground(Color.lightGray);
23 setLocation(300, 240);
24 setLayout(new FlowLayout());
25
26 //添加相关组件
27 add(new JLabel("用户名: "));
28 name = new JTextField(5);
29 add(name);
30
31 add(new JLabel("密码: "));
32 passWord = new JPasswordField(5);
33 add(passWord);
34
35 readyButton = new JButton("确定");
36 exitButton = new JButton("退出");
37 add(readyButton);
38 add(exitButton);
39
40 messageLabel = new JLabel(" ");
41 add(messageLabel);
42
43 //添加相关事件,将事件源与处理者联系起来
44 //this实现了事件处理方法ActionListener
45 readyButton.addActionListener(this);
46 exitButton.addActionListener(this);
47 //注意messageLabel就不用进行关联了,因为其不可以触发事件
48
49 //注意setVisible(true)一定要放在最后
50 setVisible(true);
51 }
52
53 //实现对应的事件处理:
54 public void actionPerformed(ActionEvent e) {
55 if (e.getSource() == readyButton) {
56 String key = String.valueOf(passWord.getPassword());
57 String userName = name.getText();
58 if (key.equals("834430027") && userName.equals("次林梦叶"))
59 messageLabel.setText("欢迎您的使用,次林梦叶");
60 else messageLabel.setText("用户名或密码错误");
61 //好像这个语句加不加都没问题?
62 /*setVisible(true);*/
63 } else if (e.getSource() == exitButton)
64 System.exit(0);
65 }
66 }
67
68 class App {
69 public static void main(String[] args) {
70 new GUIClass01();
71 }
72 }
原文地址:http://www.cnblogs.com/cilinmengye/p/16852962.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性