谁能解释一下为什么我的开始/停止按钮不起作用?这不是一个完全实现的秒表,但我被困在这里。任何帮助表示赞赏!这是我第一次在论坛发问题,所以如果我的帖子有任何问题,请告诉我。这是我的代码:
public class StopWatch {
private static boolean tiktok;
public static void setGo(boolean go) {
tiktok = go;
}
public static void main(String[] args) {
int counter = 0;
StopWatch stop = new StopWatch();
ClockFrame window = new ClockFrame("StopWatch");
JLabel lb = window.init();
while (true) {
lb.setText(Integer.toString(counter++));
if (counter == 61) {
counter = 0;
}
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
}
}
}
class ClockFrame extends JFrame {
JLabel hour, minus, sec;
public ClockFrame(String title) {
super(title);
}
JLabel init() {
JFrame frame = new JFrame("Stop Watch");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel time = new JPanel();
hour = new JLabel("0");
minus = new JLabel("0");
sec = new JLabel("0");
time.add(hour);
time.add(minus);
time.add(sec);
JPanel pane = new JPanel();
pane.setLayout(new FlowLayout());
JButton start = new JButton("Start");
start.addActionListener(new startstopActionListener(true));
JButton stop = new JButton("Stop");
stop.addActionListener(new startstopActionListener(false));
JButton reset = new JButton("Reset");
pane.add(start);
pane.add(stop);
pane.add(reset);
Container window = frame.getContentPane();
window.setLayout(new GridLayout(2, 1));
window.add(pane);
window.add(time);
frame.setSize(500, 200);
frame.setVisible(true);
return sec;
}
}
class startstopActionListener implements ActionListener {
private boolean b;
public startstopActionListener(boolean b) {
this.b = b;
}
@Override
public void actionPerformed(ActionEvent e) {
StopWatch.setGo(b);
}
}