优先重绘 GUI Thread.Sleep
Redraw GUI Prior Thread.Sleep
我认为这对我来说会简单得多,但我错过了这个概念。简而言之,我做了一个淡出音频的方法,并使用 Thread.Sleep 在 for 循环中让它 'wait' 。在调用该方法之前,我将几个标签和文本字段设置为不可见,但它们会等到线程休眠后才会更改。我试图将标签包装在一个 do-while 循环中以强制它们,甚至试图在它之后调用重绘,但它没有解决任何问题。任何建议将不胜感激:
编辑以包含 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();
}
}
你在这里屏蔽了美国东部时间。使用 SwingWorker 调用 AudioHandler1
new SwingWorker<Void,Void>()
{
@Override
protected Void doInBackground() throws Exception
{
AudioHandler1("Theme.wav",false,true);
return null;
}
}.execute();
我认为这对我来说会简单得多,但我错过了这个概念。简而言之,我做了一个淡出音频的方法,并使用 Thread.Sleep 在 for 循环中让它 'wait' 。在调用该方法之前,我将几个标签和文本字段设置为不可见,但它们会等到线程休眠后才会更改。我试图将标签包装在一个 do-while 循环中以强制它们,甚至试图在它之后调用重绘,但它没有解决任何问题。任何建议将不胜感激:
编辑以包含 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();
}
}
你在这里屏蔽了美国东部时间。使用 SwingWorker 调用 AudioHandler1
new SwingWorker<Void,Void>()
{
@Override
protected Void doInBackground() throws Exception
{
AudioHandler1("Theme.wav",false,true);
return null;
}
}.execute();