大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
学java的时候自己编的,很简单,能够连续输入计算式后进行计算
为辽阳等地区用户提供了全套网页设计制作服务,及辽阳网站建设行业解决方案。主营业务为成都网站建设、成都做网站、辽阳网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**简易计算器,能够进行简单的计算
*
* @see 2008.12.9
*/
public class CalculatorA
implements ActionListener{
private JFrame frame;
private JTextField field;
private JButton[] allButtons;
private JButton clearButton;
// private JButton backButton;
String result="";//保存结果
StringBuilder sb = new StringBuilder();//保存要进行的计算式
int x = 0; //用来判断上一次的事件类型
String str = "123+456-789*0.=/";
ArrayListString arrayList = new ArrayListString();//保存计算式,通过方法进行运算
public CalculatorA(){
frame = new JFrame("我的计算器v1.1");
frame.setLocation(300,300);
field = new JTextField(25);
allButtons = new JButton[16];
for(int i=0;iallButtons.length;i++){
allButtons[i]= new JButton(str.substring(i,i+1));
}
clearButton = new JButton("CLEAR");
// backButton = new JButton("——");
init();
setFondAndColor();
addEventHander();
}
public void init(){
frame.setLayout(new BorderLayout());
JPanel northPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel southPanel = new JPanel();
northPanel.setLayout(new FlowLayout());
centerPanel.setLayout(new GridLayout(4,4));
southPanel.setLayout(new FlowLayout());
northPanel.add(field);
for(int i=0;iallButtons.length;i++){
centerPanel.add(allButtons[i]);
}
southPanel.add(clearButton);
//southPanel.add(backButton);
frame.add(northPanel,BorderLayout.NORTH);
frame.add(centerPanel,BorderLayout.CENTER);
frame.add(southPanel,BorderLayout.SOUTH);
}
//设置输入字体
public void setFondAndColor(){
field.setFont(new Font("宋体",Font.BOLD,24));
field.setBackground(new Color(100,200,200));
field.setForeground(Color.RED);
//设置字体从右起始
field.setHorizontalAlignment(JTextField.RIGHT);
}
public void showMi(){
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void addEventHander(){
for(int i=0;iallButtons.length;i++){
allButtons[i].addActionListener(this);
}
clearButton.addActionListener(this);
// backButton.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();//取得当前事件返回的值
if("0123456789.".indexOf(str)!=-1){
if(x == 0){ //当x为0时表示还没有进行输入
result=str;
sb.append(str);
field.setText(str);
x = 1;
}
else if(x == 1){
result = result +str;
sb.append(str);
field.setText(result);
x = 1;
}
else if(x == 2){
sb.delete(0,sb.length());
result = result+str;
sb.append(str);
field.setText(result);
x = 1;
}
else if(x == 3){
result = str;
sb.delete(0,sb.length());
arrayList.clear();
field.setText(str);
sb.append(str);
field.setText(str);
x = 1;
}
else if(x == 4){
result ="";
sb.delete(0,sb.length());
arrayList.clear();
result = str;
sb.append(str);
field.setText(str);
x = 1;
}
else{
result = result +str;
sb.append(str);
field.setText(result);
x = 1;
}
}
else if("+*-/".indexOf(str)!=-1){
if(x == 0){
field.setText("");
x = 2;
}
else if(x == 1){
result = result + str;
arrayList.add(sb.toString());
arrayList.add(str);
sb.append(str);
field.setText(result);
x = 2;
}
else if(x == 2){
x = 2;
}
else if(x == 3){
field.setText(result+str);
arrayList.add(result);
arrayList.add(str);
result = result+str;
x = 2;
}
else if(x == 4){
result ="";
sb.delete(0,sb.length());
arrayList.clear();
x = 2;
}
else{
field.setText(result+str);
arrayList.add(result);
arrayList.add(str);
result = result+str;
x = 2;
}
}
else if("=".equals(str)){
if(x == 0){
field.setText("0");
arrayList.clear();
result = "0";
x = 3;
}
else if(x == 1){
try{
arrayList.add(sb.toString());
arrayList = getResult(arrayList);
result = arrayList.get(0);
field.setText(result);
arrayList.clear();
x = 3;
}catch(Exception e1){
field.setText("数据格式异常");
x = 0;
}
}
else if(x == 2){
field.setText("数据格式错误.....");
arrayList.clear();
x = 0;
}
else if(x == 3){
field.setText(result);
x = 3;
}
else if(x == 4){
result ="";
sb.delete(0,sb.length());
arrayList.clear();
x = 3;
}
else {
try{
arrayList.add(sb.toString());
arrayList = getResult(arrayList);
result = arrayList.get(0);
field.setText(result);
arrayList.clear();
x = 3;
}catch(Exception e1){
field.setText("数据格式异常");
x = 0;
}
}
}
else if("CLEAR".equals(str)){
arrayList.clear();
field.setText("0");
arrayList.add("0");
x = 4;
}
else{
if(result.length()1){
result = result.substring(0,result.length()-1);
if(sb.length()0){
sb.delete(sb.length()-1,sb.length());
}
else {
sb.delete(0,1);
}
field.setText(result);
x = 5;
}
else{
result = "";
sb.delete(0,sb.length());
arrayList.clear();
field.setText("0");
x = 0;
}
}
}
public static ArrayListString getResult(ArrayListString list){
String res = null;
String[] s = {"/","*","-","+"};
int i=0;
if(list.size()1){
for(;is.length;){
if(s[i].equals("/")){
for(int j=0;jlist.size();j++){
if(list.get(j).equals(s[i])){
res = Double.toString(Double.parseDouble(list.get(j-1))/Double.parseDouble(list.get(j+1)));
//本地的数据格式
NumberFormat nf = NumberFormat.getInstance();
res = nf.format(Double.parseDouble(res));
res = getChange(res);
list.set(j-1,res);
list.remove(j);
list.remove(j);
getResult(list);
}
}
i++;
}
else if(s[i].equals("*")){
for(int j=0;jlist.size();j++){
if(list.get(j).equals(s[i])){
res = Double.toString(Double.parseDouble(list.get(j-1))*Double.parseDouble(list.get(j+1)));
NumberFormat nf = NumberFormat.getInstance();
res = nf.format(Double.parseDouble(res));
res = getChange(res);
list.set(j-1,res);
list.remove(j);
list.remove(j);
getResult(list);
}
}
i++;
}
else if(s[i].equals("-")){
for(int j=0;jlist.size();j++){
if(list.get(j).equals(s[i])){
res = Double.toString(Double.parseDouble(list.get(j-1))-Double.parseDouble(list.get(j+1)));
NumberFormat nf = NumberFormat.getNumberInstance();
res = nf.format(Double.parseDouble(res));
res = getChange(res);
list.set(j-1,res);
list.remove(j);
list.remove(j);
getResult(list);
}
}
i++;
}
else {
for(int j=0;jlist.size();j++){
if(list.get(j).equals(s[i])){
res = Double.toString(Double.parseDouble(list.get(j-1))+Double.parseDouble(list.get(j+1)));
NumberFormat nf = NumberFormat.getInstance();
res = nf.format(Double.parseDouble(res));
res = getChange(res);
list.set(j-1,res);
list.remove(j);
list.remove(j);
getResult(list);
}
}
i++;
}
}
}
return list;
}
//对数字字符串进行排除不必要符号
public static String getChange(String res){
String s_temp = "";
char[] c = new char[res.length()];
for(int k=0;kc.length;k++){
c[k] = res.charAt(k);
}
for(int k=0;kc.length;k++){
if((c[k]= '0' c[k]= '9')|| c[k] == '.'){
s_temp += c[k];
}
}
return s_temp;
}
public static void main(String[] args){
new CalculatorA().showMi();
}
}
/*
* @(#)JCalculator.java 1.00 06/17/2015
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* A simple calculator program.
* pI saw this program in a QQ group, and help a friend correct it./p
*
* @author Singyuen Yip
* @version 1.00 12/29/2009
* @see JFrame
* @see ActionListener
*/
public class JCalculator extends JFrame implements ActionListener {
/**
* Serial Version UID
*/
private static final long serialVersionUID = -169068472193786457L;
/**
* This class help close the Window.
* @author Singyuen Yip
*
*/
private class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
int i;
// Strings for Digit Operator buttons.
private final String[] str = { "7", "8", "9", "/", "4", "5", "6", "*","1",
"2", "3", "-", ".", "0", "=", "+" };
// Build buttons.
JButton[] buttons = new JButton[str.length];
// For cancel or reset.
JButton reset = new JButton("CE");
// Build the text field to show the result.
JTextField display = new JTextField("0");
/**
* Constructor without parameters.
*/
public JCalculator() {
super("Calculator");
// Add a panel.
JPanel panel1 = new JPanel(new GridLayout(4, 4));
// panel1.setLayout(new GridLayout(4,4));
for (i = 0; i str.length; i++) {
buttons[i] = new JButton(str[i]);
panel1.add(buttons[i]);
}
JPanel panel2 = new JPanel(new BorderLayout());
// panel2.setLayout(new BorderLayout());
panel2.add("Center", display);
panel2.add("East", reset);
// JPanel panel3 = new Panel();
getContentPane().setLayout(new BorderLayout());
getContentPane().add("North", panel2);
getContentPane().add("Center", panel1);
// Add action listener for each digit operator button.
for (i = 0; i str.length; i++)
buttons[i].addActionListener(this);
// Add listener for "reset" button.
reset.addActionListener(this);
// Add listener for "display" button.
display.addActionListener(this);
// The "close" button "X".
addWindowListener(new WindowCloser());
// Initialize the window size.
setSize(800, 800);
// Show the window.
// show(); Using show() while JDK version is below 1.5.
setVisible(true);
// Fit the certain size.
pack();
}
public void actionPerformed(ActionEvent e) {
Object target = e.getSource();
String label = e.getActionCommand();
if (target == reset)
handleReset();
else if ("0123456789.".indexOf(label) 0)
handleNumber(label);
else
handleOperator(label);
}
// Is the first digit pressed?
boolean isFirstDigit = true;
/**
* Number handling.
* @param key the key of the button.
*/
public void handleNumber(String key) {
if (isFirstDigit)
display.setText(key);
else if ((key.equals(".")) (display.getText().indexOf(".") 0))
display.setText(display.getText() + ".");
else if (!key.equals("."))
display.setText(display.getText() + key);
isFirstDigit = false;
}
/**
* Reset the calculator.
*/
public void handleReset() {
display.setText("0");
isFirstDigit = true;
operator = "=";
}
double number = 0.0;
String operator = "=";
/**
* Handling the operation.
* @param key pressed operator's key.
*/
public void handleOperator(String key) {
if (operator.equals("+"))
number += Double.valueOf(display.getText());
else if (operator.equals("-"))
number -= Double.valueOf(display.getText());
else if (operator.equals("*"))
number *= Double.valueOf(display.getText());
else if (operator.equals("/"))
number /= Double.valueOf(display.getText());
else if (operator.equals("="))
number = Double.valueOf(display.getText());
display.setText(String.valueOf(number));
operator = key;
isFirstDigit = true;
}
public static void main(String[] args) {
new JCalculator();
}
}
运行界面:
import java.awt.Color;
import java.awt.Font;
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.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
class Normal{
double i,j;
public Normal(double num1,double num2){
i=num1;
j=num2;
}
public double puls(){
return i+j;
}
public double subtract(){
return i-j;
}
public double multiply(){
return i*j;
}
public double divide(){
return i/j;
}
public double surpuls(){
return i%j;
}
}
class scientific extends Normal{
public scientific(int num1, int num2) {
super(num1, num2);
}
}
public class calc extends JFrame{
public static void main(String[] args) {
viewNormal VN= new viewNormal("normal");
}
}
class viewNormal extends JFrame implements ActionListener{
JPanel jp1 = new JPanel(new GridLayout(4,3,5,5));
JPanel jp2 = new JPanel(new GridLayout(5,1,5,5));
JLabel jl;
JButton[] jb;
JButton jbs,jbo,jba,jbb,jbc,jby;
StringBuffer sb = new StringBuffer();
Normal normal;
int dot=0;
double fnum=0;
double lnum=0;
double result;
String sign=null;
public viewNormal(String title){
setTitle(title);
setLayout(null);
setVisible(true);
setBounds(200,200,305,350);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
jb= new JButton[12];
for(int i=0;i9;i++){
jb[i]=new JButton(""+(i+1));
jp1.add(jb[i]);
jb[i].addActionListener(this);
}
jb[9]=new JButton(".");
jb[10]=new JButton("0");
jb[11]=new JButton("=");
jb[9].addActionListener(this);
jb[10].addActionListener(this);
jb[11].addActionListener(this);
jp1.add(jb[9]);
jp1.add(jb[10]);
jp1.add(jb[11]);
jp1.setBounds(10, 100, 200, 200);
jbs= new JButton("+");jbo= new JButton("-");jba= new JButton("*");
jbb= new JButton("/");jby= new JButton("%");jbc= new JButton("C");
jbs.addActionListener(this);jbo.addActionListener(this);jba.addActionListener(this);
jbb.addActionListener(this);jby.addActionListener(this);jbc.addActionListener(this);
//jp2.add(jby);
jp2.add(jbs);jp2.add(jbo);jp2.add(jba);jp2.add(jbb);jp2.add(jbc);
jp2.setBounds(215, 100, 70, 200);
jl= new JLabel("0",JLabel.RIGHT);
jl.setFont(new Font("Batang",Font.BOLD, 20));
jl.setBorder(new LineBorder(Color.black,2));
jl.setBackground(Color.white);
jl.setBounds(10, 40, 275, 50);
jl.setOpaque(true);
add(jl);
add(jp1);
add(jp2);
}
//+
public void sum(){
lnum=Double.parseDouble(sb.toString());
normal=new Normal(fnum,lnum);
fnum=normal.puls();
result=fnum;
}
//-
private void sub() {
System.out.println(sb.toString());
lnum=Double.parseDouble(sb.toString());
normal=new Normal(fnum,lnum);
fnum=normal.subtract();
result=fnum;
}
//*
private void mul() {
lnum=Double.parseDouble(sb.toString());
normal=new Normal(fnum,lnum);
fnum=normal.multiply();
result=fnum;
}
// /
private void div() {
lnum=Double.parseDouble(sb.toString());
normal=new Normal(fnum,lnum);
fnum=normal.divide();
result=fnum;
}
//%
private void sur() {
lnum=Double.parseDouble(sb.toString());
normal=new Normal(fnum,lnum);
fnum=normal.surpuls();
result=fnum;
}
// =
private void same(){
if(sign.equals("+")){
sum();
}
if(sign.equals("-")){
sub();
}
if(sign.equals("*")){
mul();
}
if(sign.equals("/")){
div();
}
if(sign.equals("%")){
sur();
}
}
//result
public void Result(){
if(result%1!=0)
jl.setText(""+result);
else
{
int i=(int)result;
jl.setText(""+i);
}
}
@Override
public void actionPerformed(ActionEvent e) {
//System.out.println(sb.toString());
// 1~9
for(int i=0;i9;i++){
if(e.getSource()==jb[i]!sb.toString().equals("0")){
sb.append(jb[i].getText());
jl.setText(sb.toString());
}
else if(e.getSource()==jb[i]sb.toString().equals("0")){
int d=sb.length();
sb.delete(0, d);
sb.append(jb[i].getText());
jl.setText(sb.toString());
}
}
// 0
if(e.getSource()==jb[10]!sb.toString().equals("0")){
sb.append(jb[10].getText());
jl.setText(sb.toString());
}
// .
if(e.getSource()==jb[9]dot==0!sb.toString().equals("")){
dot++;
sb.append(jb[9].getText());
jl.setText(sb.toString());
}
// =
if(e.getSource()==jb[11]!sb.toString().equals("")){
same();
Result();
int d=sb.length();
sb.delete(0, d);
dot=0;
}
// +
if(e.getSource()==jbs!sb.toString().equals("")){
if(sign!="+"sign!=null)
same();
else
sum();
sign ="+";
Result();
int d=sb.length();
sb.delete(0, d);
dot=0;
}
//-
if(e.getSource()==jbo!sb.toString().equals("")){
if(fnum==0)
fnum=2*Double.parseDouble(sb.toString());
if(sign!="-"sign!=null)
same();
else
sub();
sign ="-";
Result();
int d=sb.length();
sb.delete(0, d);
dot=0;
}
//*
if(e.getSource()==jba!sb.toString().equals("")){
if(fnum==0)
fnum=1;
if(sign!="*"sign!=null)
same();
else
mul();
sign ="*";
Result();
int d=sb.length();
sb.delete(0, d);
dot=0;
}
// /
if(e.getSource()==jbb!sb.toString().equals("")){
if(fnum==0)
fnum=Math.pow(Double.parseDouble(sb.toString()),2);
if(sign!="/"sign!=null)
same();
else
div();
sign ="/";
Result();
int d=sb.length();
sb.delete(0, d);
dot=0;
}
//%
// if(e.getSource()==jby!sb.toString().equals("")){
// if(fnum==0){
// fnum=Double.parseDouble(sb.toString());
// result=fnum;
// }
// else {
// if(sign!="%"sign!=null)
// same();
// else{
// lnum=Double.parseDouble(sb.toString());
// normal=new Normal(fnum,lnum);
// fnum=normal.surpuls();
// result=fnum;
// }
// }
// sign ="%";
// Result();
// int d=sb.length();
// sb.delete(0, d);
// dot=0;
// }
//clear
if(e.getSource()==jbc){
int d=sb.length();
sb.delete(0, d);
jl.setText("0");
dot=0;
fnum=0;
lnum=0;
sign=null;
}
}
}
class viewScientific extends viewNormal{
public viewScientific(String title){
super(title);
setBounds(200,200,800,500);
}
}
//等号以后输入符号用不了, String转 double 本来就有错误,你可以用我的扩展成科学型的。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Counter extends WindowAdapter
{
static JFrame f=new JFrame("计算器");
static JTextField text1=new JTextField("0.");
static String source="";
static String cal="";
static String object="";
static boolean flag=false;
static boolean flag1=true;
static boolean flag2=false;
public void init()
{
try
{
Container c=f.getContentPane();
JPanel pan1=new JPanel();
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");
JButton b0=new JButton("0");
JButton b11=new JButton("+");
JButton b12=new JButton("-");
JButton b13=new JButton("*");
JButton b14=new JButton("/");
JButton b15=new JButton(".");
JButton b16=new JButton("=");
JButton bclar=new JButton("清零");
text1.setHorizontalAlignment(JTextField.RIGHT);
c.add(text1,"North");
c.add(pan1);
A aa=new A();
Result re=new Result();
Opertion op=new Opertion();
Clar cl=new Clar();
b1.addActionListener(aa);
b2.addActionListener(aa);
b3.addActionListener(aa);
b4.addActionListener(aa);
b5.addActionListener(aa);
b6.addActionListener(aa);
b7.addActionListener(aa);
b8.addActionListener(aa);
b9.addActionListener(aa);
b0.addActionListener(aa);
b11.addActionListener(op);
b12.addActionListener(op);
b13.addActionListener(op);
b14.addActionListener(op);
b16.addActionListener(re);
b15.addActionListener(aa);
bclar.addActionListener(cl);
pan1.add(b1);
pan1.add(b2);
pan1.add(b3);
pan1.add(b11);
pan1.add(b4);
pan1.add(b5);
pan1.add(b6);
pan1.add(b12);
pan1.add(b7);
pan1.add(b8);
pan1.add(b9);
pan1.add(b13);
pan1.add(b0);
pan1.add(b15);
pan1.add(b16);
pan1.add(b14);
pan1.add(bclar);
f.setSize(200,220);
f.setVisible(true);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
class A implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String a=text1.getText();
String s=e.getActionCommand();
if(a.equals("0.")||a.equals("+")||a.equals("-")||a.equals("*")||a.equals("/"))
text1.setText(s);
else {
if(flag2)
{
text1.setText(s);
flag2=false;
}
else
text1.setText(a+s);
}
}
}
class Opertion implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
cal=e.getActionCommand();
if(flag1==true)
source=text1.getText();
text1.setText(cal);
flag1=false;
flag=true;
}
}
class Result implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double num1;
num1=Double.parseDouble(source);
object=text1.getText();
double num2;
num2=Double.parseDouble(object);
double result=0;
if(cal.equals("+"))
result=num1+num2;
if(cal.equals("-"))
result=num1-num2;
if(cal.equals("*"))
result=num1*num2;
if(cal.equals("/"))
if(num2==0)
text1.setText("除数不能为0");
else
result=num1/num2;
String s1=Double.toString(result);
text1.setText(s1);
flag1=true;
flag2=true;
}
}
class Clar implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
text1.setText("0.");
}
}
public static void main(String[] args)
{
Counter count=new Counter();
count.init();
}
public void windowClosing(WindowEvent e){
System.exit(1);
}
public void windowOpened(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;public class CaculatorA {
private JFrame jf;
private JButton[] jbs;
private JTextField jtf;
private JButton clear;
private double num1,num2,jieguo;
private char c;
/**
* 构造方法实例化属性
*
*/
public CaculatorA(){
jf=new JFrame("我的计算器v1.0");
jtf=new JTextField(20);
clear=new JButton("clear");
jbs=new JButton[16];
String str="123+456-789*0./=";
for(int i=0; istr.length(); i++){
jbs[i]=new JButton(str.charAt(i)+"");
}
init();
addEventHandler();
// setFont();
// setColor();
showMe();
}
/**
package calculator;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.SystemColor;
import java.awt.Color;
/**
* pTitle:/p
*
* pDescription: 这个类用来实现一个简单的计算器/p
*
* pCopyright: Copyright (c) 2006年/p
*
* pCompany: /p
*
* @author
* @version 1.0
*/
public class CalculatorFrame extends JFrame {
boolean flag = false;
String operand1;
String operand2;
double result;
String action;
BorderLayout borderLayout1 = new BorderLayout();
JTextField txtresult = new JTextField();
JButton btn1 = new JButton();
JButton btn2 = new JButton();
JButton btn3 = new JButton();
JButton btn4 = new JButton();
JButton btn5 = new JButton();
JButton btn6 = new JButton();
JButton btn7 = new JButton();
JButton btn8 = new JButton();
JButton btn9 = new JButton();
JButton btn0 = new JButton();
JButton btnplus = new JButton();
JButton btnminus = new JButton();
JButton btnjultiply = new JButton();
JButton btndivide = new JButton();
JButton btnclear = new JButton();
JButton btnequal = new JButton();
public CalculatorFrame() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(null);
txtresult.setBackground(SystemColor.WHITE);
txtresult.setBorder(BorderFactory.createLoweredBevelBorder());
txtresult.setDisabledTextColor(Color.black);
txtresult.setEditable(false);
txtresult.setHorizontalAlignment(JTextField.RIGHT);
txtresult.setBounds(new Rectangle(50, 50, 305, 25));
btn1.setBounds(new Rectangle(50, 200, 65, 30));
btn1.setBorder(BorderFactory.createRaisedBevelBorder());
btn1.setText("1");
btn1.addActionListener(new CalculatorFrame_btn1_actionAdapter(this));
btn2.setBounds(new Rectangle(130, 200, 65, 30));
btn2.setBorder(BorderFactory.createRaisedBevelBorder());
btn2.setText("2");
btn2.addActionListener(new CalculatorFrame_btn2_actionAdapter(this));
btn3.setBounds(new Rectangle(210, 200, 65, 30));
btn3.setBorder(BorderFactory.createRaisedBevelBorder());
btn3.setText("3");
btn3.addActionListener(new CalculatorFrame_btn3_actionAdapter(this));
btn4.setBounds(new Rectangle(50, 150, 65, 30));
btn4.setBorder(BorderFactory.createRaisedBevelBorder());
btn4.setText("4");
btn4.addActionListener(new CalculatorFrame_btn4_actionAdapter(this));
btn5.setBounds(new Rectangle(130, 150, 65, 30));
btn5.setBorder(BorderFactory.createRaisedBevelBorder());
btn5.setText("5");
btn5.addActionListener(new CalculatorFrame_btn5_actionAdapter(this));
btn6.setBounds(new Rectangle(210, 150, 65, 30));
btn6.setBorder(BorderFactory.createRaisedBevelBorder());
btn6.setText("6");
btn6.addActionListener(new CalculatorFrame_btn6_actionAdapter(this));
btn7.setBounds(new Rectangle(50, 100, 65, 30));
btn7.setBorder(BorderFactory.createRaisedBevelBorder());
btn7.setText("7");
btn7.addActionListener(new CalculatorFrame_btn7_actionAdapter(this));
btn8.setBounds(new Rectangle(130, 100, 65, 30));
btn8.setBorder(BorderFactory.createRaisedBevelBorder());
btn8.setText("8");
btn8.addActionListener(new CalculatorFrame_btn8_actionAdapter(this));
btn9.setBounds(new Rectangle(210, 100, 65, 30));
btn9.setBorder(BorderFactory.createRaisedBevelBorder());
btn9.setText("9");
btn9.addActionListener(new CalculatorFrame_btn9_actionAdapter(this));
btn0.setBounds(new Rectangle(50, 250, 65, 30));
btn0.setBorder(BorderFactory.createRaisedBevelBorder());
btn0.setText("0");
btn0.addActionListener(new CalculatorFrame_btn0_actionAdapter(this));
btnplus.setBounds(new Rectangle(290, 250, 65, 30));
btnplus.setBorder(BorderFactory.createRaisedBevelBorder());
btnplus.setText("+");
btnplus.addActionListener(new CalculatorFrame_btnplus_actionAdapter(this));
btnminus.setBounds(new Rectangle(290, 200, 65, 30));
btnminus.setBorder(BorderFactory.createRaisedBevelBorder());
btnminus.setText("-");
btnminus.addActionListener(new CalculatorFrame_btnminus_actionAdapter(this));
btnjultiply.setBounds(new Rectangle(290, 150, 65, 30));
btnjultiply.setBorder(BorderFactory.createRaisedBevelBorder());
btnjultiply.setText("*");
btnjultiply.addActionListener(new
CalculatorFrame_btnjultiply_actionAdapter(this));
btndivide.setBounds(new Rectangle(290, 100, 65, 30));
btndivide.setBorder(BorderFactory.createRaisedBevelBorder());
btndivide.setText("/");
btndivide.addActionListener(new CalculatorFrame_btndivide_actionAdapter(this));
btnclear.setBounds(new Rectangle(130, 250, 65, 30));
btnclear.setBorder(BorderFactory.createRaisedBevelBorder());
btnclear.setText("C");
btnclear.addActionListener(new CalculatorFrame_btnclear_actionAdapter(this));
btnequal.setBounds(new Rectangle(210, 250, 65, 30));
btnequal.setBorder(BorderFactory.createRaisedBevelBorder());
btnequal.setText("=");
btnequal.addActionListener(new CalculatorFrame_btnequal_actionAdapter(this));
this.getContentPane().setBackground(UIManager.getColor(
"MenuItem.background"));
this.setTitle("计算器");
this.getContentPane().add(txtresult);
this.getContentPane().add(btn1);
this.getContentPane().add(btn0);
this.getContentPane().add(btnclear);
this.getContentPane().add(btnplus);
this.getContentPane().add(btnequal);
this.getContentPane().add(btn2);
this.getContentPane().add(btn3);
this.getContentPane().add(btnminus);
this.getContentPane().add(btn4);
this.getContentPane().add(btn7);
this.getContentPane().add(btn5);
this.getContentPane().add(btn6);
this.getContentPane().add(btnjultiply);
this.getContentPane().add(btndivide);
this.getContentPane().add(btn9);
this.getContentPane().add(btn8);
}
public void btnplus_actionPerformed(ActionEvent e) {
action = "plus";
operand1 = txtresult.getText();
txtresult.setText("");
flag = true;
}
public void btnminus_actionPerformed(ActionEvent e) {
action = "minus";
operand1 = txtresult.getText();
txtresult.setText("");
flag = true;
}
public void btnjultiply_actionPerformed(ActionEvent e) {
action = "multiply";
operand1 = txtresult.getText();
txtresult.setText("");
flag = true;
}
public void btndivide_actionPerformed(ActionEvent e) {
action = "divide";
operand1 = txtresult.getText();
txtresult.setText("");
flag = true;
}
public void btnclear_actionPerformed(ActionEvent e) {
txtresult.setText("");
}
public void btn1_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn1.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn1.getActionCommand());
}
}
public void btn2_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn2.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn2.getActionCommand());
}
}
public void btn3_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn3.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn3.getActionCommand());
}
}
public void btn4_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn4.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn4.getActionCommand());
}
}
public void btn5_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn5.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn5.getActionCommand());
}
}
public void btn6_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn6.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn6.getActionCommand());
}
}
public void btn7_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn7.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn7.getActionCommand());
}
}
public void btn8_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn8.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn8.getActionCommand());
}
}
public void btn9_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn9.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn9.getActionCommand());
}
}
public void btn0_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn0.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn0.getActionCommand());
}
}
public void btnequal_actionPerformed(ActionEvent e) {
double digit1, digit2;
operand2 = txtresult.getText();
if (flag == false) {
if (action.equals("plus")) {
digit1 = Double.parseDouble(operand1);
digit2 = Double.parseDouble(operand2);
result = digit1 + digit2;
txtresult.setText(String.valueOf((int) result));
flag = true;
} else if (action.equals("minus")) {
digit1 = Double.parseDouble(operand1);
digit2 = Double.parseDouble(operand2);
result = digit1 - digit2;
txtresult.setText(String.valueOf((int) result));
flag = true;
} else if (action.equals("multiply")) {
digit1 = Double.parseDouble(operand1);
digit2 = Double.parseDouble(operand2);
result = digit1 * digit2;
txtresult.setText(String.valueOf((int) result));
flag = true;
} else if (action.equals("divide")) {
digit1 = Double.parseDouble(operand1);
digit2 = Double.parseDouble(operand2);
result = digit1 / digit2;
if (digit2==0) {
txtresult.setText("ERROR");
flag=true;
} else {
txtresult.setText(String.valueOf((int) result));
flag = true;
}
}
}
}
class CalculatorFrame_btnequal_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btnequal_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnequal_actionPerformed(e);
}
}
class CalculatorFrame_btn0_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn0_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn0_actionPerformed(e);
}
}
class CalculatorFrame_btn9_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn9_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn9_actionPerformed(e);
}
}
class CalculatorFrame_btn8_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn8_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn8_actionPerformed(e);
}
}
class CalculatorFrame_btn7_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn7_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn7_actionPerformed(e);
}
}
class CalculatorFrame_btn6_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn6_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn6_actionPerformed(e);
}
}
class CalculatorFrame_btn5_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn5_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn5_actionPerformed(e);
}
}
class CalculatorFrame_btn4_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn4_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn4_actionPerformed(e);
}
}
class CalculatorFrame_btn2_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn2_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn2_actionPerformed(e);
}
}
class CalculatorFrame_btn3_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn3_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn3_actionPerformed(e);
}
}
class CalculatorFrame_btnclear_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btnclear_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnclear_actionPerformed(e);
}
}
class CalculatorFrame_btndivide_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btndivide_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btndivide_actionPerformed(e);
}
}
class CalculatorFrame_btnjultiply_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btnjultiply_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnjultiply_actionPerformed(e);
}
}
class CalculatorFrame_btnminus_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btnminus_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnminus_actionPerformed(e);
}
}
class CalculatorFrame_btnplus_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btnplus_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnplus_actionPerformed(e);
}
}
class CalculatorFrame_btn1_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn1_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn1_actionPerformed(e);
}
}
}
public class Calculator {
public static void main(String[] args) {
CalculatorFrame Objcal=new CalculatorFrame();
Objcal.setSize(410,320);
Objcal.setVisible(true);
}
}