| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 687 人关注过本帖
标题:我的程序哪里错了??
只看楼主 加入收藏
Javapet
Rank: 1
等 级:禁止访问
帖 子:80
专家分:0
注 册:2008-1-5
结帖率:50%
收藏
 问题点数:0 回复次数:7 
我的程序哪里错了??
为什么我的程序出现下面的错误:
                   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
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
for(int i=12;i<27;i++)
                {
                    B[i].setForeground(Color.blue);
                }
数组下标越界了

学习需要安静。。海盗要重新来过。。
2008-03-16 16:45
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
按钮没有添加布局..也没设置监听...给你加油...

学习需要安静。。海盗要重新来过。。
2008-03-16 16:46
Javapet
Rank: 1
等 级:禁止访问
帖 子:80
专家分:0
注 册:2008-1-5
收藏
得分:0 
你好,我一共定义了26个Button,第一个for循环是从0到11,第二个for循环是12到26,那怎么会下标越界,请指教!
2008-03-16 17:01
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
总共26,下标就只能是0-25

可惜不是你,陪我到最后
2008-03-16 17:04
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
你的按钮数组只到了25..而且你的数组大小是26那么它只能访问到0-25个下标吧..

学习需要安静。。海盗要重新来过。。
2008-03-16 17:04
Javapet
Rank: 1
等 级:禁止访问
帖 子:80
专家分:0
注 册:2008-1-5
收藏
得分:0 
哦..........我明白了,刚才我在钻牛角尖,现在知道了,谢谢各位的指点!!
2008-03-16 17:09
Ethip
Rank: 5Rank: 5
等 级:贵宾
威 望:15
帖 子:771
专家分:0
注 册:2008-1-18
收藏
得分:0 
回复 1# 的帖子
你在做计算器啊!!我之前在这里发了个计算器的程序,可以找找,先!
当然,我这也有source啦!
2008-03-16 19:16
快速回复:我的程序哪里错了??
数据加载中...
 
   



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

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