用的Mac OS ,所以eclipse运行显示为Mac OS风格。
先放效果图
程序算法十分简单,刚刚学完JAVA-GUI编程,而且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));
//btn1.setBounds(20,20,50,20);
//btn2.setBounds(90,20,50,20);
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);
}
}
}
//window时间监听器,用于关闭window窗口
class MyWindowMoniter extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}