2012-01-24 38 views
-3

当我运行下面的程序时,我收到第55行和第87行(有注释,所以你知道这些行是)的错误,说有一个ArrayIndexOutofBoundsException。我不知道是什么导致了这个问题,因为我是一名初学Java程序员。请帮帮我!Java - ArrayIndexOutOfBoundsException错误

import javax.swing.*; 
import java.awt.*; 

public class Graphics { 
// listing all the components for the word processor 
JFrame f1; 
JPanel colorspanel; 
JPanel sizepanel; 
JPanel fontpanel; 
JPanel mainpanel; 
JTextField Maintextfield; 
JLabel colorlabel; 
JLabel sizelabel; 
JLabel fontlabel; 
JButton colorbuttons[]; 
JButton sizebuttons[]; 
JButton fontbuttons[]; 

Graphics() {//line 26 
// making instances of panels 
colorspanel = new JPanel(); 
sizepanel = new JPanel(); 
fontpanel = new JPanel(); 
mainpanel = new JPanel(); 
// setting the size of the panels 
colorspanel.setSize(216, 144); 
sizepanel.setSize(216, 144); 
fontpanel.setSize(216, 144); 
mainpanel.setSize(612, 756); 
// making instances of button 
colorbuttons = new JButton[9]; 
for(int i=0; i<colorbuttons.length; i++) 
colorbuttons[i] = new JButton(); 

sizebuttons = new JButton[14]; 
fontbuttons = new JButton[9]; 
// setting content for buttons 
// colorbuttons 
colorbuttons[0].setBackground(Color.black); 
colorbuttons[1].setBackground(Color.red); 
colorbuttons[2].setBackground(Color.blue); 
colorbuttons[3].setBackground(Color.yellow); 
colorbuttons[4].setBackground(Color.green); 
colorbuttons[5].setBackground(Color.gray); 
colorbuttons[6].setBackground(Color.DARK_GRAY); 
colorbuttons[7].setBackground(Color.ORANGE); 
colorbuttons[8].setBackground(Color.pink); 
colorbuttons[9].setBackground(Color.magenta);//line 57 
// sizebuttons 
sizebuttons[0].setText("8"); 
sizebuttons[1].setText("10"); 
sizebuttons[2].setText("12"); 
sizebuttons[3].setText("14"); 
sizebuttons[4].setText("16"); 
sizebuttons[5].setText("18"); 
sizebuttons[6].setText("20"); 
sizebuttons[7].setText("22"); 
sizebuttons[8].setText("24"); 
sizebuttons[9].setText("26"); 
sizebuttons[10].setText("28"); 
sizebuttons[11].setText("30"); 
sizebuttons[12].setText("32"); 
sizebuttons[13].setText("34"); 
sizebuttons[14].setText("36"); 
// fontbuttons 
fontbuttons[0].setText("New Times Roman"); 
fontbuttons[1].setText("Blackadder ITC"); 
fontbuttons[2].setText("Andy"); 
fontbuttons[3].setText("Buxton Sketch"); 
fontbuttons[4].setText("Arial Black"); 
fontbuttons[5].setText("Comic Sans MS"); 
fontbuttons[6].setText("Old English Text MT"); 
fontbuttons[7].setText("SketchFlow Print"); 
fontbuttons[8].setText("Harlow Solid Italic"); 
fontbuttons[9].setText("Algerian"); 
f1.setVisible(true); 
} 

public static void main(String[] args){ 
Graphics graphics = new Graphics();//line 87 

} 


} 

回答

2

你创建colorbuttons为包含9个元素的阵列,所以它有9个指数:0,1,2,3,4,5,6,7和8刚刚摆脱其访问colorbuttons[9]线的。由于数组索引从0开始而不是1,因此如果创建大小为n的数组,则最高有效索引为n-1

0

那么,你创建一个大小为9的数组,然后尝试访问第十个事物。我想说,这意味着你要出界了......

colorbuttons = new JButton[9]; 
... 
colorbuttons[9].setBackground(Color.magenta);//line 57 
0

变化

colorbuttons = new JButton[9]; 
for(int i=0; i<colorbuttons.length; i++) 
colorbuttons[i] = new JButton(); 

sizebuttons = new JButton[14]; 
fontbuttons = new JButton[9]; 

colorbuttons = new JButton[10]; 
for(int i=0; i<colorbuttons.length; i++) 
    colorbuttons[i] = new JButton(); 
sizebuttons = new JButton[15]; 
for(int i=0; i<sizebuttons.length; i++) 
    sizebuttons[i]=new JButton(); 
fontbuttons = new JButton[10]; 
for(int i=0; i<fontbuttons.length; i++) 
    fontbuttons[i]=new JButton(); 

当您创建新的阵列可以指定有多少元素该数组将包含:如果您想要填充索引范围从0到N的数组,您想拥有一个包含N + 1个元素的数组。

您还忘记在sizebuttonsfontbuttons阵列中创建JButton

相关问题