gui 设计的简单计算器 java_利用java做一个简单的计算器

共两个类。还只是完成+、-、×、÷运算而已。

GUI只是用了AWT,很简单,相信一看就能懂了。

Calculator.java

public class Calculator{

private String result = "0";

private int op = 0,add = 1,sub = 2,mul = 3,div = 4;

private double stringToDouble(String x){

double y = Double.parseDouble(x);

return y;

}

private void operate(String x){

double x1 = stringToDouble(x);

double y = stringToDouble(result);

switch (op){

case 0:

result = x;

break;

case 1:

result = String.valueOf(y+x1);

break;

case 2:

result = String.valueOf(y-x1);

break;

case 3:

result = String.valueOf(y*x1);

break;

case 4:

if(x1!=0){

result = String.valueOf(y/x1);

}else{

result = "The divisor can't be zero!";

}

break;

}

}

public String opAdd(String x){

operate(x);

op = add;

return result;

}

public String opSubtract(String x){

operate(x);

op = sub;

return result;

}

public String opMultiply(String x){

operate(x);

op = mul;

return result;

}

public String opDivide(String x){

operate(x);

op = div;

return result;

}

public String opEquals(String x){

operate(x);

op = 0;

return result;

}

public void opClean(){

op = 0;

result = "0";

}

}

-------------------------------------------------------------------

第二个

CalculatorGUI.java

import java.awt.*;

import java.awt.event.*;

import java.util.EventObject;

