所以我正在制作这款游戏,让你记住一个模式并重新创建它。图形第一次运行良好,但第二次运行不正常。我尝试使用Swing Timer但它对我不起作用(也许我没有正确使用它,无论我还需要帮助)
public static void main(String args[]) {
frame.getContentPane().setBackground(new Color(255, 218, 185));
frame.setBounds(100, 100, 400, 600);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
Counter = 0;
//
JButton btnScore = new JButton("Time Left: ");
btnScore.setHorizontalAlignment(SwingConstants.LEFT);
btnScore.setFont(new Font("Tahoma", Font.PLAIN, 26));
//
btnScore.setBounds(86, 113, 207, 73);
frame.getContentPane().add(btnScore);
//
JButton btnHighScore = new JButton("High Score: ");
btnHighScore.setHorizontalAlignment(SwingConstants.LEFT);
btnHighScore.setFont(new Font("Tahoma", Font.PLAIN, 16));
btnHighScore.setBounds(86, 455, 207, 50);
frame.getContentPane().add(btnHighScore);
//
Scorebutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
//
Scorebutton.setHorizontalAlignment(SwingConstants.LEFT);
Scorebutton.setFont(new Font("Tahoma", Font.PLAIN, 16));
Scorebutton.setBounds(86, 398, 207, 50);
frame.getContentPane().add(Scorebutton);
//
A.setBackground(Color.GRAY);
A.setContentAreaFilled(false);
A.setOpaque(true);
//
A.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (code[Counter] == 1) {
Score++;
} else {
End();
}
Counter++;
if (Counter == Size) {
Size++;
n++;
frame.setVisible(true);
main(args);
}
}
});
A.setBounds(86, 187, 89, 73);
frame.getContentPane().add(A);
//
B.setBackground(Color.GRAY);
B.setContentAreaFilled(false);
B.setOpaque(true);
B.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (code[Counter] == 2) {
Score++;
} else {
End();
}
Counter++;
if (Counter == Size) {
Size++;
n++;
frame.setVisible(true);
main(args);
}
}
});
//
B.setBounds(204, 187, 89, 73);
frame.getContentPane().add(B);
//
C.setBackground(Color.GRAY);
C.setContentAreaFilled(false);
C.setOpaque(true);
C.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (code[Counter] == 3) {
Score++;
} else {
End();
}
Counter++;
if (Counter == Size) {
Size++;
n++;
frame.setVisible(true);
main(args);
}
}
});
//
C.setBounds(86, 286, 89, 73);
frame.getContentPane().add(C);
//
D.setBackground(Color.GRAY);
D.setContentAreaFilled(false);
D.setOpaque(true);
D.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (code[Counter] == 4) {
Score++;
} else {
End();
}
Counter++;
}
});
//
D.setBounds(204, 286, 89, 73);
frame.getContentPane().add(D);
//
frame.setVisible(true);
//
try {
generate();
} catch (InterruptedException e1) {
}
}
public static void generate() throws InterruptedException {
Counter = 0;
rn = new Random();
while (Counter != Size) {
int Number = rn.nextInt(4) + 1;
code[Counter] = (Number);
Counter++;
}
Counter = 0;
while (Counter != Size) {
System.out.println(code[Counter]);
Counter++;
}
Counter = 0;
Show();
}
public static int x = 0, z = 0;
public static void Show() throws InterruptedException {
timer1 = new Timer(250, new ActionListener() {// Timer 4 seconds
public void actionPerformed(ActionEvent e) {
if (Counter == Size) {
Size++;
n++;
frame.setVisible(true);
main(null);
}
}
});
if (Size != Counter) {
Go();
}
if (Size == Counter) {
Counter = 0;
timer1.start();
}
}`public static void Go() throws InterruptedException {
if (code[Counter] == 1) {
System.out.println("worked");
Thread.sleep(500);
A.setBackground(Color.RED);
Thread.sleep(500);
A.setBackground(Color.GRAY);
Counter++;
}
if (code[Counter] == 2) {
System.out.println("worked");
Thread.sleep(500);
B.setBackground(Color.RED);
Thread.sleep(500);
B.setBackground(Color.GRAY);
Counter++;
}
if (code[Counter] == 3) {
System.out.println("worked");
Thread.sleep(500);
C.setBackground(Color.RED);
Thread.sleep(500);
C.setBackground(Color.GRAY);
Counter++;
}
if (code[Counter] == 4) {
System.out.println("worked");
Thread.sleep(500);
D.setBackground(Color.RED);
Thread.sleep(500);
D.setBackground(Color.GRAY);
Counter++;
}
Show();
}`