我正在尝试创建一个闪烁的圆圈。我已经到了改变一次然后抛出错误Exception in thread "AWT-EventQueue-0
的地步
我不知道为什么。你能帮助我吗?这是我的代码:
public class Final {
private static JPanel contentPane;
private static int cnt = 0;
private static int counter =0;
private static Color[] colours = {
Color.ORANGE,
Color.GRAY
};
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setSize(400, 525);
frame.setLocationByPlatform(true);
frame.setVisible(true);
ActionListener actListner = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if (cnt == (colours.length)) {
cnt = 0;
}
frame.setBackground(colours[cnt++]);
counter = cnt;
System.out.println("Counter = "+cnt);
}
};
contentPane = new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(colours[counter]);
g.fillOval(160, 70, 50, 50);
}
};
frame.add(contentPane);
Timer timer = new Timer(500, actListner);
timer.start();
}
}
完整的例外:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2
at Final$2.paintComponent(Final.java:56)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1300(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
答案 0 :(得分:1)
在>>检查其边界后,您正在递增计数器,然后使用它。不要这样做,因为这是错误的顺序。首先递增,下一次检查边界并在需要时重置为0,然后才使用它。
// increment first, cnt++;, but not inline as you're doing it
cnt++;
cnt %= colours.length; // mod it to make it 0 if greater than length
myColor = colours[cnt]; // then use it
repaint();
答案 1 :(得分:0)
只需更改
if (cnt == (colours.length)){
到
if (cnt == (colours.length-1)){