Tommonkey

All greatness comes from a brave beginning

0%

Java入门代码练习与记录-2

这次主要练习的是图形用户界面设计,当然也有一些乱起八糟的东西,但主要是Applet的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// java 序列化与反序列化demo
// 导入反序列化所需要的包
/*
import javax.naming.event.ObjectChangeListener;
import java.io.*;

class user implements java.io.Serializable{
public String name;
public String id;
public void OutInfo(){
System.out.println(this.name+this.id);
}
}

public class test {
public static void main(String[] args){
user ldd = new user();
ldd.name = "tommonkey";
ldd.id = "10086";
System.out.println(ldd.id);

try {
// 进行序列化,会在当前目录下生成一个序列化文件
FileOutputStream fileOut = new FileOutputStream("./serial.txt"); // 打开文件流
ObjectOutputStream objOut = new ObjectOutputStream(fileOut); // 创建对象流
objOut.writeObject(ldd); // 输出序列化对象
objOut.close(); // 关闭
fileOut.close();
System.out.println("serial is over!");

// 进行反序列化
user n = null;
FileInputStream fileIn = new FileInputStream("./serial.txt");
ObjectInput objIn = new ObjectInputStream(fileIn);
n = (user) objIn.readObject();
objIn.close();
fileIn.close();
System.out.println(n.name+n.id);
}catch (IOException i){
i.printStackTrace();
return;
}catch (ClassNotFoundException a){
System.out.println("Employee class not found");
a.printStackTrace();
return;
}
}
}
*/





// Runtime.getRuntime().exec():系统命令执行方法
/*
public class test {
public static void main(String[] args) throws Exception{
Runtime.getRuntime().exec("notepad.exe"); // 打开记事本
}
}
*/





// applet,awt-GUI界面响应事件流程demo
// 启动方法,再html页面中设置此class文件的路径名,再cmd启动:AppletViewer ****.html
// html中添加路径的方法:<APPLET CODE="test.class" width=500 height=500></APPLET>
/*
import sun.security.mscapi.CPublicKey;

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class test extends Applet implements ActionListener{ // 继承appelt 且实现接口:动作监听
private Button but1,but2;
int button = 0;
public void init(){
but1 = new Button("按钮1");
but2 = new Button("按钮2");
add(but1); // 注册事件监听
add(but2);
but1.addActionListener(this); // 事件监听本对象:but1
but2.addActionListener(this);
}
public void paint(Graphics g){
if(button==1)
g.drawString("事件监听到你选择了按钮1!",10,20);
else if(button==2)
g.drawString("事件监听到你选择了按钮2!",10,20);
}
public void actionPerformed(ActionEvent e){ // 实现动作监听必须要有实现该方法
if(e.getActionCommand().equals("按钮1"))
button = 1;
else if(e.getActionCommand().equals("按钮2"))
button =2;
repaint();
}
}
*/




// applet的文本框textField与文本区textArea演示
/*
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class test extends Applet implements ActionListener,TextListener{
TextArea ta1,ta2; // 创建两个文本区对象
TextField text1; // 创建一个文本框对象
public void init() {
text1 = new TextField("this is textField!", 30);
text1.addActionListener(this);
text1.addTextListener(this);
add(text1);

ta1 = new TextArea("this is textArea!", 100, 100);
ta1.addTextListener(this);
add(ta1);
ta2 = new TextArea("this is orther textArea!", 100, 100);
ta2.addTextListener(this);
add(ta2);
}
public void actionPerformed(ActionEvent e){
ta1.append("we have capture you action event and output here!\n");
repaint(); //刷新
}
public void textValueChanged(TextEvent e){
ta2.append("we have capture you action event and output here!\n");
repaint();
}
}
*/



// Appl的单选框与列表demo
/*
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class test extends Applet implements ItemListener{
CheckboxGroup cbk;
Checkbox cb1,cb2,cb3,cb4;
Color showColor;
List ls;
String[] colorName={"red","yellow","blue","black"};
String[] setColor={String.valueOf(Color.red), String.valueOf(Color.yellow), String.valueOf(Color.blue), String.valueOf(Color.black)};
int num;

public void init(){
cbk = new CheckboxGroup();
cb1 = new Checkbox("Red",cbk,true);
cb2 = new Checkbox("Yellow",cbk,false);
cb3 = new Checkbox("Blue",cbk,false);
cb4 = new Checkbox("Black",cbk,false);
showColor = Color.red;
cb1.addItemListener(this);
cb2.addItemListener(this);
cb3.addItemListener(this);
cb4.addItemListener(this);
add(cb1);
add(cb2);
add(cb3);
add(cb4);

ls = new List(4,false);
for(int i=0;i<colorName.length;i++){
ls.add(colorName[i]);
}
ls.addItemListener(this);
add(ls);
}

public void itemStateChanged(ItemEvent e){
if (e.getSource() == cb1){
showColor = Color.red; // getSource()方法:返回当前动作所指向的对象,包含对象的所有信息,而getActionCommond()是指当前动作指向的对象的名称
}
if (e.getSource() == cb2){
showColor = Color.yellow;
}
if (e.getSource() == cb3){
showColor = Color.blue;
}
if (e.getSource() == cb4){
showColor = Color.black;
}

// showStatus()方法:打印一行状态信息
showStatus(ls.getSelectedItem()+":序列号为 "+ls.getSelectedIndex());
num = ls.getSelectedIndex();
repaint();
}

public void paint(Graphics g){
g.setColor(showColor);
g.fillRect(50,80,50,50);

g.setColor(Color.decode(setColor[num])); // 这里的类型转化出了问题,我没找解决办法
g.fillOval(270,100,50,50);
}
}
*/


// 容器Panel(面板):无边框容器,没有边框,也不能移动,缩放,和关闭,通常作为一个容器组件被加到其他容器中
/*
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class test extends Applet implements ActionListener,ContainerListener{
Button but1,but2;
Label lab1,lab2,lab3,lab4,lab5;
Panel pan1,pan2,pan3,pan4,pan5; // 创建五个panel容器(面板)
public void init(){
pan1 = new Panel();
pan2 = new Panel();
pan3 = new Panel();
pan4 = new Panel();
pan5 = new Panel();
pan1.setBackground(Color.red); // 设置面板背景颜色
pan2.setBackground(Color.yellow);
pan3.setBackground(Color.black);
pan4.setBackground(Color.blue);
pan5.setBackground(Color.green);

lab1 = new Label("this is red panel");
lab2 = new Label("this is yellow panel");
lab3 = new Label("this is black panel");
lab4 = new Label("this is blue panel");
lab5 = new Label("this is green panel");

but1 = new Button("this is button1");
but2 = new Button("this is button2");

pan1.add(lab1); // 将标签添加到panel上
pan2.add(lab2);
pan3.add(lab3);
pan4.add(lab4);
pan5.add(lab5);

pan3.add(but1); // 将按钮添加到panel上
pan4.add(but2);

pan1.add(pan3);
pan2.add(pan4);

add(pan1); // 将panel添加到容器中
add(pan2);
add(pan5);

but1.addActionListener(this);
but2.addActionListener(this);

pan1.addContainerListener(this);
pan2.addContainerListener(this);
}

public void actionPerformed(ActionEvent e){
if (e.getSource() == but1){
pan1.remove(pan3);
showStatus("remove panel3 in panel1");
}else{
pan2.remove(pan4);
showStatus("remove panel4 in panel2");
}
}

public void componentRemoved(ContainerEvent e){

}

public void componentAdded(ContainerEvent e){

}
}
*/
奖励作者买杯可乐?