| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 677 人关注过本帖
标题:求指教,关于计算器界面的问题
只看楼主 加入收藏
c语言新手yu
Rank: 2
等 级:论坛游民
帖 子:38
专家分:17
注 册:2012-11-5
结帖率:62.5%
收藏
已结贴  问题点数:20 回复次数:6 
求指教,关于计算器界面的问题
小弟有一代码不能找出错误的原因,请大神指教:

package javatest03;
import java.
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;


public class JavaTest_3_GUI_V1  extends JFrame implements ActionListener
{
    /**/
    private static final long serialVersionUID = 1L;

    // 第一步:确定间隔、大小等界面上的常量,以像素点为单位
    private static final int  ComponentHeight = 30;
    private static final int  ButtonWidth     = 50;
    private static final int  ComponentSpaceX = 10;
    private static final int  ComponentSpaceY = 10;
    private static final int  OriginOffsetY   = 5;
    private static final int  OriginOffsetX   = 5;
    private static final int  outPutFieldWidth = 700;
   
    // 第二步,依次创建各个按钮
    JTextField outputField   = new JTextField(150);
    JButton    buttonOne     = new JButton("1");
    JButton    buttonTwo     = new JButton("2");
        JButton    buttonThree   = new JButton("3");
        JButton    buttonFour    = new JButton("4");
        JButton    buttonFive    = new JButton("5");
        JButton    buttonSix     = new JButton("6");
        JButton    buttonSeven   = new JButton("7");
        JButton    buttonEight   = new JButton("8");
        JButton    buttonNine    = new JButton("9");
        JButton    buttonZero    = new JButton("0");
        JButton    buttonLess    = new JButton("<__");
    JButton    buttonPlus    = new JButton("+");
        JButton    buttonReduce  = new JButton("-");
        JButton    buttonAsterisk= new JButton("*");
        JButton    buttonBias    = new JButton("/");
        JButton    buttonPoint   = new JButton(".");
    JButton    buttonEqual   = new JButton("=");
    JButton    buttonClear   = new JButton("CE");
        
   
    // 第三不,生成各控件的相对于主窗口的矩形位置(原点x值,原点y值,宽,高)
    Rectangle  boundOutputField =
                    new Rectangle(OriginOffsetY + 0 *(ComponentSpaceX + ButtonWidth), OriginOffsetX + 0 *(ComponentSpaceY + ComponentHeight),
                                    outPutFieldWidth, ComponentHeight);
    Rectangle  boundButtonOne =
                    new Rectangle(OriginOffsetY + 0 *(ComponentSpaceX + ButtonWidth), OriginOffsetX + 1 *(ComponentSpaceY + ComponentHeight),
                             ButtonWidth, ComponentHeight);   
    Rectangle  boundButtonTwo =
                    new Rectangle(OriginOffsetY + 1 *(ComponentSpaceX + ButtonWidth), OriginOffsetX + 1 *(ComponentSpaceY + ComponentHeight),
                             ButtonWidth, ComponentHeight);
    Rectangle  boundButtonThree =
                    new Rectangle(OriginOffsetY + 2 *(ComponentSpaceX + ButtonWidth), OriginOffsetX + 1 *(ComponentSpaceY + ComponentHeight),
                             ButtonWidth, ComponentHeight);
    Rectangle  boundButtonfour =
                    new Rectangle(OriginOffsetY + 3 *(ComponentSpaceX + ButtonWidth), OriginOffsetX + 1 *(ComponentSpaceY + ComponentHeight),
                             ButtonWidth, ComponentHeight);
    Rectangle  boundButtonFive =
                    new Rectangle(OriginOffsetY + 4 *(ComponentSpaceX + ButtonWidth), OriginOffsetX + 1 *(ComponentSpaceY + ComponentHeight),
                             ButtonWidth, ComponentHeight);
    Rectangle  boundButtonSix =
                    new Rectangle(OriginOffsetY + 5 *(ComponentSpaceX + ButtonWidth), OriginOffsetX + 1 *(ComponentSpaceY + ComponentHeight),
                             ButtonWidth, ComponentHeight);
    Rectangle  boundButtonSeven =
                    new Rectangle(OriginOffsetY + 6 *(ComponentSpaceX + ButtonWidth), OriginOffsetX + 1 *(ComponentSpaceY + ComponentHeight),
                             ButtonWidth, ComponentHeight);
    Rectangle  boundButtonEight =
                    new Rectangle(OriginOffsetY + 7 *(ComponentSpaceX + ButtonWidth), OriginOffsetX + 1 *(ComponentSpaceY + ComponentHeight),
                             ButtonWidth, ComponentHeight);
    Rectangle  boundButtonNine =
                    new Rectangle(OriginOffsetY + 8 *(ComponentSpaceX + ButtonWidth), OriginOffsetX + 1 *(ComponentSpaceY + ComponentHeight),
                             ButtonWidth, ComponentHeight);
    Rectangle  boundButtonZero =
                    new Rectangle(OriginOffsetY + 9 *(ComponentSpaceX + ButtonWidth), OriginOffsetX + 1 *(ComponentSpaceY + ComponentHeight),
                             ButtonWidth, ComponentHeight);
    Rectangle  boundButtonLess =
                    new Rectangle(OriginOffsetY + 0 *(ComponentSpaceX + ButtonWidth), OriginOffsetX + 2 *(ComponentSpaceY + ComponentHeight),
                             ButtonWidth, ComponentHeight);
   
