1 importjava.awt.Button;2 importjava.awt.FlowLayout;3 importjava.awt.Frame;4 importjava.awt.Label;5 importjava.awt.Panel;6 importjava.awt.TextField;7 importjava.awt.event.ActionEvent;8 importjava.awt.event.ActionListener;9 importjava.awt.event.WindowAdapter;10 importjava.awt.event.WindowEvent;11 importjava.awt.event.WindowListener;12 importjava.io.BufferedReader;13 importjava.io.File;14 importjava.io.FileReader;15 importjava.io.IOException;16 importjava.util.Scanner;17
18 importjavax.swing.JOptionPane;19
20 public classqmh {21 public staticString name,password;22 public static class DoEvent implementsWindowListener{23
24 @Override25 public voidwindowActivated(WindowEvent arg0) {26 //TODO Auto-generated method stub
27
28 }29
30 @Override31 public voidwindowClosed(WindowEvent arg0) {32 //TODO Auto-generated method stub
33
34 }35
36 @Override37 public voidwindowClosing(WindowEvent arg0) {38 //TODO Auto-generated method stub
39 System.out.println("窗口关闭");40 System.exit(0);41 }42
43 @Override44 public voidwindowDeactivated(WindowEvent arg0) {45 //TODO Auto-generated method stub
46
47 }48
49 @Override50 public voidwindowDeiconified(WindowEvent arg0) {51 //TODO Auto-generated method stub
52
53 }54
55 @Override56 public voidwindowIconified(WindowEvent arg0) {57 //TODO Auto-generated method stub
58
59 }60
61 @Override62 public voidwindowOpened(WindowEvent arg0) {63 //TODO Auto-generated method stub
64
65 }66
67 }68
69 public static voidmain(String[] args) {70
71 class DoEvent2 extendsWindowAdapter{72
73 @Override74 public voidwindowClosing(WindowEvent e) {75 //TODO Auto-generated method stub76 //super.windowClosing(e);
77 System.out.println("用户关闭窗口");78 System.exit(0);79 } //window已经实现了WindowListener的方法
80
81 }82 //TODO Auto-generated method stub83 //MVC V视图84 //DoEvent do1 = new DoEvent();//两个使用一个就可以
85 DoEvent2 do1 = newDoEvent2();86
87
88 Label l1=new Label("姓名:"); //标签
89 Label l2=new Label("密码:");90 TextField tf1=new TextField(20); //文本框
91 TextField tf2=new TextField(20);92 Button b1=new Button("确定"); //按钮
93 Button b2=new Button("取消");94 Frame f1=new Frame("登录窗口"); //窗口
95 /*-------------------------------------------------*/
96 Scanner scan = newScanner(System.in);97 String file_road=scan.nextLine();98 File file = newFile(file_road);99 BufferedReader reader = null;100 StringBuffer sbf = newStringBuffer();101 try{102 reader = new BufferedReader(newFileReader(file));103 String tempStr;104 while ((tempStr = reader.readLine()) != null) {105 sbf.append(tempStr);106 }107 reader.close();108 String[] s = sbf.toString().split(" ");109 name = s[0];110 password=s[1];111 //return sbf.toString();
112 } catch(IOException e) {113 e.printStackTrace();114 } finally{115 if (reader != null) {116 try{117 reader.close();118 } catch(IOException e1) {119 e1.printStackTrace();120 }121 }122 }123
124
125
126
127
128 /*-------------------------------------------------*/
129
130 /*-------------------------------------------------*/
131 //f1.addWindowListener(do1);//注册事件 ,这样注册和下面匿名类注册一样的结果
132
133 f1.addWindowListener(newWindowAdapter(){134
135 @Override136 public voidwindowClosing(WindowEvent e) {137 //TODO Auto-generated method stub138 //super.windowClosing(e);
139 System.out.println("用户关闭窗口");140 System.exit(0);141 } //window已经实现了WindowListener的方法
142
143 });144 /*-------------------------------------------------*/
145
146
147 /*-------------------------------------------------*/
148 //因为tf1在main里面,所以定义类也需要在tf1变量定义的下面
149 class Dobutton implementsActionListener{150
151 @Override152 public voidactionPerformed(ActionEvent a) {153 //TODO Auto-generated method stub
154 if(a.getSource()==b1)155 {156 if(name.equals(tf1.getText()) &&password.equals(tf2.getText()))157 {158 System.out.println("登陆成功!");159 System.out.println("用户名为:"+tf1.getText());160 System.out.println("用户密码为:"+tf2.getText());161 System.exit(0);162 }else{163 //System.out.println("用户名或密码错误!");
164 JOptionPane.showMessageDialog(null, "用户名或密码错误!", "Title",JOptionPane.ERROR_MESSAGE);165 tf1.setText(" "); //设置文本框内容,必须要有一个空格
166 tf2.setText(" ");167 }168
169 }170 else if(a.getSource()==b2){171 tf1.setText(" ");172 tf2.setText(" ");173 }174 }175
176 }177 b1.addActionListener(newDobutton());178 b2.addActionListener(newDobutton());179 /*-------------------------------------------------*/
180
181 /*-------------------------------------------------*/
182 //b2.addActionListener(new ActionListener(){183 //
184 //@Override185 //public void actionPerformed(ActionEvent arg0) {186 // //TODO Auto-generated method stub187 //tf1.setText("qwe");188 //tf2.setText("qwe");189 //}190 //
191 //});
192
193 /*-------------------------------------------------*/
194 f1.setSize(250, 300); //设置窗口大小
195 f1.setLocation(400, 400);196 f1.setLayout(null);197 Panel p=newPanel();198 p.setSize(250, 200);199 p.setLocation(0, 100);200 p.add(l1); //添加各种组件
201 p.add(tf1);202 p.add(l2);203 p.add(tf2);204 p.add(b1);205 p.add(b2);206 f1.add(p);207 f1.setVisible(true); //只有加上这一句各种组件才会显示出来
208 }209 }