Java 无法获取计时器标签以显示时间

Java 无法获取计时器标签以显示时间,java,swing,timer,runnable,Java,Swing,Timer,Runnable,好的,我已经试过了。我仍然无法让我的计时器更新!。。。这是我到目前为止得到的。。 到目前为止我已经尝试过的事情: 一个单独的线程可以工作,但我必须使用java.swing.Timer 如下图所示,计时器不工作 java.util.timer。 任何建议都会有帮助 进口数独板 import java.awt.Color; import java.awt.Font; import java.awt.Rectangle; import java.awt.event.ActionEvent; impor

好的,我已经试过了。我仍然无法让我的计时器更新!。。。这是我到目前为止得到的。。 到目前为止我已经尝试过的事情: 一个单独的线程可以工作,但我必须使用java.swing.Timer 如下图所示,计时器不工作 java.util.timer。 任何建议都会有帮助 进口数独板

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.InputVerifier;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.Timer;

public class Sudoku_GUI extends JFrame {

    class NumberedField extends JTextField {
        public int row = -1, col = -1;
        public void setRowAndCol(int _row, int _col) {
            row = _row;
            col = _col;
        }
    }

    private static final long serialVersionUID = 1L;
    boolean paused = false;
    Timer timer = null;
    int minutes_elapsed = 0, seconds_elapsed = 0;
    NumberedField field[][];
    Sudoku_Board Puzzle_Board, Solution_Board;
    JLabel time_label, time;
    JButton pause;
    public Sudoku_GUI() {

        super("fokleSudoku");
        setSize(420, 480);
        setResizable(false);

        Solution_Board = new Sudoku_Board();
        Puzzle_Board = new Sudoku_Board();

        time_label = new JLabel("Time: ");
        time_label.setBounds(new Rectangle(20, 8, 100, 20));
        add(time_label);
        time_label.setVisible(false);

        time = new JLabel();
        time.setBounds(new Rectangle(60, 8, 100, 20));
        time.setVisible(false);
        time.setForeground(Color.BLUE);
        add(time);

        JButton new_game = new JButton("New Game");
        new_game.setBounds(new Rectangle(20, 400, 100, 30));
        new_game.setBackground(Color.BLACK);
        new_game.setForeground(Color.WHITE);
        add(new_game);

        new_game.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                Solution_Board.generate();
                Puzzle_Board.random_remove(Solution_Board);
                apply_fields(Puzzle_Board);
                time_label.setVisible(true);
                pause.setVisible(true);
                if(paused)
                    unpause();
                start_timer(0, 0);

            }

        });

        pause = new JButton("Pause");
        pause.setBounds(new Rectangle(280, 400, 100, 30));
        pause.setBackground(Color.red);
        pause.setForeground(Color.WHITE);
        pause.setVisible(false);
        pause.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                if(paused){
                    unpause();
                    start_timer(minutes_elapsed, seconds_elapsed);
                }
                else {
                    pause();
                }

            }
        });
        add(pause);

        setLayout(null);

        int x = 20, y = 35;
        field = new NumberedField[9][9];
        for (int row = 0; row < 9; row++) {
            for (int col = 0; col < 9; col++) {
                field[row][col] = new NumberedField();
                field[row][col].setBounds(x, y, 40, 40);
                field[row][col].setFont(new Font(Font.MONOSPACED, Font.BOLD, 20));
                field[row][col].setRowAndCol(row, col);
                field[row][col].setHorizontalAlignment(JTextField.CENTER);
                field[row][col].setInputVerifier(new InputVerifier() {
                    public boolean verify(JComponent arg0) {
                        NumberedField curr_field = (NumberedField) arg0;
                        if (curr_field.getText().length() != 1)
                            curr_field.setText("");
                        else if (!(curr_field.getText().charAt(0) >= '0' && curr_field
                                .getText().charAt(0) <= '9'))
                            curr_field.setText("");
                        else if (curr_field.getText().charAt(0) >= '0'
                                && curr_field.getText().charAt(0) <= '9') {
                            final int temp[] = Puzzle_Board.validValue(
                                    Integer.parseInt(curr_field.getText()),
                                    curr_field.row, curr_field.col);
                            if (temp[0] != 808 && temp[1] != -1) {
                                new Thread(new Runnable() {
                                    public void run() {
                                        Color original_color = field[temp[0]][temp[1]]
                                                .getBackground();
                                        field[temp[0]][temp[1]]
                                                .setBackground(Color.RED);
                                        try {
                                            Thread.sleep(2000);
                                        } catch (InterruptedException e) {
                                        }
                                        field[temp[0]][temp[1]]
                                                .setBackground(original_color);
                                    }

                                }).start();

                            } else {
                                Puzzle_Board.setValue(
                                        Integer.parseInt(curr_field.getText()),
                                        curr_field.row, curr_field.col);
                            }
                        }
                        return true;

                    }

                });
                add(field[row][col]);
                x += 40;

                if (col % 3 == 0 && (row) % 3 == 0)
                    field[row][col].setBorder(BorderFactory.createMatteBorder(
                            2, 2, 1, 1, Color.BLACK));
                else if ((col + 1) % 3 == 0 && (row + 1) % 3 == 0)
                    field[row][col].setBorder(BorderFactory.createMatteBorder(
                            1, 1, 2, 2, Color.BLACK));
                else if (col % 3 == 0 && (row + 1) % 3 == 0)
                    field[row][col].setBorder(BorderFactory.createMatteBorder(
                            1, 2, 2, 1, Color.BLACK));
                else if ((col + 1) % 3 == 0 && row % 3 == 0)
                    field[row][col].setBorder(BorderFactory.createMatteBorder(
                            2, 1, 1, 2, Color.BLACK));
                else if (row % 3 == 0)
                    field[row][col].setBorder(BorderFactory.createMatteBorder(
                            2, 1, 1, 1, Color.BLACK));
                else if ((row + 1) % 3 == 0)
                    field[row][col].setBorder(BorderFactory.createMatteBorder(
                            1, 1, 2, 1, Color.BLACK));
                else if (col % 3 == 0)
                    field[row][col].setBorder(BorderFactory.createMatteBorder(
                            1, 2, 1, 1, Color.BLACK));
                else if ((col + 1) % 3 == 0)
                    field[row][col].setBorder(BorderFactory.createMatteBorder(
                            1, 1, 1, 2, Color.BLACK));
                else
                    field[row][col].setBorder(BorderFactory.createMatteBorder(
                            1, 1, 1, 1, Color.BLACK));
                if ((col + 1) % 9 == 0) {
                    y += 40;
                    x = 20;
                }

            }
        }

        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public void apply_fields(Sudoku_Board sudoku_board) {
        empty_fields();
        for (int row = 0; row < 9; row++) {
            for (int col = 0; col < 9; col++) {
                if (sudoku_board.getValue(row, col) != -255) {
                    field[row][col].setText(String.valueOf(sudoku_board
                            .getValue(row, col)));
                    field[row][col].setEditable(false);
                }
            }
        }
    }

    public void fill_answer() {
        apply_fields(Solution_Board);
    }

    public void empty_fields() {
        for (int row = 0; row < 9; row++) {
            for (int col = 0; col < 9; col++) {
                field[row][col].setText("");
                field[row][col].setBackground(Color.WHITE);
                field[row][col].setEditable(true);
            }
        }
    }

    public void start_timer(int minutes, int seconds){
        if(timer == null){
             timer = new Timer(0, new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    time.setText(String.format("%02d:%02d", minutes_elapsed,seconds_elapsed));
                    seconds_elapsed++;
                    if(seconds_elapsed == 60){
                        seconds_elapsed = 0;
                        minutes_elapsed++;
                    }
                }
             });
             timer.setDelay(1000);
             timer.setRepeats(true);
        }
             timer.start();
    }

    public void stop_timer(){
        if(timer != null){
            timer.stop();
        }
    }

    public void pause(){
        if(!paused){
            paused = true;
            pause.setText("Resume");
            stop_timer();
            empty_fields();
        }
    }

    public void unpause(){   // DOES NOT START THE TIMER !! -- NEEDED IN SETTING UP A NEW GAME.
        if(paused){
            paused = false;
            pause.setText("Pause");
            apply_fields(Puzzle_Board);
        }
    }

    public static void main(String... args) throws InterruptedException {
        new Sudoku_GUI();
    }
}