    Rectangle  boundButtonPlus =
                    new Rectangle(OriginOffsetY + 1 *(ComponentSpaceX + ButtonWidth), OriginOffsetX + 2 *(ComponentSpaceY + ComponentHeight),
                                ButtonWidth, ComponentHeight);
    Rectangle  boundButtonAsterisk =
                    new Rectangle(OriginOffsetY + 2 *(ComponentSpaceX + ButtonWidth), OriginOffsetX + 2 *(ComponentSpaceY + ComponentHeight),
                             ButtonWidth, ComponentHeight);
    Rectangle  boundButtonBias =
                    new Rectangle(OriginOffsetY + 3 *(ComponentSpaceX + ButtonWidth), OriginOffsetX + 2 *(ComponentSpaceY + ComponentHeight),
                                ButtonWidth, ComponentHeight);
    Rectangle  boundButtonPoint =
                    new Rectangle(OriginOffsetY + 4 *(ComponentSpaceX + ButtonWidth), OriginOffsetX + 2 *(ComponentSpaceY + ComponentHeight),
                                ButtonWidth, ComponentHeight);            
    Rectangle  boundButtonEqual =
                    new Rectangle(OriginOffsetY + 5 *(ComponentSpaceX + ButtonWidth), OriginOffsetX + 2 *(ComponentSpaceY + ComponentHeight),
                                ButtonWidth, ComponentHeight);   
    Rectangle  boundButtonClear =
                    new Rectangle(OriginOffsetY + 6 *(ComponentSpaceX + ButtonWidth), OriginOffsetX + 2 *(ComponentSpaceY + ComponentHeight),
                                      ButtonWidth, ComponentHeight);
   
