gpt4 book ai didi

java - GUI Action Listener JAVA 中的线程 sleep

转载 作者:行者123 更新时间:2023-11-30 02:10:24 25 4
gpt4 key购买 nike

我的线程 sleep 有问题。我正在编写简单的闹钟,它将设置 10 分钟,然后在标签(lab1)中倒计时。当我按下按钮时,它卡住了,仅此而已。问题出在哪里?到目前为止,我无法将线程作为主线程工作,这就是它是线性的原因。我可以问一个问题吗?如何执行shell命令? (抱歉,但它想让这个时间更长。但它也让我感兴趣);

public class Budik extends JFrame{
private JLabel lab1,lab2;
private JButton butt1,butt2,butt3;
private JTextField t1,t2;
private JSpinner spinner;
private JCheckBox cx1,cx2;
private JList list;
private JPanel p1,p2,p3,p4,p5;
private JPasswordField pass1,pass2;
public static void main(String[] args)
{
Budik okno = new Budik();
okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
okno.setVisible(true);
okno.pack();
okno.setLocationRelativeTo(null);
okno.setResizable(false);
}
public Budik() {
super("Countdown");
setLayout(new BorderLayout());
/////////////////////////////////////////////////////////////////
// p1
GridLayout el = new GridLayout(2,1);
p1 = new JPanel(el);
add(p1,BorderLayout.NORTH);
lab1 = new JLabel("Welcome",SwingConstants.CENTER); //centr labelu
lab2 = new JLabel("Created by WajFaj",SwingConstants.CENTER); //centr labelu
lab1.setFont(new Font("Serif", Font.PLAIN, 36)); //velikost fontu
p1.add(lab1);
p1.add(lab2);
//p2
p2 = new JPanel();
add(p2,BorderLayout.WEST);
butt1 = new JButton("START");
p2.add(butt1);

//p3
p3 = new JPanel();
add(p3,BorderLayout.CENTER);
butt2 = new JButton("STOP");
p3.add(butt2);



//p4
p4 = new JPanel();
add(p4,BorderLayout.EAST);
butt3 = new JButton("RESET");
p4.add(butt3);

//p5
p5 = new JPanel();
add(p5,BorderLayout.SOUTH);





butt1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){


for(int i=600; i>0;i-- ){
try{
Thread.sleep(1000);
String cc = ""+i;
lab1.setText(cc);
}
catch(InterruptedException e){
System.out.println("Chyba!");
}
}
}
});

最佳答案

我将在这里介绍我将如何做到这一点(进一步说它应该如何)。代码内的注释。

package test;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.TimeUnit;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Budik extends JFrame {
private static final int SLEEP_MINUTES = 10;
private long endTime;
private JLabel lab1, lab2;
private JButton butt1, butt2, butt3;
private JTextField t1, t2;
private JSpinner spinner;
private JCheckBox cx1, cx2;
private JList list;
private JPanel p1, p2, p3, p4, p5;
private JPasswordField pass1, pass2;
private Timer timer;

public static void main(String[] args) {
//All swing applications must run on EDT.
//Take a look https://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(()->{
Budik okno = new Budik();
okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
okno.pack();
okno.setLocationRelativeTo(null);
okno.setResizable(false);
okno.setVisible(true); //First pack, then setVisible(), recommended.
});
}

public Budik() {
super("Countdown");
setLayout(new BorderLayout());
/////////////////////////////////////////////////////////////////
// p1
GridLayout el = new GridLayout(2, 1);
p1 = new JPanel(el);
add(p1, BorderLayout.NORTH);
lab1 = new JLabel("Welcome", SwingConstants.CENTER); // centr labelu
lab2 = new JLabel("Created by WajFaj", SwingConstants.CENTER); // centr labelu
lab1.setFont(new Font("Serif", Font.PLAIN, 36)); // velikost fontu
p1.add(lab1);
p1.add(lab2);
// p2
p2 = new JPanel();
add(p2, BorderLayout.WEST);
butt1 = new JButton("START");
p2.add(butt1);

// p3
p3 = new JPanel();
add(p3, BorderLayout.CENTER);
butt2 = new JButton("STOP");
p3.add(butt2);

// p4
p4 = new JPanel();
add(p4, BorderLayout.EAST);
butt3 = new JButton("RESET");
p4.add(butt3);

// p5
p5 = new JPanel();
add(p5, BorderLayout.SOUTH);

butt1.addActionListener(e->{
endTime = System.currentTimeMillis()+TimeUnit.MINUTES.toMillis(SLEEP_MINUTES); //define the end time
timer.start(); //Start the timer (in stop button, just add timer.stop())
});
//Initialize timer.
timer = new Timer(1000, e->{ //Loop every 1000ms a.k.a 1second
long millisBeforeEnd = endTime - System.currentTimeMillis();
if (millisBeforeEnd < 0) {
timer.stop();
lab1.setText("Welcome"); //Restore the text to your label, or write whatever you want.
return;
}
long secsBeforeEnd = TimeUnit.MILLISECONDS.toSeconds(millisBeforeEnd); //Convert the millis to seconds.
lab1.setText(secsBeforeEnd+"");
//If you want some kind of pretty print, uncomment the code below.
/*
int hours = (int) TimeUnit.MILLISECONDS.toHours(millisBeforeEnd);
int mins = (int) (TimeUnit.MILLISECONDS.toMinutes(millisBeforeEnd) - hours * 60);
int secs = (int) (TimeUnit.MILLISECONDS.toSeconds(millisBeforeEnd) - mins * 60 - hours * 60 * 60);
lab1.setText(String.format("%02d:%02d:%02d", hours, mins, secs));
*/
});
timer.setInitialDelay(0); //Don't loose the first second.
}
}

关于java - GUI Action Listener JAVA 中的线程 sleep ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50280166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com