经过大家的帮忙写的一个随即点人的程序,有不足之处,请各位指出来
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import java.util.Random;
class Tim extends JFrame{
JPanel pnl;
JButton btn1=new JButton("开始");
JButton btn2=new JButton("停止");
Vector vec=new Vector();
Random rod=new Random();
JTextField txt=new JTextField();
Class cl;
ActionListener listener=new ActionListener(){
public void actionPerformed(ActionEvent e){
obj.start();
String s=vec.get(rod.nextInt(9)).toString();
txt.setText(s);
}
};
Timer obj=new Timer(1,listener);
Tim(){
super("Timer");
vec.addElement("aa");
vec.addElement("bb");
vec.addElement("cc");
vec.addElement("dd");
vec.addElement("ee");
vec.addElement("ff");
vec.addElement("hh");
vec.addElement("jj");
vec.addElement("kk");
try{
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
men();
action();
}catch(Exception ce){
ce.printStackTrace();
}
}
void men(){
pnl=(JPanel)getContentPane();
pnl.setLayout(null);
setSize(new Dimension(400,300));
btn1.setBounds(50,100,100,20);
btn2.setBounds(200,100,100,20);
txt.setBounds(100,200,100,20);
pnl.add(btn1);
pnl.add(btn2);
pnl.add(txt);
this.setVisible(true);
}
void action(){
btn1.addActionListener(listener);
btn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
obj.stop();
}
});
}
public static void main(String[] args){
Tim cc=new Tim();
}
}