2012-01-24 59 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

相關問題