    /**
     * @param args
     */
    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        JavaTest_3_GUI_V1 frame = new JavaTest_3_GUI_V1();
        frame.setTitle("JavaTest_3_GUI_V1");
        frame.setLocation(100, 100);
        frame.setSize(outPutFieldWidth + 3 * OriginOffsetY, 350);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public JavaTest_3_GUI_V1()
    {
        setLayout(null);
        
        // 第四步:设置控件额外的属性
      // output text field configuration and initialization
      // 设置outputField的边界(相对于主窗口的位置和大小)
      outputField.setBounds(boundOutputField);
      // 设置oututField的字符串显示顺序,设为从右到左显示
      outputField.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
      // 设置outputField内的默认内容,应该显示为“0.”
      outputField.setText(".0");
      // 禁止从界面(键盘)直接向outputField输入信息,只能通过程序改变outputField的内容
      outputField.setEditable(false);
        
        // Number buttons configuration and initialization
      // 第五步:通过设置各控件的边界设置控件的坐标和大小
        buttonOne.setBounds(boundButtonOne);
        buttonTwo.setBounds(boundButtonTwo);        
        // buttonOne.setBackground(Color.RED);

       // Operations configuration and initialization
        buttonPlus.setBounds(boundButtonPlus);        
        buttonEqual.setBounds(boundButtonEqual);        
        buttonClear.setBounds(boundButtonClear);        
        
        // Add TextFields and buttons to the content panel
      // 第六步:把各个界面元素按照指定的位置加入主界面
        add(outputField);
        add(buttonOne);
        add(buttonTwo);
        add(buttonThree);
        add(buttonFour);
        add(buttonFive);
        add(buttonSix);
        add(buttonSeven);
        add(buttonEight);
        add(buttonNine);
        add(buttonZero);
        add(buttonLess);
        add(buttonPlus);
        add(buttonReduce);
        add(buttonAsterisk);
        add(buttonBias);
        add(buttonPoint);
        add(buttonEqual);
        add(buttonClear);
        
        // Register action listener so as to handle events from the JFrame window
       // 第七步:为各个按钮注册(鼠标左击)事件处理器(JavaTest_5_GUI_V1.actionPerformed)
        buttonOne.addActionListener(this);
        buttonTwo.addActionListener(this);
        buttonThree.addActionListener(this);
        buttonFour.addActionListener(this);
        buttonFive.addActionListener(this);
        buttonSix.addActionListener(this);
        buttonSeven.addActionListener(this);
        buttonEight.addActionListener(this);
        buttonNine.addActionListener(this);
        buttonZero.addActionListener(this);
        buttonLess.addActionListener(this);
        buttonplus.addActionListener(this);
        buttonReduce.addActionListener(this);
        buttonAsterisk.addActionListener(this);
        buttonBias.addActionListener(this);
        buttonPoint.addActionListener(this);
        buttonEqual.addActionListener(this);
        buttonClear.addActionListener(this);
    }

   
}
因为我是初学者,那些很繁琐的过程不会用数组输入,所以程序就这样长。劳烦帮我找一下错误,并提示一下怎样改,
谢谢!!!
搜索更多相关主题的帖子: 计算器 private package public 
2013-04-03 13:02
w527705090
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:441
专家分:1882
注 册:2011-6-28
收藏
得分:1 
那就给代码,让别人直接找错误,你都不说你的程序什么样的功能没有实现 ,别人怎么找啊??别人哪来那么多的闲工夫来看你的代码啊

有心者,千方百计;无心者,千难万难。
2013-04-04 14:50
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:14 
除了第七步里面buttonplus.addActionListener(this);的buttonPlus中的P写成了小写,还有没有实现接口ActionListener的public void actionPerformed(ActionEvent e)方法外,其它没有什么错,至少这两处改了界面可以显示了
2013-04-06 07:14
Kingbox_tang
Rank: 7Rank: 7Rank: 7
来 自:天津师范大学
等 级:黑侠
威 望:3
帖 子:146
专家分:677
注 册:2012-11-27
收藏
得分:5 
你没有实现事件接口吧
怎么可能正确??
接口就是你当点击按钮时应该有什么反应,你没有写。

旨在提高编程水平,学有所用,学有所成,学有所为。
2013-04-06 11:15
阿里那个巴巴
Rank: 3Rank: 3
来 自:中山大学
等 级:论坛游侠
威 望:2
帖 子:55
专家分:153
注 册:2013-3-19
收藏
得分:0 
我是来学习学习的
2013-04-07 22:30
c语言新手yu
Rank: 2
等 级:论坛游民
帖 子:38
专家分:17
注 册:2012-11-5
收藏
得分:0 
回复 3楼 yhlvht
哦哦,谢谢。不过我只是做个界面而已···
2013-04-10 17:08
c语言新手yu
Rank: 2
等 级:论坛游民
帖 子:38
专家分:17
注 册:2012-11-5
收藏
得分:0 
回复 2楼 w527705090
嗯嗯··谢谢
2013-04-10 17:28
快速回复:求指教,关于计算器界面的问题
数据加载中...
 
   



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

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