有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java多线程和线程间变量传输

我想用爪哇做秒表

我的第一个问题是,在第46行,我应该在一个新的线程中启动timerMethod,但我不知道如何做到这一点

第二个问题是,第44行、第45行和第53行有错误,即“在封闭范围内定义的局部变量必须是final或实际上是final”

代码:

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;
    }
}

共 (1) 个答案