重绘GUI先前Thread.Sleep

时间:2016-10-29 10:54:06

标签: java eclipse windows multithreading thread-sleep

我认为这对我来说简单得多,但我错过了这个概念。简而言之,我制作了一种消除音频的方法,并使用Thread.Sleep让它在for循环中“等待”。在调用该方法之前,我将几个标签和文本字段设置为不可见,但它们等到线程休眠后才更改。我试图将标签包装在do-while循环中以强制它们,甚至尝试在它之后调用redraw,但它没有修复任何东西。任何建议将不胜感激:

编辑包含ActionPerformed:

            public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == quit)
        {
            System.exit(0);
        }
        else if(e.getSource() == play)
        {
            System.out.println("PLAY");
            quit.setVisible(false);
            quit.setEnabled(false);
            play.setVisible(false);
            play.setEnabled(false);
            GameActivated();
        }
        else if(e.getSource() == submitTeams)
        {
            teamOneName = teamOneTF.getText();
            teamTwoName = teamTwoTF.getText();

            Swinpublic void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == quit)
        {
            System.exit(0);
        }
        else if(e.getSource() == play)
        {
            System.out.println("PLAY");
            quit.setVisible(false);
            quit.setEnabled(false);
            play.setVisible(false);
            play.setEnabled(false);
            GameActivated();
        }
        else if(e.getSource() == submitTeams)
        {
            teamOneName = teamOneTF.getText();
            teamTwoName = teamTwoTF.getText();

            SwingUtilities.invokeLater(new Runnable(){
                public void run(){
                               teamOneLabel.setVisible(false);
                               teamOneTF.setVisible(false);
                               teamTwoLabel.setVisible(false);
                               teamTwoTF.setVisible(false);
                               submitTeams.setVisible(false);
                }
                });

        }
        else
            System.out.println("Error: Button pressed with no known source.");
    }

下面是正在调用的AudioHandler1的内部:

            for(int count = 0; count < 71; count++)
        {
            gainControl.setValue((float) count*-1);
            System.out.println("Waiting!" + Math.random());
            try 
                {
                Thread.sleep(50);
                } 
            catch(InterruptedException ex) 
                {
                    Thread.currentThread().interrupt();
                }

        }

1 个答案:

答案 0 :(得分:1)

你在这里阻止了EDT。使用SwingWorker来调用AudioHandler1

        new SwingWorker<Void,Void>()
        {
        @Override
        protected Void doInBackground() throws Exception 
            {
            AudioHandler1("Theme.wav",false,true);
            return null;
            }
        }.execute();