永发信息网

java 语句 看看哪错了?简单的计算器

答案:1  悬赏:10  手机版
解决时间 2021-05-11 15:36

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class MainFrame extends JFrame implements ActionListener {
boolean clear ;
int a;
int b = 0;
int c = 0;
private JButton btplus;
private JButton btminus;
private JButton btm;
private JButton btdivide;
private JButton btequal;
private JButton btc;
private JButton bt1;
private JButton bt2;
private JButton bt3;
private JButton bt4;
private JButton bt5;
private JButton bt6;
private JButton bt7;
private JButton bt8;
private JButton bt9;
private JButton bt0;
private JTextField text;

public MainFrame() {
super();
initComponents();

}

public MainFrame(String title) {
super(title);
initComponents();
}

private void initComponents() {
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);

this.add(this.getText());

this.add(this.getBtplus());
this.add(this.getBtminus());
this.add(this.getBtm());
this.add(this.getBtdivide());
this.add(this.getBtequal());
this.add(this.getBtc());

this.add(this.getBt0());
this.add(this.getBt1());
this.add(this.getBt2());
this.add(this.getBt3());
this.add(this.getBt4());
this.add(this.getBt5());
this.add(this.getBt6());
this.add(this.getBt7());
this.add(this.getBt8());
this.add(this.getBt9());

}

public JTextField getText() {
if (text == null) {
text = new JTextField();

text.setBounds(40, 30, 200, 30);

}
return text;
}

public JButton getBtplus() {
if (btplus == null) {
btplus = new JButton("+");
btplus.setBounds(90, 200, 50, 30);
btplus.addActionListener(this);
}
return btplus;
}

public JButton getBtminus() {
if (btminus == null) {
btminus = new JButton("-");
btminus.setBounds(140, 200, 50, 30);
btminus.addActionListener(this);
}
return btminus;
}

public JButton getBtm() {
if (btm == null) {
btm = new JButton("*");
btm.setBounds(190, 120, 50, 30);
btm.addActionListener(this);
}
return btm;
}

public JButton getBtequal() {
if (btequal == null) {
btequal = new JButton("=");
btequal.setBounds(190, 160, 50, 30);
btequal.addActionListener(this);
}
return btequal;
}

public JButton getBtdivide() {
if (btdivide == null) {
btdivide = new JButton("/");
btdivide.setBounds(190, 80, 50, 30);
btdivide.addActionListener(this);

}
return btdivide;
}

public JButton getBtc() {
if (btc == null) {
btc = new JButton("c");
btc.setBounds(190, 200, 50, 30);
btc.addActionListener(this);
}
return btc;
}

public JButton getBt1() {
if (bt1 == null) {
bt1 = new JButton("1");
bt1.setBounds(40, 80, 50, 30);
bt1.addActionListener(this);
}
return bt1;
}

public JButton getBt2() {
if (bt2 == null) {
bt2 = new JButton("2");
bt2.setBounds(90, 80, 50, 30);
bt2.addActionListener(this);
}
return bt2;
}

public JButton getBt3() {
if (bt3 == null) {
bt3 = new JButton("3");
bt3.setBounds(140, 80, 50, 30);
bt3.addActionListener(this);
}
return bt3;
}

public JButton getBt4() {
if (bt4 == null) {
bt4 = new JButton("4");
bt4.setBounds(40, 120, 50, 30);
bt4.addActionListener(this);
}
return bt4;
}

public JButton getBt5() {
if (bt5 == null) {
bt5 = new JButton("5");
bt5.setBounds(90, 120, 50, 30);
bt5.addActionListener(this);
}
return bt5;
}

public JButton getBt6() {
if (bt6 == null) {
bt6 = new JButton("6");
bt6.setBounds(140, 120, 50, 30);
bt6.addActionListener(this);
}
return bt6;
}

public JButton getBt7() {
if (bt7 == null) {
bt7 = new JButton("7");
bt7.setBounds(40, 160, 50, 30);
bt7.addActionListener(this);
}
return bt7;
}

public JButton getBt8() {
if (bt8 == null) {
bt8 = new JButton("8");
bt8.setBounds(90, 160, 50, 30);
bt8.addActionListener(this);
}
return bt8;
}

public JButton getBt9() {
if (bt9 == null) {
bt9 = new JButton("9");
bt9.setBounds(140, 160, 50, 30);
bt9.addActionListener(this);
}
return bt9;
}

public JButton getBt0() {
if (bt0 == null) {
bt0 = new JButton("0");
bt0.setBounds(40, 200, 50, 30);
bt0.addActionListener(this);
}
return bt0;
}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object temp = e.getSource();
if (temp == btc) {
text.setText(null);
clear = true;
}

