当前位置:  开发笔记 > 编程语言 > 正文

Java Swing GUI - 如何让线程一直处于休眠状态并通过点击唤醒?

如何解决《JavaSwingGUI-如何让线程一直处于休眠状态并通过点击唤醒?》经验,为你挑选了1个好方法。

抱歉,这是我第一次使用Threads.

我希望Parlami类线程能够睡眠,并且只能被actionListener唤醒.

我试过这种方式,但它不起作用,他还在睡觉.以这种方式使用线程是正确的还是我应该使用wait()?

package parlami;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author giacomofava
 */
public class Parlami
{
    public boolean finito = false;
    public String s="";

    public void ascolta()
    {
        int i=0;
        while (i<=1500)
        {
            // dormi 50 millisecondi 
            try
            {
                Thread.sleep(50);
                i+=40;
            }
            catch (InterruptedException e)
            {
            }

            while (voce.SpeechInterface.getRecognizerQueueSize() > 0)
            {
                s = s+"\n"+voce.SpeechInterface.popRecognizedString();
            }
        }
    }

    public String scrivi()
    {
        return "Hai detto: "+s;
    }

    public void leggi()
    {
        voce.SpeechInterface.synthesize(s);
    }

    public void dormi(int milli)
    {
        try
        {
            System.out.println("i'm sleeping");
            Thread.sleep(milli);
        }
        catch (InterruptedException ex)
        {
            System.out.println("i'm awake ");
            ascolta();
        }
    }
}

这是gui:

public class GUI extends JFrame
{
    private Parlami p;
    private JPanel nord, centro;
    private JButton registra, leggi;
    private JTextArea display;

public static void main(String[] args)
{
    new GUI();
}

public GUI()
{
    p=new Parlami();
    initComponents();
}

private void initComponents()
{
    voce.SpeechInterface.init("./lib", true, true,"./lib/gram", "vocabolario");

    // N O R D
    nord=new JPanel();
    display=new JTextArea("");
    display.setForeground(Color.GREEN);
    display.setBackground(Color.BLACK);
    nord.setBackground(Color.BLACK);
    nord.add(display);

    // C E N T R O
    centro=new JPanel();
    registra=new JButton("tieni premuto per registrare");
    registra.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            Thread.currentThread().interrupt();// <-------- HERE I TRY TO AWAKE HIM
            display.setText(p.scrivi());
        }

    });

    centro.add(registra);
    leggi=new JButton("leggi");
    centro.add(leggi);

    this.setLayout(new BorderLayout());
    this.add(nord, BorderLayout.NORTH);
    this.add(centro, BorderLayout.CENTER);
    this.setSize(700,300);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setVisible(true);
    p.dormi(50000);  // <-------- HERE I TELL HIM TO SLEEP

}
}

Hovercraft F.. 5

如果调用Thread.sleepSwing事件线程,则会将整个应用程序置于睡眠状态,使其无效,但更重要的是,不需要执行此操作.您只需让ActionListener激活需要激活的对象,因为这是事件驱动编程的工作方式.

如果您需要在Swing应用程序中延迟,请使用Swing Timer,这是在本网站上反复讨论的内容.



1> Hovercraft F..:

如果调用Thread.sleepSwing事件线程,则会将整个应用程序置于睡眠状态,使其无效,但更重要的是,不需要执行此操作.您只需让ActionListener激活需要激活的对象,因为这是事件驱动编程的工作方式.

如果您需要在Swing应用程序中延迟,请使用Swing Timer,这是在本网站上反复讨论的内容.

推荐阅读
郑谊099_448
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有