四则运算式java代码 java四则运算代码

java四则运算import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class calculator extends JFrame implements ActionListener {
privateboolean i=true,clickable = true ;//这里四则运算式java代码的clickabel 是用来判断小数点的 如果有小数点的话 clickable则为false就不对小数点进行添加四则运算式java代码了 i在下面有解释
private double value=https://www.04ip.com/post/0;//这个就是显示出来的值了
String operate = "=";//操作数 默认情况下是=号
JFrame jframe=new JFrame("计算机");
JTextField text1=new JTextField("0");
JButton back = new JButton("退格");
JButton daoshu = new JButton("1/x");
JButton remain = new JButton("%");
JButton PI = new JButton("PI");//显示圆周率小数点后7位
JButton square = new JButton("x^2");
JButton cube = new JButton("x^3");
JButton plu=new JButton("+");
JButton min=new JButton("-");
JButton mul=new JButton("*");
JButton equ=new JButton("=");
JButton div=new JButton("/");
JButton code=new JButton("+/-");
JButton point=new JButton(".");
JButton num1=new JButton("1");
JButton num2=new JButton("2");
JButton num3=new JButton("3");
JButton num4=new JButton("4");
JButton num5=new JButton("5");
JButton num6=new JButton("6");
JButton num7=new JButton("7");
JButton num8=new JButton("8");
JButton num9=new JButton("9");
JButton num0=new JButton("0");
JButton num00=new JButton("00");
JButton clear=new JButton("C");
calculator(){
jframe.setSize(400, 400);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
p.setBackground(Color.gray);
p.setLayout(new GridLayout(5, 5));//采用的布局方式为网格布局
PI.addActionListener(this);
daoshu.addActionListener(this);
remain.addActionListener(this);
square.addActionListener(this);
cube.addActionListener(this);
plu.addActionListener(this);
equ.addActionListener(this);
num1.addActionListener(this);
num2.addActionListener(this);
clear.addActionListener(this);
num3.addActionListener(this);
num4.addActionListener(this);
num5.addActionListener(this);
num6.addActionListener(this);
num7.addActionListener(this);
num8.addActionListener(this);
num9.addActionListener(this);
num0.addActionListener(this);
num00.addActionListener(this);
min.addActionListener(this);
code.addActionListener(this);
div.addActionListener(this);
point.addActionListener(this);
mul.addActionListener(this);
back.addActionListener(this);
jframe.add(p);
p.add(PI);
p.add(code);
p.add(daoshu);
p.add(back);
p.add(clear);
p.add(num7);
p.add(num8);
p.add(num9);
p.add(plu);
p.add(remain);
p.add(num4);
p.add(num5);
p.add(num6);
p.add(min);
p.add(square);
p.add(num1);
p.add(num2);
p.add(num3);
p.add(mul);
p.add(cube);
p.add(num0);
p.add(num00);
p.add(point);
p.add(div);
p.add(equ);
jframe.add(text1,"North");
jframe.setLocation(300, 300);
jframe.setVisible(true);
}
public void actionPerformed(ActionEvent evt)
{
String str1,str2;
Object soc=evt.getSource();
String s = evt.getActionCommand();//获取按钮的数据
if(soc==back){//进行退格操作
str1=text1.getText();
int n=str1.length();
text1.setText(str1.substring(0,n-1));
str2 = text1.getText();
}
else if (s.equals("x^2")) {
str1=text1.getText();
double k = Double.parseDouble(str1);
k *=k;
text1.setText(String.valueOf(k));
}
else if (s.equals("x^3")) {
str1=text1.getText();

推荐阅读