这是代码,正方形渲染但不会移动,即使它应该渲染每个循环。问题似乎出在核心类中,在 run()
package com.game;
import java.awt.*;
import javax.swing.*;
public class Core implements Runnable{
private static boolean running = true;
public static JFrame frame = new JFrame();
public static void main(String[] args) {
frame.setResizable(false);
frame.setBounds(100,100,600,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
start();
}
package com.game;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Paint extends JPanel{
public static int x = 10;
public void paintComponent(Graphics g){
super.paintComponent(g);
g.fillRect(x,10,100,100);
}
}
private static void start() {
running = true;
Thread thread = new Thread(new Core());
thread.setPriority(Thread.MAX_PRIORITY);
thread.start();
}
@Override
public void run() {
Paint p = new Paint();
Container pane = frame.getContentPane();
while(running){
pane.add(p);
pane.show();
System.out.println(Paint.x);
try {
Thread.sleep(100);
} catch (Exception e){}
}
}
}
抱歉,如果代码以任何方式令人讨厌,我不知道应该如何格式化它......