您有一个time.setVisiblefalse;但是没有时间。setVisibletrue;。似乎您正在使显示时间的标签:可见,但显示实际时间的标签从未可见。

您有一个时间。setVisiblefalse;但是没有时间。setVisibletrue;。似乎你正在制作显示时间的标签:可视,但显示实际时间的标签从来都不可见。

哦,我的上帝!我应该马上自杀!在这个计时器问题上浪费了好几个小时,好几个小时,好几个小时!嘎@user1123599:在您的开发生涯的剩余时间里,类似这样的错误将继续困扰您。。。只希望你将来所有的问题都能有一个像这样简单的解决方案;哦,我的上帝!我应该马上自杀!在这个计时器问题上浪费了好几个小时,好几个小时,好几个小时!嘎@user1123599:在您的开发生涯的剩余时间里,类似这样的错误将继续困扰您。。。只希望你将来所有的问题都能有一个像这样简单的解决方案;
package sudoku;

import java.util.ArrayList;
import java.util.Random;

public class Sudoku_Board {
    private int Board[][];

    public Sudoku_Board() {
        Board = new int[9][9];
        clear();
    }

    public void print() {

    }

    public int[] validValue(int value, int row, int col) { // (-1,-1) invalid
        int index[] = {-1,-1};
        return index;
    }

    public void random_remove(Sudoku_Board obj) {

    }

    public void clear() {

    }
    public void generate() {

}