在期末实训写的一个计算器,希望可以帮到大家,如果有错误的地方,希望大家多多指点
package 计算器;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JFrame implements ActionListener {
private JPanel centerPanel=new JPanel();//中间面板
private String x="";//记录输入的数
private String y="";//记录输入的数
private String fh="";//记录输入的符号
private double answer;//记录答案
private JTextField tfDouble;// 显示表达式
private JTextField tfAnswer;// 显示答案
public Calculator(){
this.setBackground(Color. lightGray);// 设置背景颜色
tfDouble= new JTextField();// 初始化数字
tfDouble.setHorizontalAlignment(JTextField.LEFT);// 左对齐
tfDouble. setText ( "0.") ; //初始化答案
tfDouble.setBounds(10,20,370,50);// 设置边界
add(tfDouble, BorderLayout.NORTH);//添加文本框
tfAnswer = new JTextField();// 初始化答案
tfAnswer=new JTextField();//显示答案
tfAnswer. setHorizontalAlignment(JTextField. RIGHT);//右对齐
tfAnswer.setBounds(10,70,370,50);// 设置边界
add(tfAnswer,BorderLayout. CENTER);//添加答案
centerPanel. setLayout(new GridLayout(5,5));//创建中间面板
addButton("7", Color. blue);
addButton("8", Color. blue);
addButton("9", Color. blue);
addButton("/", Color. red);
addButton("CE", Color. green);
addButton("4", Color. blue);
addButton("5", Color. blue);
addButton("6", Color. blue);
addButton( "x", Color. red);
addButton("^", Color. blue);
addButton("1", Color. blue);
addButton("2", Color. blue);
addButton("3", Color. blue);
addButton("-", Color. red);
addButton("sqrt", Color. blue);
addButton("0", Color. blue);
addButton("+/-", Color. blue);
addButton( ".", Color. blue);
addButton("+", Color. red);
addButton("=", Color. red);
addButton("sin", Color. red);
addButton("cos", Color. red);
addButton("tan", Color.red);
addButton("ln", Color. red);
addButton("⬅", Color.red);
centerPanel.setBounds(10,120,370,250);//设置中间面板的位置
add(centerPanel, BorderLayout.SOUTH);// 添加中间面板
setLayout(new BorderLayout());// 添加布局
}
public void addButton(String name, Color color) {
JButton bt = new JButton(name);//
bt.setBackground(Color.pink);//设置按钮的背景色
bt.setForeground(color);//设置按钮的前景色
bt.addActionListener(this);//添加事件监听
centerPanel.add(bt);// 添加按钮
}
//计算功能实现
public void dengyu(String z){
if ( z. equals("+"))
answer=Double. parseDouble(x)+Double. parseDouble(y);
if ( z. equals("-"))
answer=Double. parseDouble(x)-Double. parseDouble(y);
if ( z. equals("x"))
answer=Double. parseDouble(x)*Double. parseDouble(y);
if ( z. equals("/"))
answer=Double. parseDouble(x)/Double. parseDouble(y);
if ( z. equals("^"))
answer=Math. pow(Double. parseDouble(x), Double. parseDouble(y));
if (z. equals("sin"))
answer=Math. sin(Double. parseDouble(x));
if ( z. equals("cos"))
answer=Math. cos(Double. parseDouble(x));
if (z. equals("tan"))
answer=Math. tan(Double. parseDouble(x));
if ( z. equals("ln"))
answer=Math. log(Double. parseDouble(x));
x=Double. toString(answer);//记录答案
tfAnswer. setText(x);//显示答案
y="" ;//清空
fh="";//清空
}
public void actionPerformed(ActionEvent e) throws IndexOutOfBoundsException {
//按钮的事件处理
if (e. getActionCommand(). equals("0")
|| e. getActionCommand( ) . equals ("1")
|| e. getActionCommand( ) . equals ("2")
|| e. getActionCommand( ) . equals ("3")
|| e. getActionCommand( ) . equals ("4")
|| e. getActionCommand( ) . equals ("5")
|| e. getActionCommand( ) . equals ("6")
|| e. getActionCommand( ) . equals ("7")
|| e. getActionCommand( ) . equals ("8")
|| e. getActionCommand( ) . equals ("9")){
if (fh. equals("")) /*清空*/{
x=x+e. getActionCommand();
if ( x . startsWith ( "00") )/*去掉第一个0*/ {
x=x. substring(1);
}
tfDouble. setText(x);
}else {
y=y+e. getActionCommand();
if ( y. startsWith ( " 00" ) ) {
y=y. substring(1);
}
tfDouble. setText(x+fh+y);
}
}
if (e. getActionCommand( ) . equals( ".")) {//小数
if (fh. equals("")) {
int i=0,j=0;
for ( i=0;i<x. length( );i++)
if ( x. charAt ( i ) == '.' )//判断是否为小数
j++;
if ( j==0)
x=x+ " ." ;
tfDouble. setText(x);
}else {
int i=0,j=0;
for ( i=0;i<y. length ( );i++)
if ( y. charAt ( i ) == '.' )
j++ ;
if ( j==0)
y=y+ " ." ;
tfDouble. setText(x+fh+y);
}
}
if (e.getActionCommand().equals("⬅")) {
if(fh.equals("")){
x = x.substring(0, x.length() - 1);// 截取前一个数
tfDouble.setText(x);
}
if(y.equals("")){
fh=fh.substring(0, fh.length() -1);// 截取前一个数
tfDouble.setText(x);
}
else {
y=y.substring(0, y.length() -1);// 截取前一个数
tfDouble.setText(x+fh+y);
}
}
if (e. getActionCommand(). equals("CE")){//清空
x="";
y="" ;
fh="";
tfDouble.setText("0.");
tfAnswer. setText("");//
}
if (e. getActionCommand(). equals("+/-")){//正负
if ( fh. equals( "")) {
if (x. substring(0,1). equals("-"))
x=x. substring(1);
else
x="- "+ x;
tfDouble. setText(x);
}else {
if (y. substring(0,1). equals("-"))
y=y. substring(1);
else
y="-"+y;
tfDouble. setText(x+fh+y);
}
}
if (e. getActionCommand(). equals("sqrt")){//平方根
if ( fh !="")
dengyu(fh);
answer=Math. sqrt(Double. parseDouble(x));
x=Double. toString(answer);
tfAnswer. setText(x);
}
if ( e. getActionCommand( ) . equals( "+")) {//加
if ( fh !="")
dengyu(fh);
fh="+";
tfDouble.setText(x+fh);
}
if (e. getActionCommand( ) . equals( "-")) {//减
if ( fh !="")
dengyu(fh);
fh="-";
tfDouble.setText(x+fh);
}
if ( e. getActionCommand( ) . equals ( "x") ) {//乘
if ( fh !="")
dengyu(fh);
fh="x";
tfDouble.setText(x+fh);
}
if (e. getActionCommand() . equals( "/")) {//除
if ( fh !="")
dengyu(fh);
fh="/";
tfDouble.setText(x+fh);
}
if ( e. getActionCommand( ) . equals( "^")) {//幂
if ( fh !="")
dengyu(fh);
fh="^";
}
if (e. getActionCommand(). equals("sin")){//正弦
if ( fh !="")
dengyu(fh);
answer=Math. sin(Double. parseDouble(x));
x=Double. toString (answer);
tfAnswer. setText(x);
}
if (e. getActionCommand(). equals("cos")){//余弦
if ( fh !="")
dengyu(fh);
answer=Math. cos(Double. parseDouble(x));
x=Double. toString(answer);
tfAnswer. setText(x);
}
if (e. getActionCommand(). equals("tan")){//正切
if ( fh !="")
dengyu(fh);
answer=Math. tan(Double. parseDouble(x));
x=Double.toString(answer);
tfAnswer. setText(x);
}
if (e. getActionCommand(). equals("ln")){//对数
if ( fh !="")
dengyu(fh);
answer=Math. log(Double. parseDouble(x));
x=Double. toString(answer);
tfAnswer. setText(x);
}
if ( e. getActionCommand( ). equals( "=")){// 等于
if(fh !="")
dengyu(fh);
tfAnswer.setText(x);
}
}
public static void main(String args[]){
Calculator c=new Calculator();//实力化对象
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口
c. setSize( 400,400) ;//设置窗口大小
c. setVisible(true);
}
}
效果图如下: