当我点击开始按钮时,我的计时器正在创建一个带有非工作按钮的新窗口。显示的更改仅在新窗口中发生,但启动/停止功能仅适用于旧窗口。我不确定这里发生了什么。有人可以帮我弄清楚如何在按下开始时阻止它创建新窗口吗?
我有三个班,我一起工作。第一个运行计时器。
package timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import java.awt.event.*;
public class TaskTimer extends TaskTimerWindow {
int seconds = 0;
int minutes = 0;
int hours = 0;
final int UNIT = 1000;
boolean stopped = false;
Timer timer;
public TaskTimer() {
ActionListener go = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
count();
}
};
timer = new Timer(UNIT, go);
timer.start();
}
public void resume() {
if(stopped) {
seconds = 0;
minutes = 0;
hours = 0;
}
}
public void end() {
timer.stop();
}
public void count() {
if(seconds < 59) {
seconds++;
} else if(minutes < 59) {
seconds = 0;
minutes++;
} else {
seconds = 0;
minutes = 0;
hours++;
}
changeDisplay(String.format("%02d", hours) + ":"
+ String.format("%02d", minutes) + ":"
+ String.format("%02d", seconds));
}
}
第二个创建并更新显示。
package timer;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TaskTimerWindow extends JFrame {
JLabel timeDisplay;
JButton start, stop, pause;
public TaskTimerWindow() {
JPanel timerWindow = new JPanel();
JPanel buttonSpace = new JPanel();
timeDisplay = new JLabel("00:00:00");
timeDisplay.setHorizontalAlignment((int) CENTER_ALIGNMENT);
timeDisplay.setFont(new Font("Arial", Font.PLAIN, 48));
timeDisplay.setBackground(Color.WHITE);
timerWindow.add(timeDisplay);
start = new JButton("Start");
stop = new JButton("Stop");
pause = new JButton("Pause");
buttonSpace.add(start);
buttonSpace.add(stop);
buttonSpace.add(pause);
getContentPane().add(timerWindow, BorderLayout.NORTH);
getContentPane().add(buttonSpace);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLocationRelativeTo(null);
setTitle("Task Timer");
pack();
}
public JLabel getText() {
return timeDisplay;
}
public void changeDisplay(String time) {
getText().setText(time);
}
}
第三个是控制器。
package timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ControlTimer {
boolean stopped = false;
boolean paused = false;
TaskTimer timer;
public ControlTimer() {
TaskTimerWindow window = new TaskTimerWindow();
window.start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
timer = new TaskTimer();
}
});
window.stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
stopped = true;
timer.end();
}
});
window.pause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
paused = true;
}
});
}
}
答案 0 :(得分:1)
所以主要问题出在你的ControlTimer
课程中。当TaskTimer
扩展TaskTimerWindow
时,每按一次 start 按钮就会创建新窗口。您应该修改它,如下所示:
public class ControlTimer {
boolean stopped = false;
boolean paused = false;
TaskTimer timer;
public ControlTimer() {
timer = new TaskTimer();
timer.start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
timer.resume();
timer.start();
}
});
timer.stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
timer.stopped = true;
timer.end();
}
});
timer.pause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
timer.end();
}
});
}
}
此外,TaskTimer
类还有一些细微的更改(从构造函数中移除timer.start()
并引入单独的start()
方法:
public TaskTimer() {
ActionListener go = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
count();
}
};
timer = new Timer(UNIT, go);
}
public void start() {
timer.start();
}