1

所以今天我正在制作一个程序,因为我还是一个初学者,我还在学习,但我想知道如何添加另一个圆圈,例如我有两个单元,红色和蓝色,我添加了randomize 随机选择 x 和 y 位置,但是当我点击开始时,它只显示一个红色圆圈,蓝色圆圈甚至不存在,我知道我没有做一些编码,但这是我的程序,请帮助谢谢: )

所以yh :) 在此先感谢。

4

1 回答 1

5

A few things to change here:

  • Drop all AWT components (Canvas, Panel, etc...) and replace them with their equivalent Swing one (JPanel, JTextField...). This will avoid rendering issues and bring double buffering (without any code to perform).
  • Don't ever use c.getGraphics().
  • Override paintComponent(Graphics g) and use the Graphics g parameter provided there (see also this link for some example)
  • To perform "animation" use a javax.swing.Timer. All updates to the UI must be done on the EDT (Event Dispatching Thread). Read also about concurrency in Swing
  • When using JOptionPane.showMessageDialog (or any other dialog), provide a valid parent component and not null. This will allow proper parenting of windows (avoiding dialogs to be hidden by other frames).
于 2013-06-19T08:02:46.383 回答