我想以某种方式添加延迟,单击按钮更新标签,休眠几秒钟,然后更新其他标签
I want to add a delay in a way, on button click update a label, sleep for few seconds and then update other labels
这是按钮的侦听器。
dice.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String randomNum = String.valueOf(randomNumber);
rand.setText(" Move forward "+randomNum+" boxes"); //rand is a label
try {
Thread.sleep(6000); //1000 milliseconds is one second.
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
//here some handling with randomNumber
Position[playerTurn].setText(posi);
} //end of actionlistener
我想在 rand.setText
和 Position[playerTurn].setText
之间添加睡眠,但它没有按预期工作。它首先等待然后立即设置我不想要的两个标签。
您可以使用Thread.sleep(毫秒);被 try catch 语句包围。
但更好的方法是使用线程。
此处提供了一些示例代码,这将对您有所帮助。
//declare a globalcounter and boolean like this;
private int counttalk=0;
private boolean mySpeakanim=false;
在您的代码的 oncreate 中调用此方法;
speakAnim();
//你想为你的标签开始动画的地方做这个
countalk=0;
mySpeakanim=true;
private void speakAnim() {
// gonelayout.setVisibility(View.GONE);
Handler handler1 = new Handler();
handler1.postDelayed(new Runnable() {
@Override
public void run() {
if (mySpeakAnim) {
Log.d("SpeakAnim", "SpeakAnim");
counttalk += 1;
if (counttalk == 1) {
label.setText("Your Label1");
} else if (counttalk == 2) {
label.setText("Your Label2");
} else if (counttalk == 3) {
label.setText("Your Label3");
} else if (counttalk == 4) {
label.setText("Your Label4");
counttalk = 0;
}
}
speakAnim();
}
}, 100L);
}
您可以根据需要更改线程持续时间 100L 和动画计数器。
EventQueueclass提供了两个方法invokeLater和invokeAndWait,方便以后调用
package com.makesh.sandbox;
import java.awt.Button;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class HelloWorldSwing implements ActionListener {
private Button button;
private JLabel delayedLabel;
private JPanel contentPane;
private JPanel myPanel;
private void createAndShowGUI() {
JFrame frame = new JFrame("HelloWorldSwing");
frame.getContentPane().setSize(800,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setTitle("My Program");
contentPane = new JPanel();
contentPane.setOpaque(true);
contentPane.setBackground(Color.WHITE);
contentPane.setLayout(new GridBagLayout());
contentPane.setBorder(BorderFactory.createTitledBorder("My Program"));
myPanel = new JPanel();
myPanel.setOpaque(true);
myPanel.setBorder(BorderFactory.createTitledBorder("Login"));
myPanel.setLayout(new GridLayout(2, 2, 20, 20));
button = new Button(" Click Me please to display text after timer sleep");
delayedLabel = new JLabel("Label 2");
myPanel.add(button);
myPanel.add(delayedLabel);
contentPane.add(myPanel);
frame.setContentPane(contentPane);
button.addActionListener(this);
// Display the window.
frame.pack();
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
try {
Thread.sleep(6000); // 1000 milliseconds is one second.
EventQueue.invokeLater(new Worker());
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
private final class Worker implements Runnable {
@Override
public void run() {
try {
Thread.sleep(600);
delayedLabel.setText(" Modified label 2 after sleep");
} catch (InterruptedException ex) {
}
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
HelloWorldSwing helloWorldSwing = new HelloWorldSwing();
helloWorldSwing.createAndShowGUI();
}
});
}
}
这是因为还没有调用repaint。所以在后台你正在改变 rand 文本,它等待 6 秒,然后它会设置 Position 文本。但是中间没有重绘,所以他们会同时绘制。
dice.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String randomNum = String.valueOf(randomNumber);
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
Thread.sleep(6000);
}
catch(Exception ie){}
Position[playerTurn].setText(posi);
}
});
}
});
这将启动一个新线程,这样将允许在您的主 AWT 线程中立即重新绘制第一个标签,然后您将在此处获得一个分支线程,该线程将在 6 秒内触发。但是,如果在 Runnable 中捕获到异常,您将必须确定要执行的操作。
编辑
注意到您在侦听器中声明字符串,这将成为此代码的问题。您需要将其声明为最终变量或实例变量。
这是按钮的侦听器。
dice.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String randomNum = String.valueOf(randomNumber);
rand.setText(" Move forward "+randomNum+" boxes"); //rand is a label
try {
Thread.sleep(6000); //1000 milliseconds is one second.
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
//here some handling with randomNumber
Position[playerTurn].setText(posi);
} //end of actionlistener
我想在 rand.setText
和 Position[playerTurn].setText
之间添加睡眠,但它没有按预期工作。它首先等待然后立即设置我不想要的两个标签。
您可以使用Thread.sleep(毫秒);被 try catch 语句包围。 但更好的方法是使用线程。 此处提供了一些示例代码,这将对您有所帮助。
//declare a globalcounter and boolean like this;
private int counttalk=0;
private boolean mySpeakanim=false;
在您的代码的 oncreate 中调用此方法;
speakAnim();
//你想为你的标签开始动画的地方做这个
countalk=0;
mySpeakanim=true;
private void speakAnim() {
// gonelayout.setVisibility(View.GONE);
Handler handler1 = new Handler();
handler1.postDelayed(new Runnable() {
@Override
public void run() {
if (mySpeakAnim) {
Log.d("SpeakAnim", "SpeakAnim");
counttalk += 1;
if (counttalk == 1) {
label.setText("Your Label1");
} else if (counttalk == 2) {
label.setText("Your Label2");
} else if (counttalk == 3) {
label.setText("Your Label3");
} else if (counttalk == 4) {
label.setText("Your Label4");
counttalk = 0;
}
}
speakAnim();
}
}, 100L);
}
您可以根据需要更改线程持续时间 100L 和动画计数器。
EventQueueclass提供了两个方法invokeLater和invokeAndWait,方便以后调用
package com.makesh.sandbox;
import java.awt.Button;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class HelloWorldSwing implements ActionListener {
private Button button;
private JLabel delayedLabel;
private JPanel contentPane;
private JPanel myPanel;
private void createAndShowGUI() {
JFrame frame = new JFrame("HelloWorldSwing");
frame.getContentPane().setSize(800,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setTitle("My Program");
contentPane = new JPanel();
contentPane.setOpaque(true);
contentPane.setBackground(Color.WHITE);
contentPane.setLayout(new GridBagLayout());
contentPane.setBorder(BorderFactory.createTitledBorder("My Program"));
myPanel = new JPanel();
myPanel.setOpaque(true);
myPanel.setBorder(BorderFactory.createTitledBorder("Login"));
myPanel.setLayout(new GridLayout(2, 2, 20, 20));
button = new Button(" Click Me please to display text after timer sleep");
delayedLabel = new JLabel("Label 2");
myPanel.add(button);
myPanel.add(delayedLabel);
contentPane.add(myPanel);
frame.setContentPane(contentPane);
button.addActionListener(this);
// Display the window.
frame.pack();
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
try {
Thread.sleep(6000); // 1000 milliseconds is one second.
EventQueue.invokeLater(new Worker());
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
private final class Worker implements Runnable {
@Override
public void run() {
try {
Thread.sleep(600);
delayedLabel.setText(" Modified label 2 after sleep");
} catch (InterruptedException ex) {
}
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
HelloWorldSwing helloWorldSwing = new HelloWorldSwing();
helloWorldSwing.createAndShowGUI();
}
});
}
}
这是因为还没有调用repaint。所以在后台你正在改变 rand 文本,它等待 6 秒,然后它会设置 Position 文本。但是中间没有重绘,所以他们会同时绘制。
dice.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String randomNum = String.valueOf(randomNumber);
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
Thread.sleep(6000);
}
catch(Exception ie){}
Position[playerTurn].setText(posi);
}
});
}
});
这将启动一个新线程,这样将允许在您的主 AWT 线程中立即重新绘制第一个标签,然后您将在此处获得一个分支线程,该线程将在 6 秒内触发。但是,如果在 Runnable 中捕获到异常,您将必须确定要执行的操作。
编辑
注意到您在侦听器中声明字符串,这将成为此代码的问题。您需要将其声明为最终变量或实例变量。