用JAVA编写一个类似于Windows操作系统的的科学计算器程序,要包括对数运算,阶乘运算等等。 20
展开全部
代码如下:
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.util.LinkedList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculation extends JPanel implements ActionListener{
int flag=1,n=0;//n标记上一步
String m="@";//暂存
LinkedList<String>num=new LinkedList<String>();
LinkedList<String>op=new LinkedList<String>();
String display="";
String str="";
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
JPanel p5=new JPanel();
JPanel p6=new JPanel();
Label l2=new Label("");
JTextField tf=new JTextField("",20);
JTextField tf1=new JTextField();
Button bt[]=new Button[27];
String[] ss={"BackSpace","CE","C","MC","MR","MS","M+","7","8","9","/","log",
"4","5","6","*","y^x","1","2","3","-","!","0","+/-",".","+","="};
public Calculation(){
setLayout(new BorderLayout(10,5));
//setResizable(false);
p1.setLayout(new BorderLayout(10,10));
p2.setLayout(new BorderLayout(10,10));//p5,p6
p3.setLayout(new GridLayout(1,3,5,5));//用于存放backspace,ce,c三键
p4.setLayout(new GridLayout(4,5,5,5));//用于存放数字区及附近共20键
p5.setLayout(new GridLayout(5,1,5,5));//用于存放MC,MR,MS,M+键及显示M状态文本框
p6.setLayout(new BorderLayout(10,5));//放p3 p4
tf.setBackground(new Color(255,255,255));
tf.setEditable(false);
tf1.setBackground(Color.gray);
tf1.setEditable(false);
tf1.setEnabled(false);
for(int i=0;i<27;i++){ //利用数组,构造带指定标签的按钮
bt[i]=new Button(ss[i]);
bt[i].addActionListener(this);
}
add(p1,BorderLayout.CENTER);
p1.add(tf,BorderLayout.NORTH);
p1.add(p2,BorderLayout.CENTER);
p2.add(p5,BorderLayout.WEST);
p2.add(p6,BorderLayout.CENTER);
p6.add(p3,BorderLayout.NORTH);
p6.add(p4,BorderLayout.CENTER);
p3.add(bt[0]);
p3.add(bt[1]);
p3.add(bt[2]);
p5.add(tf1);
p5.add(bt[3]);
p5.add(bt[4]);
p5.add(bt[5]);
p5.add(bt[6]);
for(int i=7;i<27;i++){
p4.add(bt[i]);
}
setBounds(400,200,286,215);
setVisible(true);
//this.setDefaultCloseOperation(3);
}
}
完整已发到邮箱。
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.util.LinkedList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculation extends JPanel implements ActionListener{
int flag=1,n=0;//n标记上一步
String m="@";//暂存
LinkedList<String>num=new LinkedList<String>();
LinkedList<String>op=new LinkedList<String>();
String display="";
String str="";
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
JPanel p5=new JPanel();
JPanel p6=new JPanel();
Label l2=new Label("");
JTextField tf=new JTextField("",20);
JTextField tf1=new JTextField();
Button bt[]=new Button[27];
String[] ss={"BackSpace","CE","C","MC","MR","MS","M+","7","8","9","/","log",
"4","5","6","*","y^x","1","2","3","-","!","0","+/-",".","+","="};
public Calculation(){
setLayout(new BorderLayout(10,5));
//setResizable(false);
p1.setLayout(new BorderLayout(10,10));
p2.setLayout(new BorderLayout(10,10));//p5,p6
p3.setLayout(new GridLayout(1,3,5,5));//用于存放backspace,ce,c三键
p4.setLayout(new GridLayout(4,5,5,5));//用于存放数字区及附近共20键
p5.setLayout(new GridLayout(5,1,5,5));//用于存放MC,MR,MS,M+键及显示M状态文本框
p6.setLayout(new BorderLayout(10,5));//放p3 p4
tf.setBackground(new Color(255,255,255));
tf.setEditable(false);
tf1.setBackground(Color.gray);
tf1.setEditable(false);
tf1.setEnabled(false);
for(int i=0;i<27;i++){ //利用数组,构造带指定标签的按钮
bt[i]=new Button(ss[i]);
bt[i].addActionListener(this);
}
add(p1,BorderLayout.CENTER);
p1.add(tf,BorderLayout.NORTH);
p1.add(p2,BorderLayout.CENTER);
p2.add(p5,BorderLayout.WEST);
p2.add(p6,BorderLayout.CENTER);
p6.add(p3,BorderLayout.NORTH);
p6.add(p4,BorderLayout.CENTER);
p3.add(bt[0]);
p3.add(bt[1]);
p3.add(bt[2]);
p5.add(tf1);
p5.add(bt[3]);
p5.add(bt[4]);
p5.add(bt[5]);
p5.add(bt[6]);
for(int i=7;i<27;i++){
p4.add(bt[i]);
}
setBounds(400,200,286,215);
setVisible(true);
//this.setDefaultCloseOperation(3);
}
}
完整已发到邮箱。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询