gui线程

Posted Kooing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 计时器

package thread;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

public class Thread1 extends JFrame implements Runnable ,ActionListener {

    private JPanel contentPane;
    private Thread thread1=new Thread(this);
    private boolean live=true;
    JTextArea text = new JTextArea();
    JButton start = new JButton("New button");
    JButton stop = new JButton("New button");
    SimpleDateFormat format=new SimpleDateFormat("hh:mm:ss");
    /**
     * 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, 343, 417);
        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.NORTH);
        
        panel.add(start);
        
        panel.add(stop);
        
        JPanel panel_1 = new JPanel();
        contentPane.add(panel_1, BorderLayout.CENTER);
        
        JScrollPane scrollPane = new JScrollPane();
        panel_1.add(scrollPane);
        
        text.setRows(14);
        text.setColumns(17);
        scrollPane.setViewportView(text);
        start.addActionListener(this);
        stop.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==start){
            thread1.start();
            
        }
        else if(e.getSource()==stop){
            live=false;
        }
    }

    @Override
    public void run() {
        for(;;){
            Date date1=new Date();
            text.append(format.format(date1)+"\\n");
            try{
                Thread.currentThread().sleep(1000);
            }
            catch(Exception e){
                System.out.println(e.getMessage());
            }
            if(!live)
                return;
        }
    }

}
View Code

 计时器线程

一个很方便的Timer类,在javax.swing包中,

它感觉就像一个有for循环的线程一样,隔一段时间调用一次,不过他的调用了通过监听器实现的

两个构造方法

Timer(int a)

Timer(int a,Object b)b是实现了ActionListener接口中actionPerformed方法的类,而且还要是JFrame,JButton等组件类的实例

还有三个方法 start(),stop(),restart()

相当线程的 strat(),wart(),notify()

测试代码

package thread;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.Timer;

public class Thread1 extends JFrame implements ActionListener {

    private Timer time=new Timer(1000,this);
    private JPanel contentPane;
    private Thread thread1;
    private boolean live=true;
    JButton start = new JButton("start");
    JButton continue1 = new JButton("continue");
    JButton stop = new JButton("stop");
    SimpleDateFormat format=new SimpleDateFormat("hh:mm:ss");
    private JTextField text;
    /**
     * 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, 502, 181);
        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.NORTH);
        
        panel.add(start);
        
        panel.add(stop);
        
        panel.add(continue1);
        
        JPanel panel_1 = new JPanel();
        contentPane.add(panel_1, BorderLayout.CENTER);
        
        text = new JTextField();
        panel_1.add(text);
        text.setColumns(13);
        start.addActionListener(this);
        stop.addActionListener(this);
        continue1.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==time){
            Date date=new Date();
            text.setText(format.format(date));
            int x=text.getBounds().x;
            int y=text.getBounds().y;
            y+=2;
            x-=2;
            text.setLocation(x,y);
        }
        else if(e.getSource()==start){
            time.start();
        }
        else if(e.getSource()==stop){
            time.stop();
        }
        else if(e.getSource()==continue1){
            time.restart();
        }
    }


}
View Code

 

以上是关于gui线程的主要内容,如果未能解决你的问题,请参考以下文章

从在非 GUI 线程中运行的 C 代码获取 QInputDialog::getText() 结果

newCacheThreadPool()newFixedThreadPool()newScheduledThreadPool()newSingleThreadExecutor()自定义线程池(代码片段

QT 实用代码片段

QThread 和 GUI 线程说明

如何将 PyQt 插槽从后台线程连接到 gui 线程

多线程更新UI的常用方法