java可以制作动画么_在Java中制作动画的最佳方式?
我认为覆盖paintComponent(Graphics g)并定期调用JPanel上的重绘方法或者使用Timer绘制的任何内容更合适。你的问题可能是由于你试图绘制然后Swing做它自己的平局。
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel() {
public void paintComponent(Graphics g) {
//draw here
}
};
panel.setPreferredSize(800, 600);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true)
new Timer(16, new ActionListener() {
public void actionPerformed(ActionEvent event) {
panel.repaint();
}
}).start();
}
}
我认为覆盖paintComponent(Graphics g)并定期调用JPanel上的重绘方法或者使用Timer绘制的任何内容更合适。你的问题可能是由于你试图绘制然后Swing做它自己的平局。 public class Main { public static void main(String[] args) { JFrame frame = new JFrame(); JPanel panel = new JPanel() { public void paintComponent(Graphics g) { //draw here } }; panel.setPreferredSize(800, 600); frame.add(panel); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true) new Timer(16, new ActionListener() { public void actionPerformed(ActionEvent event) { panel.repaint(); } }).start(); } }