gpt4 book ai didi

java - 在倒计时器上使用 windowFocusListener

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

我正在尝试制作一个倒计时器,仅当窗口位于屏幕顶部时才运行。

我尝试过这个:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class TimerVisible extends JFrame implements WindowFocusListener{
static TimerVisible frame = new TimerVisible("chrono",2,1,3);//I set a random time
JTextArea display;
private Counter counter;

public static void main(String[] args) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addComponentsToPane();
frame.pack();
frame.setVisible(true);
}

private void addComponentsToPane() {
display = new JTextArea();
display.setEditable(true);
JScrollPane scrollPane = new JScrollPane(display);
scrollPane.setPreferredSize(new Dimension(500, 450));
getContentPane().add(scrollPane, BorderLayout.CENTER);
addWindowFocusListener(this);
}

public TimerVisible(String name, int hours, int minutes, int secondes) {
super(name);
counter=new Counter(hours, minutes, secondes); //Counter is in secondes but is created with hours, minutes and seconds
}


public void windowGainedFocus(WindowEvent e) {
displayMessage("WindowFocusListener method called: windowGainFocus.");
try{
while(counter.getCounter()!=0){
Thread.sleep(1000);
displayMessage(counter.toString());
counter.decrement();
}
}
catch(InterruptedException exc){
System.exit(-1);
}
}

public void windowLostFocus(WindowEvent e) {
displayMessage("WindowFocusListener method called: windowLostFocus.");
}

private void displayMessage(String msg) {
display.append(msg+"\n");
System.out.println(msg);
}
}

当我运行这个程序时,它会在我的终端上显示消息和倒计时,而不是在窗口上,但是如果我在注释下设置 while 循环,它会在窗口上正确显示消息。有人知道为什么我会出现这种差异吗?

谢谢

最佳答案

您的 while 循环在 Swing 事件线程上运行,阻止它并阻止它绘制到 GUI 或与用户交互。使用Swing Timer反而。请注意,使用 Swing 计时器,您不会有 while 循环,而是会重复调用 actionPerformed,直到您停止计时器。

这样的东西可能接近工作(代码未测试)

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.Timer;

public class TimerVisible extends JFrame implements WindowFocusListener{
private static final int TIMER_DELAY = 1000;
static TimerVisible frame = new TimerVisible("chrono",2,1,3);//I set a random time
JTextArea display;
private Counter counter;
Timer timer = null;

public static void main(String[] args) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addComponentsToPane();
frame.pack();
frame.setVisible(true);
}

private void addComponentsToPane() {
display = new JTextArea();
display.setEditable(true);
JScrollPane scrollPane = new JScrollPane(display);
scrollPane.setPreferredSize(new Dimension(500, 450));
getContentPane().add(scrollPane, BorderLayout.CENTER);
addWindowFocusListener(this);
}

public TimerVisible(String name, int hours, int minutes, int secondes) {
super(name);
counter=new Counter(hours, minutes, secondes); //Counter is in secondes but is created with hours, minutes and seconds
}


public void windowGainedFocus(WindowEvent e) {
displayMessage("WindowFocusListener method called: windowGainFocus.");
if (timer != null && timer.isRunning()) {
return;
}

timer = new Timer(TIMER_DELAY, new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
if (counter.getCounter() <= 0) {
timer.stop();
} else {
displayMessage(counter.toString());
counter.decrement();
}

}
});
timer.start();
}

public void windowLostFocus(WindowEvent e) {
displayMessage("WindowFocusListener method called: windowLostFocus.");
}

private void displayMessage(String msg) {
display.append(msg+"\n");
System.out.println(msg);
}
}

关于java - 在倒计时器上使用 windowFocusListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37775132/

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