| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 690 人关注过本帖
标题:我的程序哪里错了??
取消只看楼主 加入收藏
Javapet
Rank: 1
等 级:禁止访问
帖 子:80
专家分:0
注 册:2008-1-5
结帖率:50%
收藏
 问题点数:0 回复次数:2 
我的程序哪里错了??
为什么我的程序出现下面的错误:
                   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]]
搜索更多相关主题的帖子: thread public import super 
2008-03-16 16:33
Javapet
Rank: 1
等 级:禁止访问
帖 子:80
专家分:0
注 册:2008-1-5
收藏
得分:0 
你好,我一共定义了26个Button,第一个for循环是从0到11,第二个for循环是12到26,那怎么会下标越界,请指教!
2008-03-16 17:01
Javapet
Rank: 1
等 级:禁止访问
帖 子:80
专家分:0
注 册:2008-1-5
收藏
得分:0 
哦..........我明白了,刚才我在钻牛角尖,现在知道了,谢谢各位的指点!!
2008-03-16 17:09
快速回复:我的程序哪里错了??
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030313 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved