总结:我用if()语句写计算功能的代码时,实现不了,与switch_-catch语句不一样。不知到怎么实现
package com.p;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//这是一个计算器
//用的是网格布局管理器,不是纯按钮组合,发现
class calc extends JFrame implements ActionListener {
JPanel jp;
JTextField jf;
double s = 0;// 这里是用来接收我所计算的表达式的值
boolean of = true;
double num1 = 0;
double num2 = 0;
char c = ' ';// 这是装所有运算符
JButton jb1, jb2, jb3, jb4, jb5, jb6, jb7, jb8, jb9, jb0, jb_jia, jb_jian,
jb_cheng, jb_chu, jb_deng, jb_dian;
calc() {
jf = new JTextField();// 这里不设置,也没有区别啊?
jf.setHorizontalAlignment(JTextField.RIGHT);// 设置光标最右显示
jp = new JPanel();
jb1 = new JButton("1");// 这是计算器上的按钮
jb2 = new JButton("2");// 这是计算器上的按钮
jb3 = new JButton("3");// 这是计算器上的按钮
jb4 = new JButton("4");// 这是计算器上的按钮
jb5 = new JButton("5");// 这是计算器上的按钮
jb6 = new JButton("6");// 这是计算器上的按钮
jb7 = new JButton("7");// 这是计算器上的按钮
jb8 = new JButton("8");// 这是计算器上的按钮
jb9 = new JButton("9");// 这是计算器上的按钮
jb0 = new JButton("0");// 这是计算器上的按钮
jb_jia = new JButton("+");// 这是计算器上的按钮
jb_jian = new JButton("-");// 这是计算器上的按钮
jb_cheng = new JButton("*");// 这是计算器上的按钮
jb_chu = new JButton("/");// 这是计算器上的按钮
jb_deng = new JButton("=");
jb_dian = new JButton(".");
// 注册监听,给每一个按钮
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
jb5.addActionListener(this);
jb6.addActionListener(this);
jb7.addActionListener(this);
jb8.addActionListener(this);
jb9.addActionListener(this);
jb0.addActionListener(this);
jb_jia.addActionListener(this);
jb_jian.addActionListener(this);
jb_cheng.addActionListener(this);
jb_chu.addActionListener(this);
jb_deng.addActionListener(this);
jb_dian.addActionListener(this);
// this.add(jf);//在按钮上添加文本框
jp.add(jb1);
jp.add(jb2);
jp.add(jb3);
jp.add(jb_jia);
jp.add(jb4);
jp.add(jb5);
jp.add(jb6);
jp.add(jb_jian);
jp.add(jb7);
jp.add(jb8);
jp.add(jb9);
jp.add(jb_cheng);
jp.add(jb0);
jp.add(jb_chu);
jp.add(jb_dian);
jp.add(jb_deng);
jp.setLayout(new GridLayout(4, 4));
this.add(jf, BorderLayout.NORTH);// 你看我并没有错,只是文本框没有显示出来,原因应该是,jframe默认的布局是居中,而我没有把它放在最上面
this.add(jp);
this.setBounds(100, 10, 300, 200);
this.setDefaultCloseOperation(3);
// this.setResizable(false);
this.setVisible(true);
}
// 这里main函数里的对象new错了。其实什么意义我都不懂啊。忘了真的是
public void actionPerformed(ActionEvent e) {
// 这是
String str = e.getActionCommand();// 获取所有的字符串
// 这里是按钮上点的字
if (str.equals("1") || str.equals("2") || str.equals(".")
|| str.equals("3") || str.equals("4") || str.equals("5")
|| str.equals("6") || str.equals("7") || str.equals("8")
|| str.equals("9")) {
if (of) {
jf.setText(str);
of = false;// 看吧这里只能输入一个数显示
} else {
jf.setText(jf.getText() + str);// 这里就可以把后来输入的数字添加在后面是个新数字
of = false;
}
} else if (str.equals("+") || str.equals("-") || str.equals("*")
|| str.equals("/")) {
num1 = Double.parseDouble(jf.getText());// 获取文本框内容的信息
// 这里从字符串中取出指定为索引位置的字符
c = str.charAt(0);
of = true;
} else if (str.equals("=")) {
num2 = Double.parseDouble(jf.getText());// 这是第二个数字
switch (c) {
case '+':
s = (num1 + num2);
break;
case '-':
s = (num1 - num2);
break;
case '*':
s = (num1 * num2);
break;
case '/':
s = (num1 / num2);
break;
}
jf.setText(s + " ");
of = true;
}
}
}
public class Deo {
public static void main(String[] args) {
new calc();
}
}