写在前面,因为用的Mac OS ,所以eclipse运行显示为Mac OS风格。
先放效果图
这里写图片描述
程序算法十分简单,刚刚学完JAVA-GUI编程,就小试牛刀,学的过时的AWT,而且API文档也没怎么看就心急赶紧做了这么个小东西。
具体代码实现如下:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class TestKeyborad {
public static void main(String []args){
MyFrame f = new MyFrame();
f.launchFrame();
}
}
class MyFrame extends Frame{
Label text2;
void launchFrame(){
setTitle("学生点名系统");
setVisible(true);
Button btn1 = new Button("开始");
Button btn2 = new Button("结束");
Label text1 = new Label("学号:");
text2 = new Label("按下开始按钮开始点名");
Panel p=new Panel();
setBounds(300,300,300,400);
p.setBackground(Color.white);
p.setBounds(0,0,300,400);
p.setLayout(new FlowLayout(FlowLayout.CENTER,60,100));
add(p);
p.add(text1);
p.add(text2);
p.add(btn1);
p.add(btn2);
this.addWindowListener(new MyWindowMoniter());
btn1.addActionListener(new btnMoniter(this));
btn2.addActionListener(new btnMoniter(this));
}
}
class btnMoniter implements ActionListener {
MyFrame f;
btnMoniter(MyFrame f){
this.f=f;
}
public void actionPerformed(ActionEvent e){
String s=e.getActionCommand();
Random r=new Random();
int i=r.nextInt(50);
if(s=="开始"){
f.repaint();
f.text2.setText("正在点名!");
}
if(s=="结束"){
f.repaint();
f.text2.setText(""+i);
}
}
}
class MyWindowMoniter extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
- 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
第一次认真的写CSDN博客,好多东西都没去研究。排版也比较垃圾。文章写的也毫无文采可言。
最重要的是:程序实现不是很好,刚刚从面向过程语言转面向对象语言。缺乏面对对象思维。
这是本人第一个JAVA桌面程序。