如何使用eclipse在后台进行倒计时运行
how to make countdown running in background using eclipse
这是 problem.i 倒计时时无法单击按钮 running.i 必须等到倒计时 stop.My 问题是如何在 运行 中进行倒计时 运行 =19=] 任何 suggestion.please 帮助我!
public static void main(String args[]){
scrbutton myWindow = new scrbutton(); //set window design
myWindow.setSize(300,70);
myWindow.setVisible(true);
myWindow.setResizable(false);
}
public scrbutton() {
super("Clicker"); //Title
setLayout(new FlowLayout());
addWindowListener(this);
add(kotak);
add(kotak2); //add and design you components
add(kotak3);
add(enter);
enter.addActionListener(this);
kotak.setText("0");
kotak2.setText("Times remaining: 60");
kotak.setEditable(false);
kotak2.setEditable(false);
kotak3.setEditable(false);
}
public void actionPerformed(ActionEvent e) //What will run through the program?
{
click++;
kotak.setText("\r"+click); //display number of click
if (click >=10){
kotak3.setText("You Win!");
enter.setEnabled(false);
}else{
kotak3.setText("try again");
}
for(int x=60; x>=0; x--)
{System.out.print(x+"\r"); // use print than println if you use (/r).
try {Thread.sleep(100);} // 1000ms=1second thus its sleep(delay) 1 second between each iteration.
catch (InterruptedException e1){}
}
}
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
public void windowOpened(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
}
我要在这里冒险,因为我不这样做 Java,但我会说你需要让应用程序是多线程的 - 你需要你的主线程不会被定时器的执行线程阻塞。创建一个带有回调的新线程以增加计时器,启动线程,然后在不再需要时终止线程。
首先,您必须从事件处理程序中删除带有 sleep() 的 for() 循环。当该代码正在执行时,您的用户界面将无响应。
尝试使用 javax.swing.Timer 设置一个每 1 秒触发一次的计时器。然后在计时器的事件处理程序中执行构成 'counting down'.
的任何操作
int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// do whatever constitutes counting down here
}
};
new Timer(delay, taskPerformer).start();
这是 problem.i 倒计时时无法单击按钮 running.i 必须等到倒计时 stop.My 问题是如何在 运行 中进行倒计时 运行 =19=] 任何 suggestion.please 帮助我!
public static void main(String args[]){
scrbutton myWindow = new scrbutton(); //set window design
myWindow.setSize(300,70);
myWindow.setVisible(true);
myWindow.setResizable(false);
}
public scrbutton() {
super("Clicker"); //Title
setLayout(new FlowLayout());
addWindowListener(this);
add(kotak);
add(kotak2); //add and design you components
add(kotak3);
add(enter);
enter.addActionListener(this);
kotak.setText("0");
kotak2.setText("Times remaining: 60");
kotak.setEditable(false);
kotak2.setEditable(false);
kotak3.setEditable(false);
}
public void actionPerformed(ActionEvent e) //What will run through the program?
{
click++;
kotak.setText("\r"+click); //display number of click
if (click >=10){
kotak3.setText("You Win!");
enter.setEnabled(false);
}else{
kotak3.setText("try again");
}
for(int x=60; x>=0; x--)
{System.out.print(x+"\r"); // use print than println if you use (/r).
try {Thread.sleep(100);} // 1000ms=1second thus its sleep(delay) 1 second between each iteration.
catch (InterruptedException e1){}
}
}
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
public void windowOpened(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
}
我要在这里冒险,因为我不这样做 Java,但我会说你需要让应用程序是多线程的 - 你需要你的主线程不会被定时器的执行线程阻塞。创建一个带有回调的新线程以增加计时器,启动线程,然后在不再需要时终止线程。
首先,您必须从事件处理程序中删除带有 sleep() 的 for() 循环。当该代码正在执行时,您的用户界面将无响应。
尝试使用 javax.swing.Timer 设置一个每 1 秒触发一次的计时器。然后在计时器的事件处理程序中执行构成 'counting down'.
的任何操作 int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// do whatever constitutes counting down here
}
};
new Timer(delay, taskPerformer).start();