这个计算器是我上大二java课时写的程序设计,现在已经研一啦。记得当时可是写了好多天呢,以前不懂起名的讲究,大多用的都是ABCD代替,大家凑合看看,可以作为java课程设计的参考,主要有三个界面,主界面是A类,保存计算记录是B类,行列式的计算是C类
下面是A类代码及界面图(主界面)。
package yuxiaoye;
import java.awt.*;
import yuxiaoye.B;
import yuxiaoye.C;
import java.awt.Button;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.*;
import java.math.BigDecimal;
import javax.swing.*;
import org.jvnet.substance.SubstanceButtonUI;
import org.jvnet.substance.SubstanceLookAndFeel;
import org.jvnet.substance.border.StandardBorderPainter;
import org.jvnet.substance.button.BaseButtonShaper;
import org.jvnet.substance.button.ClassicButtonShaper;
import org.jvnet.substance.button.StandardButtonShaper;
import org.jvnet.substance.button.SubstanceButtonShaper;
import org.jvnet.substance.painter.StandardGradientPainter;
import org.jvnet.substance.skin.GreenMagicSkin;
import org.jvnet.substance.theme.SubstanceOliveTheme;
import org.jvnet.substance.title.Glass3DTitlePainter;
import org.jvnet.substance.watermark.SubstanceBinaryWatermark;
import org.jvnet.substance.watermark.SubstanceLatchWatermark;
@SuppressWarnings("serial")
public class A extends JFrame implements ActionListener{
private Panel p1=new Panel();
private Panel p2=new Panel();
private Panel p3=new Panel();
private Panel p4=new Panel();
private Panel p5=new Panel();
private TextField t1;
private TextField t2;
private Label l1;
StringBuffer str;
double x,y,z,m,d;
static String c;
int h,i;
private Button b1,b2,b3,b4,b5,b6,b7,b8,b9;
private Button a1,a2,a3,a4,a5;
private Button c1,c2,c3,c4;
private Button d1,d2,d3;
private Button e1,e2,e3,e4,e5,e6,e7;
private Button Y,X;
public A(){
super("你的姓名 你的学号");
java.awt.Container c=getContentPane();
t1=new TextField(34);
t1.setEditable(false);
p2.add(t1);
t2=new TextField(34);
t2.setEditable(false);
p2.add(t2);
l1=new Label("欢迎使用简易计算器!",Label.CENTER);
l1.setForeground(Color.blue);
//l1.setBackground(Color.blue);
Y=new Button("查看计算记录");
X=new Button("行列式计算");
d1=new Button("sin");
d2=new Button("cos");
d3=new Button("tan");
b1=new Button("1");
b2=new Button("2");
b3=new Button("3");
b4=new Button("4");
b5=new Button("5");
b6=new Button("6");
b7=new Button("7");
b8=new Button("8");
b9=new Button("9");
a1=new Button("+");
a2=new Button("-");
a3=new Button("*");
a4=new Button("/");
a5=new Button("%");
c1=new Button("c");
c2=new Button("=");
c3=new Button(".");
c4=new Button("0");
e1=new Button("x^2");
e2=new Button("x^n");
e3=new Button("√x");
e4=new Button("n√x");
e5=new Button("ln");
e6=new Button("log");
e7=new Button("π");
p1.add(d1);
p1.add(d2);
p1.add(d3);
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(b7);
p1.add(b8);
p1.add(b9);
p3.add(a1);
p3.add(e5);
p3.add(e1);
p3.add(a2);
p3.add(e6);
p3.add(e2);
p3.add(a3);
p3.add(e7);
p3.add(e3);
p3.add(a4);
p3.add(a5);
p4.add(c2);
p4.add(c1);
p4.add(c3);
p4.add(c4);
p5.add(Y);
p5.add(X);
p3.add(e4);
p5.add(l1);
p1.setLayout(new GridLayout(4,3));
p2.setLayout(new GridLayout(2,1));
p3.setLayout(new GridLayout(4,3));
p4.setLayout(new GridLayout(4,1));
p5.setLayout(new GridLayout(3,1));
c.add("Center",p1);
c.add("North",p2);
c.add("West",p3);
c.add("East",p4);
c.add("South",p5);
setSize(420,330);
c2.setForeground(Color.white);
c2.setBackground(Color.red);
a1.setForeground(Color.blue);
a2.setForeground(Color.blue);
a3.setForeground(Color.blue);
a4.setForeground(Color.blue);
//a5.setForeground(Color.blue);
setVisible(true);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
a1.addActionListener(this);
a2.addActionListener(this);
a3.addActionListener(this);
a4.addActionListener(this);
a5.addActionListener(this);
c1.addActionListener(this);
c2.addActionListener(this);
c3.addActionListener(this);
c4.addActionListener(this);
e1.addActionListener(this);
e2.addActionListener(this);
e3.addActionListener(this);
e4.addActionListener(this);
e5.addActionListener(this);
e6.addActionListener(this);
e7.addActionListener(this);
d1.addActionListener(this);
d2.addActionListener(this);
d3.addActionListener(this);
Y.addActionListener(this);
X.addActionListener(this);
str=new StringBuffer();
}
public void B()
{BigDecimal b=new BigDecimal(x);
double f1=b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
x=f1;}
public static void main(String[] args) {
// 装载可选择的主题
try {
//设置外观
UIManager.setLookAndFeel(new SubstanceLookAndFeel());
JFrame.setDefaultLookAndFeelDecorated(true);
//设置主题 .边框
SubstanceLookAndFeel.setCurrentTheme(new SubstanceOliveTheme());
//设置按钮外观
// SubstanceLookAndFeel.setCurrentButtonShaper((SubstanceButtonShaper) new SubstanceButtonUI());
//设置水印
//SubstanceLookAndFeel.setCurrentWatermark(new SubstanceLatchWatermark());
//设置边框
SubstanceLookAndFeel.setCurrentBorderPainter(new StandardBorderPainter());
//设置渐变渲染
//SubstanceLookAndFeel.setCurrentGradientPainter(new StandardGradientPainter());
//设置标题
SubstanceLookAndFeel.setCurrentTitlePainter(new Glass3DTitlePainter());
//皮肤
SubstanceLookAndFeel.setSkin(new GreenMagicSkin());
} catch (Exception e) {
System.out.println(e.getMessage());
}
@SuppressWarnings("unused")
A a=new A();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==b1){
t1.setText(t1.getText()+"1");
x=Double.parseDouble(t1.getText());
}
else if
(e.getSource()==b2){
t1.setText(t1.getText()+"2");
x=Double.parseDouble(t1.getText());
}
else if
(e.getSource()==b3){
t1.setText(t1.getText()+"3");
x=Double.parseDouble(t1.getText());
}
else if
(e.getSource()==b4){
t1.setText(t1.getText()+"4");
x=Double.parseDouble(t1.getText());
}
else if
(e.getSource()==b5){
t1.setText(t1.getText()+"5");
x=Double.parseDouble(t1.getText());
}
else if
(e.getSource()==b6){
t1.setText(t1.getText()+"6");
x=Double.parseDouble(t1.getText());
}
else if
(e.getSource()==b7){
t1.setText(t1.getText()+"7");
x=Double.parseDouble(t1.getText());
}
else if
(e.getSource()==b8){
t1.setText(t1.getText()+"8");
x=Double.parseDouble(t1.getText());
}
else if
(e.getSource()==b9){
t1.setText(t1.getText()+"9");
x=Double.parseDouble(t1.getText());
}
else if
(e.getSource()==c4){
t1.setText(t1.getText()+"0");
x=Double.parseDouble(t1.getText());
}
else if
(e.getSource()==c3){
t1.setText(t1.getText().trim()+".");
}else if
(e.getSource()==a5){
t1.setText(t1.getText());
x=Double.parseDouble(t1.getText());
}
else if
(e.getSource()==d1){
t2.setText("sin");
// x=Double.parseDouble(t1.getText());
h=5;
}
else if
(e.getSource()==d2){
t2.setText("cos");
// x=Double.parseDouble(t1.getText());
h=6;
}
else if
(e.getSource()==d3){
t2.setText("tan");
// x=Double.parseDouble(t1.getText());
h=7;
}
if
(e.getSource()==a1){
t1.setText("");
y=x;
t2.setText(y+"+");//加号
h=0;
}
if
(e.getSource()==a2){
y=x;
t1.setText("");
t2.setText(y+"-"); //减号
h=1;
}
if
(e.getSource()==a3){
t1.setText("");
y=x;
t2.setText(y+"*"); //乘号
h=2;
}
if
(e.getSource()==a4){
t1.setText("");
y=x;
t2.setText(y+"/");
//除号
h=3;}
if
(e.getSource()==a5){
t1.setText("");
t2.setText(x+"%"+"");
//百分号
h=4;}
if
(e.getSource()==e1){
t1.setText("");
t2.setText(x+"^2"+"");
//平方
h=8;}
if
(e.getSource()==e2){
t1.setText("");
y=x;
t2.setText(y+"^y");
//n次方
h=9;}
if
(e.getSource()==e3){
t1.setText(""+x);
t2.setText("√"+x);
//平方根
h=10;}
if
(e.getSource()==e4){
y=x;
t1.setText("");
t2.setText("y√"+y);
//开n次根号
h=11;}
if
(e.getSource()==e5){
t1.setText("");
t2.setText("ln"+x);
//求ln
h=12;}
if
(e.getSource()==e6){
y=x;
t1.setText("");
t2.setText("log"+x+"请输入对数");
//求log
h=13;}
if
(e.getSource()==e7){
x=Math.PI;
B();
t1.setText(""+x);
}
if
(e.getSource()==Y){
B bb=new B(); //查看计算记录
bb.E();
}
if
(e.getSource()==X){
C w=new C();
w.dd();
//行列式计算
}
if
(e.getSource()==c1){
str.setLength(0);
t1.setText("");
t2.setText(""); //清零
str.setLength(0);
}
if
(e.getSource()==c2){
if(h==0){
t2.setText(y+"+"+x+"=");
z=x+y;
x=z;
t1.setText(x+""); //等号
}
if(h==1){
t2.setText(y+"-"+x+"=");
z=y-x;
x=z;
B();
t1.setText(x+"");
}
if(h==2){
t2.setText(y+"*"+x+"=");
z=x*y;
x=z;
B();
t1.setText(x+"");
}
if(h==3){
t2.setText(y+"/"+x+"=");
z=y/x;
x=z;
B();
t1.setText(x+"");
}
if(h==4){
m=x;
z=x*0.01;
x=z;
t1.setText(x+"");
t2.setText(m+"%"+"=");
}
if(h==5){
m=x;
z=Math.sin(x);
x=z;
t1.setText(""+x);
t2.setText("sin"+m+"=");
}
if(h==6){
m=x;
z=Math.cos(x);
x=z;
t1.setText(""+x);
t2.setText("cos"+m+"=");
}
if(h==7){
m=x;
z=Math.tan(x);
x=z;
t1.setText(""+x);
t2.setText("tan"+m+"=");
}
if(h==8){
m=x;
z=x*x;
x=z;
t1.setText(x+"");
t2.setText(m+"^2"+"=");
}
if(h==9){
m=y;
d=x;
for(i=1;i<x;i++){
y=y*m;
}
z=y;
x=y;
B();
t1.setText(""+x);
t2.setText(m+"^"+d+"=");
}
if(h==10){
m=x;
y=Math.sqrt(x);
x=y;
//B();
t1.setText(""+x);
t2.setText("√"+m+"=");
}
if(h==11){
m=x;
z=Math.pow(y,1/x);
x=z;
B();
t1.setText(""+x);
t2.setText(m+"√"+y+"=");
}
if(h==12){
m=x;
z = Math.log(x);
x=z;
B();
t1.setText(""+x);
t2.setText("ln"+m+"=");
}
if(h==13){
m=x;
z =Math.log(x) / Math.log(y);
x=z;
B();
t1.setText(""+x);
t2.setText("log"+y+" "+m+"=");
}
c=t2.getText()+t1.getText();
B bb=new B();
bb.D();
}
}
}
下面是B类代码及界面(保存记录到文件):
package yuxiaoye;
import java.awt.Button;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JFrame;
import yuxiaoye.A;
@SuppressWarnings("serial")
public class B extends JFrame implements ActionListener{
/**
*/
static JFrame jf=new JFrame("计算记录");
static Panel panel1=new Panel();
static Panel panel2=new Panel();
static TextArea area=new TextArea();
static Button button=new Button("隐藏");
static Button button1=new Button("清除记录");
static Button button2=new Button("保存到文件");
public static void main(String[] args)throws IOException {
}
public void D(){
jf.add(area);
jf.setSize(500,300);
jf.setVisible(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
area.setText(area.getText());
area.append(A.c+"\n");
}
public void E(){
jf.add(panel1);
jf.add(panel2);
panel2.add(area);
area.setVisible(true);
panel2.add(button);
panel2.add(button1);
panel2.add(button2);
jf.setSize(440,300);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.addActionListener(this);
button1.addActionListener(this);
button2.addActionListener(this);
area.setText(area.getText());
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if
(e.getSource()==button){
jf.setVisible(false);
}
if
(e.getSource()==button1){
area.setText("");
}
if
(e.getSource()==button2){
try{
FileInputStream f2 = null;
f2 = new FileInputStream("abc.txt");
byte input[]=new byte[1000];
f2.read(input);
FileOutputStream f3 = null;
f3 = new FileOutputStream("abc.txt");
String a=area.getText();
byte output[]=a.getBytes("GB2312");
f3.write(output);
f3.write(input);
f2.close();
f3.close();}
catch(IOException e1)
{e1.printStackTrace();}
}
}
}
下面是C类代码及界面(计算行列式):
package yuxiaoye;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class C extends JFrame implements ActionListener{
JFrame J1;
Panel pd1,pd2;
Label ld1,ld2;
Button bd,guanbi;
int value;
TextField td1,td2,td3,td4,td5,td6,td7,td8,td9,td10,td11,td12,td13,td14,td15,td16,td17;
public static void main(String[] args) {
C nn=new C();
nn.dd();
}
public void dd(){
bd=new Button("计算");
guanbi=new Button("隐藏");
J1=new JFrame();
pd1=new Panel();
pd2=new Panel();
ld1=new Label("行列式计算 ");
ld2=new Label("从第一行第一个开始输入");
td1=new TextField(7);
td2=new TextField();
td3=new TextField();
td4=new TextField();
td5=new TextField();
td6=new TextField();
td7=new TextField();
td8=new TextField();
td9=new TextField();
td10=new TextField();
td11=new TextField();
td12=new TextField();
td13=new TextField();
td14=new TextField();
td15=new TextField();
td16=new TextField();
td17=new TextField(5);
td17.setEditable(false);
J1.add(ld1);
J1.add(ld2);
pd1.setSize(300, 130);
//pd1.add(ld2);
pd1.add(td1);
pd1.add(td2);
pd1.add(td3);
pd1.add(td4);
pd1.add(td5);
pd1.add(td6);
pd1.add(td7);
pd1.add(td8);
pd1.add(td9);
pd1.add(td10);
pd1.add(td11);
pd1.add(td12);
pd1.add(td13);
pd1.add(td14);
pd1.add(td15);
pd1.add(td16);
pd2.add(bd);
pd2.add(td17);
pd2.add(guanbi);
pd1.setLayout(new GridLayout(4,4));
pd2.setLayout(new FlowLayout());
J1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
J1.setLayout(new FlowLayout());
J1.add(pd1);
J1.add(pd2);
J1.setVisible(true);
J1.setSize(350,230);
bd.addActionListener(this);
guanbi.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==guanbi){
J1.setVisible(false);
}
if(e.getSource()==bd){
if(td3.getText().trim().equals("")&&td4.getText().trim().equals("")){
int v1=Integer.parseInt(td1.getText());
int v2=Integer.parseInt(td2.getText());
int v5=Integer.parseInt(td5.getText());
int v6=Integer.parseInt(td6.getText());
value=v1*v6-v2*v5;
td17.setText(""+value);
}
if(td3.getText().length()>0&&td4.getText().trim().equals("")){
int v1=Integer.parseInt(td1.getText());
int v2=Integer.parseInt(td2.getText());
int v3=Integer.parseInt(td3.getText());
int v5=Integer.parseInt(td5.getText());
int v6=Integer.parseInt(td6.getText());
int v7=Integer.parseInt(td7.getText());
int v9=Integer.parseInt(td9.getText());
int v10=Integer.parseInt(td10.getText());
int v11=Integer.parseInt(td11.getText());
value=v1*v6*v11+v2*v7*v9+v3*v5*v10-v3*v6*v9-v2*v5*v11-v1*v7*v10;
td17.setText(""+value);
}
if(td4.getText().length()>0){
int v1=Integer.parseInt(td1.getText());
int v2=Integer.parseInt(td2.getText());
int v3=Integer.parseInt(td3.getText());
int v4=Integer.parseInt(td4.getText());
int v5=Integer.parseInt(td5.getText());
int v6=Integer.parseInt(td6.getText());
int v7=Integer.parseInt(td7.getText());
int v8=Integer.parseInt(td8.getText());
int v9=Integer.parseInt(td9.getText());
int v10=Integer.parseInt(td10.getText());
int v11=Integer.parseInt(td11.getText());
int v12=Integer.parseInt(td12.getText());
int v13=Integer.parseInt(td13.getText());
int v14=Integer.parseInt(td14.getText());
int v15=Integer.parseInt(td15.getText());
int v16=Integer.parseInt(td16.getText());
value=v1*(v6*v11*v16+v8*v15*v10+v7*v12*v14-v8*v11*v14-v6*v15*v12-v7*v10*v16)
-v5*(v2*v11*v16+v4*v15*v10+v3*v12*v14-v4*v11*v14-v2*v15*v12-v3*v10*v16)
+v9*(v2*v7*v16+v4*v15*v6+v3*v8*v14-v4*v7*v14-v2*v15*v8-v3*v6*v16)
-v13*(v2*v7*v12+v4*v11*v6+v3*v8*v10-v4*v7*v10-v2*v11*v8-v3*v6*v12);
td17.setText(""+value);
}
}
}
}