-1
我想在java中製作一個秒錶。線程之間的多線程和傳輸變量
我的第一個問題是,在第46行,我應該在一個新的線程啓動timerMethod,但我不知道如何做到這一點。
第二個問題是行44,45和53有錯誤,說「本地變量isTiming在封閉範圍內定義必須是最終的或有效的最終」。
代碼:
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Stopwatch {
public static void main(String[] args) {
windowMethod();
}
public static void windowMethod() {
//create frame
JFrame frame = new JFrame();
frame.setSize(900, 600);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//create panel
JPanel panel = new JPanel();
frame.add(panel);
panel.setLayout(null);
//create time label
JLabel timeLabel = new JLabel("00:00:00");
panel.add(timeLabel);
timeLabel.setFont(new Font("Tahoma",Font.PLAIN, 32));
Dimension timeLabelSize = timeLabel.getPreferredSize();
int timeLabelx = (450-(timeLabelSize.width/2));
timeLabel.setBounds(timeLabelx, 50, timeLabelSize.width, timeLabelSize.height);
//create start/stop button
JButton startStopButton = new JButton("Start Timer");
panel.add(startStopButton);
startStopButton.setFont(new Font("Tahoma",Font.PLAIN, 32));
Dimension startStopButtonSize = startStopButton.getPreferredSize();
int startStopButtonx = (450-(startStopButtonSize.width/2));
startStopButton.setBounds(startStopButtonx, 150, startStopButtonSize.width, startStopButtonSize.height);
boolean isTiming = false;
startStopButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (isTiming = false) { //This runs if the button is clicked to start the timer
isTiming = true;
timerMethod(isTiming); //runs the timerMethod in a new thread?
startStopButton.setText("Stop Timer");
Dimension startStopButtonSize = startStopButton.getPreferredSize();
int startStopButtonx = (450-(startStopButtonSize.width/2));
startStopButton.setBounds(startStopButtonx, 150, startStopButtonSize.width, startStopButtonSize.height);
}
else { //This runs if the button is clicked to stop the timer
isTiming = false;
startStopButton.setText("Start Timer");
Dimension startStopButtonSize = startStopButton.getPreferredSize();
int startStopButtonx = (450-(startStopButtonSize.width/2));
startStopButton.setBounds(startStopButtonx, 150, startStopButtonSize.width, startStopButtonSize.height);
}
}
});
}
public static String timerMethod(boolean isTiming) {
int milliseconds = 0;
int seconds = 0;
int minutes = 0;
while(isTiming = true) {
milliseconds++;
if (milliseconds > 999) {
milliseconds = 0;
seconds++;
if (seconds > 59) {
seconds = 0;
minutes++;
}
}
}
String outputTime = (minutes + ":" + seconds + ":" + milliseconds);
return outputTime;
}
}
[在線程上運行](https://stackoverflow.com/questions/3489543/how-to-call-a-method-with-a-separate-thread-in-java)和[解決局部變量範圍問題](https://stackoverflow.com/questions/25894509/problems-with-local-variable-scope-how-to-solve-it) – KernelMode
可能的重複[如何調用Java中的單獨線程的方法?](https://stackoverflow.com/questions/3489543/how-to-call-a-method-with-a-separate-thread-in-java) –