海珠 2009-10-12 21:52
浏览 211
已采纳

计算器问题 很烦人!!以后有钱给您补上

package com.A;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
public class CalculatorDemo extends WindowAdapter implements ActionListener
{

int j,p,k,l,m ,i;
JButton b[]=new JButton[10]; //按钮数组
JFrame frame;
JButton Ab,dian,dao,jia,jian,cheng,chu,equ,CE,C,Ba,BF,sq,zf;
JTextField show;
JDialog about;
double getValue,sum;

int length=30;
public  CalculatorDemo()
{   
    frame=new JFrame("计算器");
    frame.setSize(270, 210);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container con=frame.getContentPane();
    JTextField show=new JTextField(30);
    show.setEditable(false);
    show.setHorizontalAlignment(JTextField.RIGHT);
    con.add(show,BorderLayout.NORTH);
    con.add(getmainPanel(),BorderLayout.CENTER);
    frame.setResizable(false);
    frame.setLocation(300,280);
    frame.setVisible(true);

    }
public JPanel getmainPanel()
{
    JPanel mainPanel=new JPanel();
    JPanel upPanel =new JPanel(new GridLayout(1,4,2,2));
    Ba=new JButton("Back");
    upPanel.add(Ba);
    Ba.addActionListener(this);
    CE=new JButton("CE");
    upPanel.add(CE);
    CE.addActionListener(this);
    C=new JButton("C");
    upPanel.add(C);
    C.addActionListener(this);
    Ab=new JButton("About");
    upPanel.add(Ab);
    Ab.addActionListener(this);
    about=new JDialog(frame,"关于计算器",true);
    about.setLocation(300,300);
    JLabel lable=new JLabel("作者:章黎");
    about.add(lable,"Center");
    about.setSize(250, 100);
    about.addWindowListener(this);

    upPanel.setVisible(true);
    mainPanel.add(upPanel);

    JPanel dPanel =new JPanel(new BorderLayout(2,2));

    JPanel leftPanel =new JPanel(new GridLayout(4,3,2,3));
    for(i=9;i>=0;i--)
    {
      b[i]=new JButton(String.valueOf(i));
      leftPanel.add(b[i]);
      b[i].addActionListener(this);
    }  
     JButton zf=new JButton("+/-");
     leftPanel.add(zf);
     zf.addActionListener(this);
     JButton dian=new JButton(".");
     dian.addActionListener(this);
     leftPanel.add(dian);
     leftPanel.setVisible(true);
     dPanel.add(leftPanel,BorderLayout.WEST);

     JPanel rightPanel =new JPanel(new GridLayout(4,2,2,3));
     JButton chu=new JButton("/");
     rightPanel.add(chu);
     chu.addActionListener(this);
     JButton sq=new JButton("sqrt");
     rightPanel.add(sq);
     sq.addActionListener(this);
     JButton cheng=new JButton("*");
     rightPanel.add(cheng);
     cheng.addActionListener(this);
     JButton BF=new JButton("%");
     rightPanel.add(BF);
     BF.addActionListener(this);
     JButton jian=new JButton("-");
     rightPanel.add(jian);
     jian.addActionListener(this);
     JButton dao=new JButton("1/x");
     rightPanel.add(dao);
     dao.addActionListener(this);
     JButton jia=new JButton("+");
     rightPanel.add(jia);
     jia.addActionListener(this);
     JButton equ=new JButton("=");
     rightPanel.add(equ);
     equ.addActionListener(this);
     rightPanel.setVisible(true);
     dPanel.add(rightPanel,BorderLayout.CENTER);
     dPanel.setVisible(true);
     mainPanel.add(dPanel);
     mainPanel.setVisible(true);
     return mainPanel;
}


public void actionPerformed(ActionEvent e) {

// getValue=Double.parseDouble(show.getText());//记住输入的数
if(e.getSource()==jia)
{
if(k==0)//表示加号没有被触发过
sum=getValue;
if(l==1)//连续触发加号键直接得结果
{
sum+=getValue;
}
setSum();
k++;l=1;p=0;
}
if(e.getSource()==jian)
{
if(k==0)
sum=getValue;
if(l==2)
{
sum-=getValue;
}
setSum();
k++;l=2;p=0;
}
if(e.getSource()==cheng)
{
if(k==0)
sum=getValue;
if(l==3)
{
sum*=getValue;
}
setSum();
k++;l=3;p=0;
}
if(e.getSource()==chu)
{
if(k==0)
sum=getValue;
if(l==4)
{
sum/=getValue;
}
setSum();
k++;l=4;p=0;
}
if(e.getSource()==equ)

{
switch(l)//判断触发了那个运算符后再触发等于号
{
case 1:show.setText(String.valueOf(sum+getValue));break;
case 2:show.setText(String.valueOf(sum-getValue));break;
case 3:show.setText(String.valueOf(sum*getValue));break;
case 4:show.setText(String.valueOf(sum/getValue));break;
}
setSum();
k=0;p=0;l=0;
}

if(e.getSource()==dian)//如果触发按钮".",是第一次的话允许添加"."并接受后来输入的数
{
    if(p==0)
    {
        show.setText(show.getText()+"."+e.getActionCommand());
    }
    p=1;
}
if(e.getSource()==C||e.getSource()==CE)//触发按钮还原则显示0
{
    k=0;p=0;l=0;sum=0;
    show.setText("0");
}
if(e.getSource()==Ab)
{
    about.setVisible(true);
}   
for(i=9;i>=0;i--)
{
    if(e.getSource()==b[i])
     {
        if(j==0)//触发了等于号后j=0;再输入数时就得清空以前的数
            show.setText("");
            String s=show.getText();
        if(s.length()<length)
        {
            show.setText(s+e.getActionCommand());
        }
        if(e.getSource()==b[0]&&getValue==0&&p==0)
        {
            show.setText("0");
        }
        if(e.getSource()!=b[0]&&getValue==0&&p==0)//如果第一个数输入的是0第二个数不是则显示第二个数
       {
            show.setText(e.getActionCommand());
       }
        j++;
    }
  }
if(e.getSource()==dao)
{
    sum=1/getValue;
    setSum();
}
if(e.getSource()==BF)
{
    sum=getValue/100;
    setSum();
}
if(e.getSource()==sq)
{
    sum=getValue*getValue;
    setSum();
}
if(e.getSource()==Ba)
{
    String s=show.getText();
    if(s.length()>1)
    {
        show.setText("");//清空显示的数用来接收退格以后的数
        for(m=0;m<s.length()-1;m++)//用一个循环来显示从0到length-1的位置的字符
        {
            show.setText(show.getText()+s.charAt(m));
        }
    }
    else
        show.setText("0");
}
if(e.getSource()==zf)
{
    String s=show.getText();
    char a=s.charAt(0);
    if(a=='-')
    {
        show.setText("");
        for(m=1;m<s.length();m++)
        {
            show.setText(show.getText()+s.charAt(m));
        }
    }
    else
        show.setText("-"+s);
}
}

public void setSum()
{
    show.setText(String.valueOf(sum));
    String s=show.getText();
    char a=s.charAt(s.length()-1);
    char b=s.charAt(s.length()-2);
    if(a=='0'&&b=='.')
{

// show.setText(String.valueOf(Math.round(sum)));
show.setText(s.substring(0, s.length()-2));
}

}
public void windowClosing(WindowEvent e){
    if(e.getSource()==about)
        about.setVisible(false);
    else if(e.getSource()==frame)
        System.exit(0);
}

public static void main(String []args)
{
    new CalculatorDemo();
}

}为什么触发数字键就有错误啊 !!怎么想也不明白!
[b]问题补充:[/b]
我的show 不new 了吗?也添加到frame 上啦

  • 写回答

4条回答 默认 最新

  • pysky521 2009-10-16 11:29
    关注

    开始看错了,你定义了类的属性是show[code="java"]JTextField show;[/code]然后又在构造方法里定义了一个临时变量是show[code="java"]JTextField show = new JTextField(30);[/code],要明白这两个show是不一样的,把构造方法里的JTextField去了就可以运行了,不过还又好多其它问题你自己调吧

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