⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calculator.java

📁 java版公式计算器
💻 JAVA
字号:
package calculator;



//import java.util.*;
import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.math.BigDecimal;
//主类 
class jsq extends Frame { 
//声明 
double op, pa1, pa2, pa3; 
String op1, op2, ll, s; 
boolean isResult = false; 
String h=" ",m="";
//public CalculateString x;
//设置标题 
Frame p=new Frame("Formula Calculator"); 
//创建按钮 
Panel p0 = new Panel(); 
Panel p1 = new Panel(); 
Panel p2 = new Panel(); 
Panel p3 = new Panel(); 
Panel p4 = new Panel(); 
Panel p5 = new Panel(); 
Panel p6 = new Panel(); 
Panel p7 = new Panel(); 


// 创建文本框 
JTextField t = new JTextField("0"); 
TextArea te = new TextArea(); 



//初始化按钮 
Button b1 = new Button("Backspace"); 
Button b2 = new Button("CE"); 
Button b3 = new Button("sin"); 
Button b4 = new Button("MC"); 
Button b5 = new Button("7"); 
Button b6 = new Button("8"); 
Button b7 = new Button("9"); 
Button b8 = new Button("/"); 
Button b9 = new Button("sqrt"); 
Button b10 = new Button("MR"); 
Button b11 = new Button("4"); 
Button b12 = new Button("5"); 
Button b13 = new Button("6"); 
Button b14 = new Button("*"); 
Button b15 = new Button("%"); 
Button b16 = new Button("MS"); 
Button b17 = new Button("1"); 
Button b18 = new Button("2"); 
Button b19 = new Button("3"); 
Button b20 = new Button("-"); 
Button b21 = new Button("1/x"); 
Button b22 = new Button("M+"); 
Button b23 = new Button("0"); 
Button b24 = new Button("+/-"); 
Button b25 = new Button("."); 
Button b26 = new Button("+"); 
Button b27 = new Button("="); 
Button ba = new Button("exit"); 
Button a1 = new Button("("); 
Button a2 = new Button("cos"); 
Button a3 = new Button(")"); 
Button a4 = new Button("  "); 


//创建JMenuBar 
MenuBar mbar = new MenuBar(); 


//
Menu menu1 = new Menu("File"); 
Menu menu2 = new Menu("help"); 
//Menu menu3 = new Menu("Edit"); 


//创建MenuItem 
MenuItem me1 = new MenuItem("copy"); 
MenuItem me2 = new MenuItem("paste"); 
MenuItem me3 = new MenuItem("about"); 
MenuItem me4 = new MenuItem("open"); 
MenuItem me5 = new MenuItem("source"); 
public void jbInit() { 
t.setHorizontalAlignment(JTextField.RIGHT); 


//创建菜单项 
this.setMenuBar(mbar); 
mbar.add(menu1); 
mbar.add(menu2); 
//mbar.add(menu3); 
menu1.add(me1); 
menu1.addSeparator(); 
menu1.add(me2); 
menu1.addSeparator(); 
menu1.add(me4); 
menu2.add(me3); 
//menu3.add(me5); 
//为me1创建快捷键 
me1.setShortcut(new MenuShortcut(67)); 
me1.setActionCommand("copy"); 
me2.setShortcut(new MenuShortcut(86)); 
me2.setActionCommand("paste"); 


//添加画板

this.add(p0); 
//为画板p0设置 布局 
p0.setLayout(new GridLayout(9, 6, 10, 5)); 
p0.setBackground(Color.GREEN);

p0.add(p1); 
//为画板p1设置 布局 
p1.setLayout(new BorderLayout()); 
p1.setBackground(Color.GREEN); 

p1.add(t); 
//为画板p7设置 布局 
p7.setLayout(new GridLayout(1, 4, 5, 5)); 
p7.setBackground(Color.GREEN); 
//向画板P7中添加按钮 
p0.add(p7); 
p7.add(a1); 
p7.add(a2); 
p7.add(a3); 
p7.add(a4); 
p0.add(p2); 
//为画板p0设置 布局 
p2.setLayout(new GridLayout(1, 4, 5, 5)); 
p2.setBackground(Color.GREEN); 
//向画板P2中添加按钮 
p2.add(ba); 
p2.add(b1); 
p2.add(b2); 
p2.add(b3); 
p0.add(p3); 
//为画板p0设置 布局 
p3.setLayout(new GridLayout(1, 6, 5, 5)); 
p3.setBackground(Color.GREEN); 
//向画板P3中添加按钮 
p3.add(b4); 
p3.add(b5); 
p3.add(b6); 
p3.add(b7); 
p3.add(b8); 
p3.add(b9); 
p0.add(p4); 
//为画板p0设置 布局 
p4.setLayout(new GridLayout(1, 6, 5, 5)); 
p4.setBackground(Color.GREEN); 
//向画板P4中添加按钮 
p4.add(b10); 
p4.add(b11); 
p4.add(b12); 
p4.add(b13); 
p4.add(b14); 
p4.add(b15); 
p0.add(p5); 
//为画板p0设置 布局 
p5.setLayout(new GridLayout(1, 6, 5, 5)); 
p5.setBackground(Color.GREEN); 
//向画板P5中添加按钮 
p5.add(b16); 
p5.add(b17); 
p5.add(b18); 
p5.add(b19); 
p5.add(b20); 
p5.add(b21); 
p0.add(p6); 
//为画板p0设置 布局 
p6.setLayout(new GridLayout(1, 6, 5, 5)); 
p6.setBackground(Color.GREEN); 
//向画板P6中添加按钮 
p6.add(b22); 
p6.add(b23); 
p6.add(b24); 
p6.add(b25); 
p6.add(b26); 
p6.add(b27); 
//f.add(pp); 
// f.add(te); 

//调整大小并显示 
this.pack(); 
this.setVisible(true); 
t.setEditable(false); 
t.setBackground(Color.WHITE); 


//******************************************************//
// 向按钮中增加监听器 

b2.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
String m = t.getText(); 
t.setText(" "); 
} 
}); 

