Javapet 发表于 2008-3-16 16:33

我的程序哪里错了??

为什么我的程序出现下面的错误:
                   Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:26

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

class Counter extends JFrame
{
          public Counter(String str)
          {
                    super(str);
                    this.setLayout(null);
                    Container Container_=this.getContentPane();
                    this.pet(Container_);
          }
                public void pet(Container Container_)
                {                               
                                //创建布局管理器
                                GridLayout GridLayout1=new GridLayout(1,3,5,5);
                                GridLayout GridLayout2=new GridLayout(4,1,5,5);
                                GridLayout GridLayout3=new GridLayout(4,5);
                               
                                //实例化JPanel
                                JPanel JP1=new JPanel();
                                JPanel JP2=new JPanel();
                                JPanel JP3=new JPanel();
                                JPanel JP4=new JPanel();
                                JPanel JP5=new JPanel();
                               
                                //创建菜单栏
                                MenuBar Menu_Bar=new MenuBar();
                                Menu Menu_a=new Menu("编辑");
                                Menu Menu_b=new Menu("帮助");
                                MenuItem Menu_Item_a=new MenuItem("复制");
                                MenuItem Menu_Item_b=new MenuItem("粘贴");
                                MenuItem Menu_Item_c=new MenuItem("关于计算机");
                                Menu_a.add(Menu_Item_a);
                                Menu_a.add(Menu_Item_b);
                                Menu_b.add(Menu_Item_c);
                                Menu_Bar.add(Menu_a);
                                Menu_Bar.add(Menu_b);
                                this.setMenuBar(Menu_Bar);
                               
                                //创建布置数值显示屏和记忆屏
                                JTextField JText=new JTextField("0.",21);
                                JText.setBackground(Color.white);
                                JText.setHorizontalAlignment(JTextField.RIGHT );
                                JText.setEditable(false);
                                TextField Text=new TextField(1);
                                Text.setEnabled(false);
                          JP4.add(JText);
                                JP5.add(Text);
                                JP4.setBounds(2,0,250,30);
                                JP5.setBounds(26,30,0,0);
                               
                                //创建按钮并设置文本颜色
                          Button[] B=new Button[26];
                                B[0]=new Button("MC");
                                B[1]=new Button("MR");
                                B[2]=new Button("MS");
                                B[3]=new Button("M+");
                          B[4]=new Button("Backspace");
                                B[5]=new Button("CE");
                                B[6]=new Button("C");
                          B[7]=new Button("/");
                                B[8]=new Button("*");
                                B[9]=new Button("+");
                                B[10]=new Button("-");
                                B[11]=new Button("=");
                                B[12]=new Button("1");
                                B[13]=new Button("2");
                                B[14]=new Button("3");
                                B[15]=new Button("4");
                                B[16]=new Button("5");
                                B[17]=new Button("6");
                                B[18]=new Button("7");
                                B[19]=new Button("8");
                                B[20]=new Button("9");
                                B[21]=new Button("0");
                                B[22]=new Button(".");
                                B[23]=new Button("%");
                                B[24]=new Button("+/-");
                                B[25]=new Button("1/x");
                                for(int i=0;i<12;i++)
                                {
                                        B[i].setForeground(Color.red);
                                }
                          for(int i=12;i<27;i++)
                                {
                                        B[i].setForeground(Color.blue);
                                }
               
                                //布置JP1
                                JP1.setLayout(GridLayout2);
                                JP1.add(B[0]);
                                JP1.add(B[1]);
                                JP1.add(B[2]);
                                JP1.add(B[3]);
                                JP1.setBounds(9,65,35,120);                               
                         
                          //容器整合
                          Container_.add(JP1);
                                Container_.add(JP2);
        Container_.add(JP4);
        Container_.add(JP5);   
                }
                public void paintComponent(Graphics g)
          {
              Graphics2D g1=(Graphics2D)g;
              g1.setColor(Color.white);
    }               
    public static void main(String[] args)
                {
                                Counter C=new Counter("计算器");
                                C.setSize(260,246);
                                C.setLocation(200,200);
                                C.setVisible(true);
                }
}

[[it] 本帖最后由 Javapet 于 2008-3-16 16:41 编辑 [/it]]

sunkaidong 发表于 2008-3-16 16:45

for(int i=12;i<27;i++)
                {
                    B[i].setForeground(Color.blue);
                }
数组下标越界了

sunkaidong 发表于 2008-3-16 16:46

按钮没有添加布局..也没设置监听...给你加油...

Javapet 发表于 2008-3-16 17:01

你好,我一共定义了26个Button,第一个for循环是从0到11,第二个for循环是12到26,那怎么会下标越界,请指教!

千里冰封 发表于 2008-3-16 17:04

总共26,下标就只能是0-25

sunkaidong 发表于 2008-3-16 17:04

你的按钮数组只到了25..而且你的数组大小是26那么它只能访问到0-25个下标吧..

Javapet 发表于 2008-3-16 17:09

哦..........我明白了,刚才我在钻牛角尖,现在知道了,谢谢各位的指点!!

Ethip 发表于 2008-3-16 19:16

回复 1# 的帖子

你在做计算器啊!!我之前在这里发了个计算器的程序,可以找找,先!
当然,我这也有source啦!

页: [1]

编程论坛