JavaGUI实现点名系统

来源:互联网 发布:行知实验中学 编辑:程序博客网 时间:2024/05/26 12:03
package Week1008;
 
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
 
 
public class Main extendsThreadimplementsActionListener {
    String[] s= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
        JFrame frame=newJFrame("点名使用");
        JButton btn=newJButton("停止");
        JTextField tf=newJTextField();    
        publicMain(){
            frame.setLayout(null);
            frame.setBounds(300, 400, 300, 350);
            tf.setBounds(100, 40, 120, 30);
            tf.setFont(newFont("Monospaced", 23, 30));
            btn.setBounds(80, 120, 150, 100);
            frame.add(btn);
            frame.add(tf);
            frame.setVisible(true);
            btn.addActionListener(this);
        }      
        publicvoid run(){
            for(int i=1;i<=20;i++){
                 
                tf.setText(s[i-1]);
                try{
                    sleep(50);
                }catch(InterruptedException e) {
                    e.printStackTrace();
                }
                if(i==20)i=1;
            }          
        }      
        publicvoid actionPerformed(ActionEvent e) {
            if(e.getSource()==btn){
                if(btn.getText().equals("开始")){
                    this.resume();
                    btn.setText("停止");
                }
                elseif(btn.getText().equals("停止")){
                    //this.stop();
                    this.suspend();
                    btn.setText("开始");
                }              
                }
            }                  
        publicstaticvoid main(String[] args) {
            Main m=newMain();
            m.start();
        }
}
1 0