b23.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
ll = t.getText(); 
if (ll.equals("0")) { 
} 
else { 
t.setText(t.getText() + "0");
  m=t.getText() + "0";
} 
} 
}); 


b17.addActionListener(new ActionListener() { 
public  void actionPerformed(ActionEvent e) { 
ll = t.getText(); 
if (ll.equals("0")) 
{ 
    h=h+"1";	
    t.setText(h);
} 
else { 
t.setText(t.getText() + "1"); 
 
 
} 

 m=m+"1";
} 

}); 



b18.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
ll = t.getText(); 
if (ll.equals("0")) 
{
	h=h+"2";	
    t.setText(h); 
} 

else { 
t.setText(t.getText() + "2"); 

} 
m=m+"2";

} 

}); 

b19.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
ll = t.getText(); 
if (ll.equals("0")) 
{
	h=h+"3";
    t.setText(h); 
} 
else { 
t.setText(t.getText() + "3"); 
}
m=m+"3";

} 
}); 

b11.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
ll = t.getText(); 
if (ll.equals("0")) 
{ 
	h=h+"4";
    t.setText(h); 
} 
else { 
t.setText(t.getText() + "4"); 
} 
m=m+"4";
} 
}); 

b12.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
ll = t.getText(); 
if (ll.equals("0")) 
{
	h=h+"5";
    t.setText(h); 
} 
else { 
t.setText(t.getText() + "5"); 
} 
m=m+"5";
} 
}); 

b13.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
ll = t.getText(); 
if (ll.equals("0")) 
{ 
	h=h+"6";
    t.setText(h); 
} 
else { 
t.setText(t.getText() + "6"); 
} 
m=m+"6";
} 
}); 

b5.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
ll = t.getText(); 
if (ll.equals("0")) 
{
	h=h+"7";
    t.setText(h); 
} 
else { 
t.setText(t.getText() + "7");
} 
m=m+"7";
} 
}); 

b6.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
ll = t.getText(); 
if (ll.equals("0")) 
{
	h=h+"8";
    t.setText(h); 
} 
else { 
t.setText(t.getText() + "8"); 
} 
m=m+"8";
} 
}); 

b7.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
ll = t.getText(); 
if (ll.equals("0")) 
{ 
	h=h+"9";
    t.setText(h); 
} 
else { 
t.setText(t.getText() + "9"); 
} 
m=m+"9";
} 
}); 



ba.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
System.exit(0); //设置退出按钮 
} 
});


b25.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
String s11 = t.getText(); 
if (s11.indexOf(".") < 0) { 
s11 = s11 + "."; 
t.setText(s11); 
} 
} 
}); 


b3.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
String string=e.getActionCommand() ; 
String mm = t.getText(); 
if(e.equals("弧度") ){ 
double nn = Double.parseDouble(mm); 
nn *= Math.PI / 180; 
nn = Math.sin(nn); 
nn = nn * 10000; 
nn = Math.rint(nn); 
nn = nn / 10000; 
t.setText(" " + nn); 
} 
else{ 
double num=Double.parseDouble(mm) ; 
t.setText(String.valueOf(Math.sin(num) ) ) ; 
} 
} 
});


//为sin添加监听器 
a2.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
String mm = t.getText(); 




} 
}); 

a1.addActionListener(new ActionListener() { 
	public void actionPerformed(ActionEvent e) { 
	String mm = t.getText(); 
	if (mm.equals("0")) 
	{ 
		h=h+"(";
	    t.setText(h); 
	} 
	else { 
	t.setText(t.getText() + "("); 
	}
	m=m+"(";
	}}); 


a3.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
String mm = t.getText(); 
if (mm.equals("0")) 
{ 
	h=h+")";
    t.setText(h); 
} 
else { 
t.setText(t.getText() + ")"); 
}
m=m+")";

}}); 



b26.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
op1 = t.getText();
if(op1.equals("0"))
{h=h+"+";
t.setText(h);
m=h+"+";
//op = '+'; //设置加号 
//System.out.println(h);
}
else { 
	t.setText(t.getText() + "+"); 

	}