public class CalculatorGUI{

private Frame f;

private Panel p1,p2;

private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;

private Button bPoint,bAdd,bDec,bMul,bDiv,bCal;

private TextField tf;

private String s,op;

private Calculator cal = new Calculator();

private boolean ifOp;

public CalculatorGUI(){

f = new Frame("Calculator");

p1 = new Panel();

p2 = new Panel();

b0 = new Button("0");

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");

bPoint = new Button(".");

bAdd = new Button("+");

bDec = new Button("-");

bMul = new Button("*");

bDiv = new Button("/");

bCal = new Button("=");

tf = new TextField(25);

tf.setEditable(false);

}

public void launchFrame(){

f.setSize(220,160);

f.setResizable(false);

f.addWindowListener(new myWindowListener());

p1.setLayout(new FlowLayout(FlowLayout.CENTER));

p1.add(tf);

f.add(p1,BorderLayout.NORTH);

p2.setLayout(new GridLayout(4,4));

b0.addActionListener(new setLabelText_ActionListener());

b1.addActionListener(new setLabelText_ActionListener());

b2.addActionListener(new setLabelText_ActionListener());

b3.addActionListener(new setLabelText_ActionListener());

b4.addActionListener(new setLabelText_ActionListener());

b5.addActionListener(new setLabelText_ActionListener());

b6.addActionListener(new setLabelText_ActionListener());

b7.addActionListener(new setLabelText_ActionListener());

b8.addActionListener(new setLabelText_ActionListener());

b9.addActionListener(new setLabelText_ActionListener());

bPoint.addActionListener(new setLabelText_ActionListener());

bAdd.addActionListener(new setOperator_ActionListener());

bDec.addActionListener(new setOperator_ActionListener());

bMul.addActionListener(new setOperator_ActionListener());

bDiv.addActionListener(new setOperator_ActionListener());

bCal.addActionListener(new setOperator_ActionListener());

p2.add(b7);

p2.add(b8);

p2.add(b9);

p2.add(bAdd);

p2.add(b4);

p2.add(b5);

p2.add(b6);

p2.add(bDec);

p2.add(b1);

p2.add(b2);

p2.add(b3);

p2.add(bMul);

p2.add(b0);

p2.add(bPoint);

p2.add(bCal);

p2.add(bDiv);

f.add(p2,BorderLayout.SOUTH);

f.setVisible(true);

}

public void setTextFieldText_Temp(){

if (tf.getText().length()<15 && (tf.getText().indexOf(".")==-1 || !s.equals("."))){

tf.setText(tf.getText()+s);

}else{

tf.setText((tf.getText()+s).substring(0,15));

}

}

public void setTextFieldText(){

if(ifOp){

ifOp = false;

tf.setText("");

setTextFieldText_Temp();

}else{

setTextFieldText_Temp();

}

}

public static void main(String[] args){

CalculatorGUI calculator = new CalculatorGUI();

calculator.launchFrame();

}

class myWindowListener extends WindowAdapter{

public void windowClosing(WindowEvent e){

System.exit(0);

}

}

class setLabelText_ActionListener implements ActionListener{

public void actionPerformed(ActionEvent e){

Button tempB = (Button)e.getSource();

s = tempB.getLabel();

setTextFieldText();

}

}

class setOperator_ActionListener implements ActionListener{

public void actionPerformed(ActionEvent e){

Button tempB = (Button)e.getSource();

op = tempB.getLabel();

if(op.equals("+")){

tf.setText(cal.opAdd(tf.getText()));

ifOp = true;

}else if(op.equals("-")){

tf.setText(cal.opSubtract(tf.getText()));

ifOp = true;

}else if(op.equals("*")){

tf.setText(cal.opMultiply(tf.getText()));

ifOp = true;

}else if(op.equals("/")){

tf.setText(cal.opDivide(tf.getText()));

ifOp = true;

}else if(op.equals("=")){

tf.setText(cal.opEquals(tf.getText()));

ifOp = true;

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以计算加减乘除的计算器小程序package zj lec1; import java awt BorderLayout; import java awt FlowLayout; import java awt GridLayout; import java awt event ActionEvent; import java awt event ActionListener; import java math BigInteger; import javax swing JButton; import javax swing JFrame; import javax swing JLabel; import javax swing JPanel; import javax swing JTextField; public class 简易计算机 extends JFrame implements ActionListener { JTextField a b c d; JButton ok exit; public 简易计算机 { this setLayout new FlowLayout ; this add a new JTextField 10 ; a setText "0" ; a setHorizontalAlignment JTextField RIGHT ; this add b new JTextField 1 ; this add c new JTextField 10 ; c setText "0" ; c setHorizontalAlignment JTextField RIGHT ; this add ok new JButton " " ; this add d new JTextField 10 ; this add exit new JButton "Exit" ; a addActionListener this ; b addActionListener this ; c addActionListener this ; exit addActionListener this ; ok addActionListener this ; this setSize 540 70 ; this setTitle "简易计算机" ; this setVisible true ; this setResizable false ; } public static void main String[] args { new 简易计算机 ; } @Override public void actionPerformed ActionEvent e { if e getSource ok || e getSource c || e getSource a { BigInteger ia new BigInteger a getText ; BigInteger ic new BigInteger c getText ; String ib b getText ; 判断是用加法 还是减法 还是乘法 除法 if ib contains "+" { BigInteger id ia add ic ; d setText id toString ; } else if ib contains " " { BigInteger id ia subtract ic ; d setText id toString ; } else if ib contains " " { BigInteger id ia multiply ic ; d setText id toString ; } else if ib contains " " { BigInteger id ia divide ic ; d setText id toString ; } } else if e getSource exit { System exit 0 ; } } }">可以计算加减乘除的计算器小程序package zj lec1; import java awt BorderLayout; import java awt FlowLayout; import java awt GridLayout; import java awt event ActionEvent; import java awt event ActionListener; import java math BigInteger; import javax swing JButton; import javax swing J [更多]
好的,我会尽力回答您的问题。下面是一个简单Java GUI计算器的实现代码,您可以参考一下: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CalculatorGUI implements ActionListener { private JFrame frame; private JPanel panel; private JTextField textField; private JButton[] buttons; private String[] buttonLabels = {"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"}; private double num1, num2, result; private char operator; public CalculatorGUI() { frame = new JFrame("Calculator"); panel = new JPanel(); textField = new JTextField(20); buttons = new JButton[buttonLabels.length]; num1 = 0.0; num2 = 0.0; result = 0.0; operator = ' '; textField.setEditable(false); textField.setHorizontalAlignment(JTextField.RIGHT); for (int i = 0; i < buttons.length; i++) { buttons[i] = new JButton(buttonLabels[i]); buttons[i].addActionListener(this); panel.add(buttons[i]); } panel.setLayout(new GridLayout(4, 4, 5, 5)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 300); frame.add(textField, BorderLayout.NORTH); frame.add(panel, BorderLayout.CENTER); frame.setVisible(true); } public static void main(String[] args) { CalculatorGUI calculator = new CalculatorGUI(); } public void actionPerformed(ActionEvent e) { for (int i = 0; i < buttons.length; i++) { if (e.getSource() == buttons[i]) { String buttonLabel = buttonLabels[i]; if (buttonLabel.matches("[0-9.]")) { if (operator == ' ') { num1 = Double.parseDouble(textField.getText() + buttonLabel); textField.setText(Double.toString(num1)); } else { num2 = Double.parseDouble(textField.getText() + buttonLabel); textField.setText(Double.toString(num2)); } } else if (buttonLabel.matches("[/*\\-+]")) { operator = buttonLabel.charAt(0); } else if (buttonLabel.equals("=")) { switch (operator) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': result = num1 / num2; break; } textField.setText(Double.toString(result)); num1 = result; num2 = 0.0; operator = ' '; } else if (buttonLabel.equals("C")) { num1 = 0.0; num2 = 0.0; result = 0.0; operator = ' '; textField.setText(""); } break; } } } } ``` 这个代码实现了一个基本的四则运算的计算器功能,界面简单,易于操作。您可以根据自己的需求进行修改和扩展。希望能对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值