Java 第一次图形效果很好,但第二次或第三次帧冻结
所以我在做这个游戏,让你记住一个模式并重新创建它。图形在第一次运行良好,但在第二次运行不正常。我尝试使用Swing定时器,但它对我不起作用(也许我没有正确使用它,尽管我仍然需要帮助)Java 第一次图形效果很好,但第二次或第三次帧冻结,java,swing,Java,Swing,所以我在做这个游戏,让你记住一个模式并重新创建它。图形在第一次运行良好,但在第二次运行不正常。我尝试使用Swing定时器,但它对我不起作用(也许我没有正确使用它,尽管我仍然需要帮助) 1) Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或与布局填充和边框一起使用。2) 为了更快地获得更好的帮助,请发布或。。。3) 请学习常见的Java命名法(命名约定-例如,EachWordUpperCaseC
1) Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或与布局填充和边框一起使用。2) 为了更快地获得更好的帮助,请发布或。。。3) 请学习常见的Java命名法(命名约定-例如,
EachWordUpperCaseClass
,firstWordLowerCaseMethod()
,firstWordLowerCaseAttribute
,除非它是一个大写常量
),并一致地使用它。4)还应与上面@andrewhompson建议的Java命名法一起使用,变量名应该是描述性的A.setBackground(颜色.灰色)代码>什么是A
?5) 不要让一切都是静态的,了解它是什么,它不是一个神奇的“交叉方法”字,让你调用方法从主。。。6) 谈到main
,您的程序主要是一个main
方法,它的结构更好,因此更容易理解。正如Java教程中所解释的,问题可能与未能在事件调度线程(EDT)上运行GUI有关。考虑到其他人提到的命名问题,很难确定。顺便说一句,不要在main
调用的代码中调用main
。
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();
}`