首页 > 代码库 > gui线程

gui线程

技术分享
package thread;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Thread1 extends JFrame implements Runnable ,ActionListener {

    private JPanel contentPane;
    private JTextField text;
    private JLabel ah = new JLabel("a");
    private Thread giveLetter=new Thread(this);
    private int score=0;
    private     JLabel defen = new JLabel("\u5F97\u52060");
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Thread1 frame = new Thread1();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Thread1() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 670, 129);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        
        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.CENTER);
        
        JLabel label = new JLabel("\u663E\u793A\u5B57\u6BCD");
        panel.add(label);
        
        panel.add(ah);
        
        JLabel label_1 = new JLabel("\u8F93\u5165\u663E\u793A\u7684\u5B57\u6BCD");
        panel.add(label_1);
        
        text = new JTextField();
        panel.add(text);
        text.setColumns(10);
        
        panel.add(defen);
        
        text.addActionListener(this);
        giveLetter.start();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO 自动生成的方法存根
        String letter=ah.getText().trim();
        String daan=text.getText();
        if(letter.equals(daan)){
            score++;
            defen.setText("score"+score);
            text.setText(null);
            validate();
            giveLetter.interrupt();
        }
        text.setText(null);
    }

    @Override
    public void run() {
        // TODO 自动生成的方法存根
        char c=‘a‘;
        for(;;){
            ah.setText(""+c+" ");
            validate();
            c=(char)(c+1);
            if(c>‘z‘)
                c=‘a‘;
            System.out.println(c);
            try{
                Thread.sleep(2000);
            }
            catch(Exception e){
                System.out.println(e.getMessage());
            }
        }
            
    }

}
View Code

 

gui线程