if (temp == bt0) {
if (clear == false)
text.setText(null);
text.setText(text.getText() + "0");

}

if (temp == bt1) {
if (clear == false)
text.setText(null);

text.setText(text.getText() + "1");
clear = true;
}

if (temp == bt2) {
if (clear == false)
text.setText(null);

text.setText(text.getText() + "2");
clear = true;
}

if (temp == bt3) {
if (clear == false)
text.setText(null);

text.setText(text.getText() + "3");
clear = true;
}

if (temp == bt4) {
if (clear == false)
text.setText(null);

text.setText(text.getText() + "4");
clear = true;
}

if (temp == bt5) {
if (clear == false)
text.setText(null);

text.setText(text.getText() + "5");
clear = true;
}

if (temp == bt6) {
if (clear == false)
text.setText(null);

text.setText(text.getText() + "6");
clear = true;
}

if (temp == bt7) {
if (clear == false)
text.setText(null);

text.setText(text.getText() + "7");
clear = true;
}

if (temp == bt8) {
if (clear == true)

text.setText(null);
text.setText(text.getText() + "8");
clear = true;
}

if (temp == bt9) {
if (clear == false)
text.setText(null);

text.setText(text.getText() + "9");
clear = true;
}

if (e.getActionCommand().equals("+")) {
c = 1;
try {
clear = false;
a = Integer.parseInt(text.getText());

b += a;
text.setText(String.valueOf(b));
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
System.out.println("输入错误");
}

}
if (e.getActionCommand().equals("-")) {
c = 2;
try {
clear = false;
a = Integer.parseInt(text.getText());
b -= a;
text.setText(String.valueOf(b));
} catch (NumberFormatException e2) {
System.out.println("输入错误");
}
}
if(e.getActionCommand().equals("*")){
c= 3;
try{
clear = false;
a = Integer.parseInt(text.getText());
b*=a;
text.setText(String.valueOf(b));
}catch(NumberFormatException e3){
System.out.println("输入错误");
}
}
if(e.getActionCommand().equals("/")){
c = 4;
try{
clear = false;
a = Integer.parseInt(text.getText());
b/=a;
text.setText(String.valueOf(b));
}catch(ArithmeticException e4){
System.out.println("除数不能0");
}
}
if(e.getActionCommand().equals("=")){
if(c ==1){
try {
clear = false;
a = Integer.parseInt(text.getText());

b += a;
text.setText(String.valueOf(b));
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
System.out.println("输入错误");
}
}
else if(c==2){
try {
clear = false;
a = Integer.parseInt(text.getText());
b -= a;
text.setText(String.valueOf(b));
} catch (NumberFormatException e2) {
System.out.println("输入错误");
}
}
else if(c==3){
try{
clear = false;
a = Integer.parseInt(text.getText());
b*=a;
text.setText(String.valueOf(b));
}catch(NumberFormatException e3){
System.out.println("输入错误");
}

}
else if(c==4){
try{
clear = false;
a = Integer.parseInt(text.getText());
b/=a;
text.setText(String.valueOf(b));
}catch(ArithmeticException e4){
System.out.println("除数不能0");
}

}
}

}

}

public class Test {


public static void main(String[] args) {
// TODO Auto-generated method stub
MainFrame m = new MainFrame("计算器");
m.setVisible(true);
}

}

最佳答案
怎么一句注释都没有,这不是成心不让别人回答吗
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
铝的抗弯系数是多少。
NBA好看不
周迅主演的一部电影
表达早上刚睡醒的句子,有山字的诗句
没发育前ml处女膜会破吗
中医学院排名
幸运魔锤开什么盒子,最新的dnf怎么买不了幸运
其他人不能访问我的网页
灰色衬衫配什么外套,灰色衬衫配什么外套
100个大学生有99个答不出来的题
关于空间写日志文字的限制!
为什么空间里面买不到种子
友谊真的很那么脆弱吗?不堪一击,该怎样才能
我的电脑是10月30号买的 以前一直没加密码 昨
为何我打开空间会出现这个提示框
推荐资讯
Justin Bieber 的新专辑什么时候才会上市?
合肥北大青鸟怎么样? 上过的说下 。不要从网
怎么样才能算好男人?
求马克思佩恩2用女主角在阳台上掩护男主角的
英语才子进!!“首先,感谢父母这十八年来对
请问十月二十五日出生的是什么星座?
为什么、蜡笔小新的老婆跟着迪迦奥特曼跑了、
红仔陂地址在什么地方,想过去办事
组装电脑里面的东西用什么牌子的好。猫什么牌
大嘴新皮肤小狗多少钱,英雄联盟刀妹冰霜之刃
大家会刮痧么?要是刮错了不会有什么事吧
梦幻Tab小地图怎么变唯美版
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?