java计算器设计减法出错_java中设计一个计算器要求实现加减乘除。 可以实现加减,为什么乘除就不行嘞? 高手指点...

我的程序如下importjavax.swing.*;importjava.awt.*;importjavax.swing.border.*;importjava.awt.event.*;publicclassExample7{publicstaticvoidmain(Stringargs[]){WindowBoxwin=new...

我的程序如下

import javax.swing.*;

import java.awt.*;

import javax.swing.border.*;

import java.awt.event.*;

public class Example7 {

public static void main(String args[]){

WindowBox win=new WindowBox("简单计数器");

}

}

class WindowBox extends Frame implements ActionListener{

Box baseBox,boxV1,boxV2,boxV3;

Label labe1,labe2,labe3;

TextField text1,text2,text3;

Button butt1,butt2,butt3,butt4;

WindowBox(String s){

super(s);

boxV1=Box.createVerticalBox();

labe1=new Label("操作数");

boxV1.add(labe1);

boxV1.add(Box.createVerticalStrut(8));

labe2=new Label("操作数");

boxV1.add(labe2);

boxV1.add(Box.createVerticalStrut(8));

labe3=new Label("结果");

boxV1.add(labe3);

boxV2=Box.createVerticalBox();

text1=new TextField(20);

boxV2.add(text1);

boxV2.add(Box.createVerticalStrut(8));

text2=new TextField(20);

boxV2.add(text2);

boxV2.add(Box.createVerticalStrut(8));

text3=new TextField(20);

boxV2.add(text3);

boxV3=Box.createVerticalBox();

butt1=new Button("+");

boxV3.add(butt1);

butt1.addActionListener(this);

boxV3.add(Box.createVerticalStrut(8));

butt2=new Button("-");

boxV3.add(butt2);

butt2.addActionListener(this);

boxV3.add(Box.createVerticalStrut(8));

butt3=new Button("*");

boxV3.add(butt3);

butt3.addActionListener(this);

boxV3.add(Box.createVerticalStrut(8));

butt4=new Button("/");

boxV3.add(butt4);

butt4.addActionListener(this);

/*butt1.addActionListener(this);

butt2.addActionListener(this);

butt3.addActionListener(this);

butt4.addActionListener(this);

*/

baseBox=Box.createHorizontalBox();

baseBox.add(boxV1);

baseBox.add(Box.createHorizontalStrut(10));

baseBox.add(boxV2);

baseBox.add(Box.createHorizontalStrut(50));

baseBox.add(boxV3);

setLayout(new FlowLayout());

add(baseBox);

setBounds(120,125,350,150);

setVisible(true);

validate();

}

public void actionPerformed(ActionEvent e){

Integer m,n,p;

m=Integer.parseInt(text1.getText());

n=Integer.parseInt(text2.getText());

if(e.getSource()==butt1){

p=m+n;

text3.setText(m+"与"+n+"的和为:"+p);

}

if(e.getSource()==butt2){

p=m-n;

text3.setText(m+"与"+n+"的差为:"+p);

if(e.getSource()==butt3){

p=m*n;

text3.setText(m+"与"+n+"的积为:"+p);

if(e.getSource()==butt4){

p=m/n;

text3.setText(m+"与"+n+"的差为:"+p);

}

}

}

}

}

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值