m=m+"+";
} 

}); 
b20.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
op1 = t.getText(); 
if(op1.equals("0"))
{h=h+"-";
t.setText(h); 
//op = '-'; //设置减号 
//System.out.println(h);
}
else { 
	t.setText(t.getText() + "-");
	}
    m=m+"-";
} 
}); 
b14.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
op1 = t.getText(); 
if(op1.equals("0"))
{
//h=h+"*";
t.setText(h); 
//op = '*'; //设置乘号 
}
else { 
	t.setText(t.getText() + "*"); 
	}
m=m+"*";
} 
}); 
b8.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
op1 = t.getText();
if(op1.equals("0"))
{
h=h+"/";
t.setText(h); 
//op = '/'; //设置除号
}
else{ 
	t.setText(t.getText() + "/"); 
	}
m=m+"/";
} 
}); 


//设置等号按钮 
b27.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
	//////////////////////////////////////////////////////////
	
	
	
	
	
	
	//////////////////////////////////////////////////////////
op2 = t.getText();
//t.setText(""+ h);
System.out.println(m);

String l=m;
String[] strs = new String[]{ l };

for(int j=0;j<strs.length;j++)
{
	BigDecimal result = new CalculateString().calculateString(strs[j]);
	
	l=result.toString();
	t.setText(" " + l);
}


//设置清除按钮 
b8.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
op1 = t.getText(); 
t.setText(" "); 
//op='sin'; 
} 
}); 


 



/*pa1 = Double.parseDouble(op1); //字弧串变为双精度数 
pa2 = Double.parseDouble(op2); //字弧串变为双精度数 
//double pa3 =0; 
if (op == '+') 
pa3 = pa1 + pa2; 
if (op == '-') 
pa3 = pa1 - pa2; 
if (op == '*') 

pa3 = pa1 * pa2; 
if (op == '/') 
pa3 = pa1 / pa2;  */

} 
}); 


//增加菜单监听器 
me1.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
str = t.getText(); 
} 
}); 
me2.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
t.setText(str); 
} 
}); 
me3.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
dialog u=new dialog(); 
u.setSize(300,200) ; 
u.show() ; 
 
} 
}); 
me4.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 

} 
}) ; 
b21.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
try { 
double len =Double.parseDouble(t.getText()); 
len = (1 / len); 
if (t.getText() != null) { 
t.setText("" + len); 
} 
} 
catch (NumberFormatException ex) { 
} 
} 
}) ; 

b24.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 

try { 
int len = Integer.parseInt(t.getText()); 
len = ~len + 1; 
if (t.getText() != null) { 
t.setText("" + len); 
} 
} 
catch (NumberFormatException ex) { 
System.out .print("error") ; 
} 
} 
}) ; 

b1.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
String ss=t.getText() ; 

int len=ss.length(); 
if(ss!=null){ 
t.setText(ss.substring(0,len-1)) ; 
if(len==1){ 
t.setText("0"); 
} 
} 
b1_actionPerformed(e); 

} 
}) ; 

me5.addActionListener(new ActionListener() { 

public void actionPerformed(ActionEvent e) { 

dialog1 m=new dialog1(); 
m.setSize(300,300) ; 
m.show() ; 
} 
}) ; 

b9.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
double oo=Double.parseDouble(t.getText() ) ; 
t.setText(" ") ; 
t.setText(String.valueOf(-Math.sqrt(oo) ) ) ; 
t.setText(String.valueOf( Math.sqrt(oo) ) ) ; 
} 
}); 
t.addMouseListener(new MouseAdapter() { 
public void mouseClicked(MouseEvent e) { 

if(e.getClickCount() ==2){ 

System.exit(0) ; 
} 
} 
}); 

//增加窗口监听器 
this.addWindowListener(new WindowAdapter( ) { 
public void windowClosing(WindowEvent e) { 
System.exit(0); 
} 
}); 


//添加阶乘监听器 
/*a1.addActionListener(new ActionListener() { 

public void actionPerformed(ActionEvent e) { 
int mull = 1; 
int x = Integer.parseInt(t.getText()); //小数化整 
t.setText(" "); 
for (int i = 1; i <= x; i++) 
mull = mull * i; 
t.setText(String.valueOf(mull)); 
} 
}); 
*/



} 
String str; 
public jsq() { 
jbInit(); 
} 







public static void main(String[] args) { 
jsq u = new jsq(); //实例化a 
} 

void b1_actionPerformed(ActionEvent e) { 

} 
} 

class dialog extends JFrame{ 
JLabel jlabel=new JLabel(); 
JPanel jpanel=new JPanel(); 

dialog(){ 
this.getContentPane().add(jpanel) ; 
jpanel.setLayout(new BorderLayout( )) ; 
jpanel.add(jlabel) ; 
jlabel.setText("******Welcome to java formula  Calculator******") ; 
} 
} 

class dialog1 extends Frame{ 
ScrollPane scrollpane=new ScrollPane(); 
TextArea textarea=new TextArea (); 
dialog1(){ 
this.add(scrollpane) ; 
scrollpane.add(textarea) ; 
textarea.setText(" ") ; 
} 
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -