java GUI实现简易计算器

在期末实训写的一个计算器,希望可以帮到大家,如果有错误的地方,希望大家多多指点java GUI实现简易计算器_第1张图片

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

效果图如下:java GUI实现简易计算器_第2张图片

你可能感兴趣的:(java,java,开发语言)