好的,我现在已经尝试了一切。我仍然无法让我的计时器更新!......这是我到目前为止所拥有的......到目前为止我尝试过的事情:一个单独的线程(有效但我真的必须使用java.swing.Timer)计时器作为在下面看到它不起作用 java.util.timer。任何建议都会有帮助 import sudoku.Sudoku_Board;
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();
}
}
这是数独板文件,如果您想要它进行调试...
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() {
}