本文目录一览:
用java实现一个简单的计算器。
/*
* @(#)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();
}
}
运行界面:
如何用JAVA语言编写计算器小程序?
具体代码如下:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Calculator extends JFrame implements ActionListener {
private JFrame jf;
private JButton[] allButtons;
private JButton clearButton;
private JTextField jtf;
public Calculator() {
//对图形组件实例化
jf=new JFrame("任静的计算器1.0:JAVA版");
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(){
System.exit(0);
}
});
allButtons=new JButton[16];
clearButton=new JButton("清除");
jtf=new JTextField(25);
jtf.setEditable(false);
String str="123+456-789*0.=/";
for(int i=0;iallButtons.length;i++){
allButtons[i]=new JButton(str.substring(i,i+1));
}
}
public void init(){
//完成布局
jf.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(jtf);
for(int i=0;i16;i++){
centerPanel.add(allButtons[i]);
}
southPanel.add(clearButton);
jf.add(northPanel,BorderLayout.NORTH);
jf.add(centerPanel,BorderLayout.CENTER);
jf.add(southPanel,BorderLayout.SOUTH);
addEventHandler();
}
//添加事件监听
public void addEventHandler(){
jtf.addActionListener(this);
for(int i=0;iallButtons.length;i++){
allButtons[i].addActionListener(this);
}
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Calculator.this.jtf.setText("");
}
});
}
//事件处理
public void actionPerformed(ActionEvent e) {
//在这里完成事件处理 使计算器可以运行
String action=e.getActionCommand();
if(action=="+"||action=="-"||action=="*"||action=="/"){
}
}
public void setFontAndColor(){
Font f=new Font("宋体",Font.BOLD,24);
jtf.setFont(f);
jtf.setBackground(new Color(0x8f,0xa0,0xfb));
for(int i=0;i16;i++){
allButtons[i].setFont(f);
allButtons[i].setForeground(Color.RED);
}
}
public void showMe(){
init();
setFontAndColor();
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new Calculator().showMe();
}
}
用java语言,编写一个计算器
有一个现成的.你看看吧
import java.awt.event.ActionEvent;
public class Application extends JFrame {
protected String str = "";
protected boolean isChar = true;
protected boolean isEqual = false;
protected JTextField textField;
public Application() {
Listener listerner = new Listener(this);
getContentPane().setLayout(null);
JButton button = new JButton("7");
button.addActionListener(listerner);
button.setBounds(12, 69, 43, 27);
getContentPane().add(button);
textField = new JTextField();
textField.setText("0");
textField.setEditable(false);
textField.setHorizontalAlignment(JTextField.RIGHT);
textField.setBounds(12, 22, 377, 27);
getContentPane().add(textField);
textField.setColumns(10);
JButton button_1 = new JButton("8");
button_1.addActionListener(listerner);
button_1.setBounds(103, 69, 43, 27);
getContentPane().add(button_1);
JButton button_2 = new JButton("9");
button_2.addActionListener(listerner);
button_2.setBounds(182, 69, 43, 27);
getContentPane().add(button_2);
JButton button_3 = new JButton("4");
button_3.addActionListener(listerner);
button_3.setBounds(12, 106, 43, 27);
getContentPane().add(button_3);
JButton button_4 = new JButton("5");
button_4.addActionListener(listerner);
button_4.setBounds(103, 106, 43, 27);
getContentPane().add(button_4);
JButton button_5 = new JButton("6");
button_5.addActionListener(listerner);
button_5.setBounds(182, 106, 43, 27);
getContentPane().add(button_5);
JButton button_6 = new JButton("1");
button_6.addActionListener(listerner);
button_6.setBounds(12, 143, 43, 27);
getContentPane().add(button_6);
JButton button_7 = new JButton("2");
button_7.addActionListener(listerner);
button_7.setBounds(103, 143, 43, 27);
getContentPane().add(button_7);
JButton button_8 = new JButton("3");
button_8.addActionListener(listerner);
button_8.setBounds(182, 143, 43, 27);
getContentPane().add(button_8);
JButton button_9 = new JButton("+");
button_9.addActionListener(listerner);
button_9.setBounds(269, 72, 43, 27);
getContentPane().add(button_9);
JButton button_10 = new JButton("-");
button_10.addActionListener(listerner);
button_10.setBounds(346, 72, 43, 27);
getContentPane().add(button_10);
JButton button_11 = new JButton("*");
button_11.addActionListener(listerner);
button_11.setBounds(269, 109, 43, 27);
getContentPane().add(button_11);
JButton button_12 = new JButton("/");
button_12.addActionListener(listerner);
button_12.setBounds(346, 109, 43, 27);
getContentPane().add(button_12);
JButton button_13 = new JButton("=");
button_13.addActionListener(listerner);
button_13.setBounds(346, 143, 43, 27);
getContentPane().add(button_13);
JButton button_14 = new JButton("0");
button_14.addActionListener(listerner);
button_14.setBounds(103, 180, 43, 27);
getContentPane().add(button_14);
JButton btnReset = new JButton("reset");
btnReset.addActionListener(listerner);
btnReset.setBounds(269, 180, 118, 27);
getContentPane().add(btnReset);
JButton button_15 = new JButton(".");
button_15.addActionListener(listerner);
button_15.setBounds(269, 146, 43, 27);
getContentPane().add(button_15);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setSize(442, 260);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new Application();
}
}
class Listener implements ActionListener {
private Application app = null;
public Listener(Application app) {
this.app = app;
}
public void actionPerformed(ActionEvent e) {
String value = e.getActionCommand();
if (value.matches("[0-9.]")) {
if (app.isChar) {
app.textField.setText("");
app.isChar = false;
}
if (app.isEqual app.str.matches("[0-9.]*")) {
app.str = "";
app.isEqual = false;
}
app.str += value;
app.textField.setText(app.textField.getText() + value);
} else if (value.matches("[\\+\\-\\*/]")) {
if (!app.str.substring(app.str.length() - 1)
.matches("[\\+\\-\\*/]")) {
app.str += value;
app.isChar = true;
}
} else if ("=".equals(value)) {
app.isEqual = true;
if (app.str.substring(app.str.length() - 1).matches("[\\+\\-]")) {
app.str += "0";
} else if (app.str.substring(app.str.length() - 1)
.matches("[\\*/]")) {
app.str += "1";
}
Interpreter bsh = new Interpreter();
String obj = null;
try {
obj = bsh.eval(app.str).toString();
} catch (Exception exception) {
System.out.println(exception.getMessage());
}
System.out.println(app.str);
app.textField.setText(obj);
app.str = obj;
app.isChar = true;
} else {
app.str = "";
app.textField.setText("0");
}
}
}
用JAVA编写一个计算器
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.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class Jisuanqi extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
Result result = new Result(); // 定义text的面板
Number_Key number_key = new Number_Key(); // 定义按钮面板
// 当点击按钮+、-、*、/时,com = true
boolean com = false;
// 当i=0时说明是我们第一次输入,字符串text不会累加
int i = 0;
// 存放text的内容
String text = "";
// 存放点击按钮+、-、*、/之前的数值
double defbutton = 0;
// +、-、*、/的代号分别为1,2,3,4
int symbol = 0;
// 构造函数
Jisuanqi() {
super("WangJiao"); // 设定标题
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设定关闭窗体时退出程序
JPanel pane = new JPanel(); // 定义主面板
pane.setLayout(new BorderLayout());
setBounds(380, 220, 30, 80); // 前两个参数是在屏幕上显示的坐标,后两个是大小
// 替换图标
ImageIcon icon = new ImageIcon("F:1.GIF");
// Jisuanqi.class.getResource("APPLE.GIF")
// );
setIconImage(icon.getImage());
pane.add(result, BorderLayout.NORTH);
pane.add(number_key, BorderLayout.CENTER);
pane.add(number_key.equal, BorderLayout.SOUTH);
number_key.one.addActionListener(this); // 对1按钮添加监听事件
number_key.two.addActionListener(this); // 对2按钮添加监听事件
number_key.three.addActionListener(this); // 对3按钮添加监听事件
number_key.four.addActionListener(this); // 对4按钮添加监听事件
number_key.five.addActionListener(this); // 对5按钮添加监听事件
number_key.six.addActionListener(this); // 对6按钮添加监听事件
number_key.seven.addActionListener(this); // 对7按钮添加监听事件
number_key.eight.addActionListener(this); // 对8按钮添加监听事件
number_key.nine.addActionListener(this); // 对9按钮添加监听事件
number_key.zero.addActionListener(this); // 对0按钮添加监听事件
number_key.ce.addActionListener(this); // 对置零按钮添加监听事件
number_key.plus.addActionListener(this); // 对+按钮添加监听事件
number_key.equal.addActionListener(this); // 对=按钮添加监听事件
number_key.sub.addActionListener(this); // 对-按钮添加监听事件
number_key.mul.addActionListener(this); // 对*按钮添加监听事件
number_key.div.addActionListener(this); // 对/按钮添加监听事件
number_key.point.addActionListener(this); // 对.按钮添加监听事件
setContentPane(pane);
pack(); // 初始化窗体大小为正好盛放所有按钮
}
// 各个按钮触发的事件
public void actionPerformed(ActionEvent e) {
/*
* 如果是点击数字按钮那么先要判断是否在此之前点击了+、-、*、/、=,如果是那么com=true 如果没有com=
* false;或者是否点击数字键,如果是i = 1,如果没有 i = 0;
*/
if (e.getSource() == number_key.one) {
if (com || i == 0) {
result.text.setText("1");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "1");
}
} else if (e.getSource() == number_key.two) {
if (com || i == 0) {
result.text.setText("2");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "2");
}
} else if (e.getSource() == number_key.three) {
if (com || i == 0) {
result.text.setText("3");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "3");
}
} else if (e.getSource() == number_key.four) {
if (com || i == 0) {
result.text.setText("4");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "4");
}
} else if (e.getSource() == number_key.five) {
if (com || i == 0) {
result.text.setText("5");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "5");
}
} else if (e.getSource() == number_key.six) {
if (com || i == 0) {
result.text.setText("6");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "6");
}
} else if (e.getSource() == number_key.seven) {
if (com || i == 0) {
result.text.setText("7");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "7");
}
} else if (e.getSource() == number_key.eight) {
if (com || i == 0) {
result.text.setText("8");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "8");
}
} else if (e.getSource() == number_key.nine) {
if (com || i == 0) {
result.text.setText("9");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "9");
}
}
/*
* 对于0这个按钮有一定的说法,在我的程序里不会出现如00000这样的情况,我加了判断条件就是
* 如果text中的数值=0就要判断在这个数值中是否有.存在?如果有那么就在原来数值基础之上添 加0;否则保持原来的数值不变
*/
else if (e.getSource() == number_key.zero) { // result.text.getText()是得到text里内容的意思
if (com || i == 0) {
result.text.setText("0");
com = false;
i = 1;
} else {
text = result.text.getText();
if (Float.parseFloat(text) 0 || Float.parseFloat(text) 0) { // Float.parseFloat(text)就是类型转换了,下面都是一样
result.text.setText(text + "0");
} else {
if (text.trim().indexOf(".") == -1) {
result.text.setText(text);
} else {
result.text.setText(text + "0");
}
}
}
} else if (e.getSource() == number_key.ce) {
result.text.setText("0");
i = 0;
com = true;
// text = "";
defbutton = 0;
}
/*
* 本程序不会让一个数值中出现2个以上的小数点.具体做法是:判断是否已经存在.存在就不添加, 不存在就添加.
*/
else if (e.getSource() == number_key.point) {
if (com || i == 0) {
result.text.setText("0.");
com = false;
i = 1;
} else {
text = result.text.getText();
if (text.trim().indexOf(".") == -1) {
result.text.setText(text + ".");
} else {
result.text.setText(text);
}
}
} // 获得点击+之前的数值
else if (e.getSource() == number_key.plus) {
com = true;
i = 0;
defbutton = Double.parseDouble(result.text.getText());
symbol = 1;
} // 获得点击-之前的数值
else if (e.getSource() == number_key.sub) {
com = true;
i = 0;
defbutton = Double.parseDouble(result.text.getText());
symbol = 2;
} // 获得点击*之前的数值
else if (e.getSource() == number_key.mul) {
com = true;
i = 0;
defbutton = Double.parseDouble(result.text.getText());
System.out.println(defbutton);
symbol = 3;
} // 获得点击/之前的数值
else if (e.getSource() == number_key.div) {
com = true;
i = 0;
defbutton = Double.parseDouble(result.text.getText());
symbol = 4;
} else if (e.getSource() == number_key.equal) {
switch (symbol) {
case 1: { // 计算加法
double ad = defbutton
+ Double.parseDouble(result.text.getText());
result.text.setText(ad + "");
i = 0;
text = "";
break;
}
case 2: { // 计算减法
double ad = defbutton
- Double.parseDouble(result.text.getText());
result.text.setText(String.valueOf(ad));
i = 0;
text = "";
break;
}
case 3: { // 计算乘法
double ad = defbutton
* Double.parseDouble(result.text.getText());
result.text.setText(ad + "");
i = 0;
text = "";
break;
}
case 4: { // 计算除法
double ad = defbutton
/ Double.parseDouble(result.text.getText());
result.text.setText(ad + "");
i = 0;
text = "";
break;
}
}
System.out.println(com);
}
System.out.println(result.text.getText());
}
@SuppressWarnings("deprecation")
public static void main(String[] args) {
Jisuanqi loveyou = new Jisuanqi();
loveyou.show();
}
}
// 计算器数字按钮定义面板
class Number_Key extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
JButton zero = new JButton("0"); // 数字键0
JButton one = new JButton("1"); // 数字键1
JButton two = new JButton("2"); // 数字键2
JButton three = new JButton("3"); // 数字键3
JButton four = new JButton("4"); // 数字键4
JButton five = new JButton("5"); // 数字键5
JButton six = new JButton("6"); // 数字键6
JButton seven = new JButton("7"); // 数字键7
JButton eight = new JButton("8"); // 数字键8
JButton nine = new JButton("9"); // 数字键9
JButton plus = new JButton("+");
JButton sub = new JButton("-");
JButton mul = new JButton("*");
JButton div = new JButton("/");
JButton equal = new JButton("=");
JButton ce = new JButton("清零"); // 置零键
JButton point = new JButton(".");
Number_Key() {
setLayout(new GridLayout(4, 4, 1, 1)); // 定义布局管理器为网格布局
setBackground(Color.blue); // 设置背景颜色
// 添加按钮
add(one);
add(two);
add(three);
add(four);
add(five);
add(six);
add(seven);
add(eight);
add(nine);
add(zero);
add(plus);
add(sub);
add(mul);
add(div);
add(point);
add(equal);
add(ce);
}
}
// 计算器显示结果的窗体
class Result extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
// text先是输入和结果
JTextField text = new JTextField("0");
@SuppressWarnings("deprecation")
Result() { // 讲输入的数字或得到的结果在text的右边显示
text.setHorizontalAlignment(SwingConstants.RIGHT);
text.enable(false); // 文本框不能编辑
setLayout(new BorderLayout()); // 设定布局管理器边框布局
add(text, BorderLayout.CENTER); // text放置在窗体的中间
}
}
直接复制 保存成Jisuanqi .java可以直接运行了
怎么用JAVA编程编写一个计算器?
打开IED:打开自己java编程的软件,采用的是eclipse软件。
建立java工程。
编写类。
编写类的详细步骤
1.类的基本结构:
由于这里用到了界面,所以要进行窗口界面的编程,按钮事件的处理,和计算处理界面;
package MyCaculator;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyCaculator extends JFrame {
private int add=1,sub=2,mul=3,div=4;
private int op=0;
boolean ifOp;
private String output="0";
private Button[] jba=new Button[]{new Button("7"),new Button("8"),
new Button("9"),new Button("+"),
new Button("4"),new Button("5"),new Button("6"),new Button("-"),
new Button("1"),new Button("2"),new Button("3"),new Button("*"),
new Button("0"),new Button("."),new Button("="),new Button("/")};
private JPanel jpt=new JPanel();
private JPanel jpb=new JPanel();
private JTextField jtf=new JTextField("");
private MyCaculator(){
}
private void operate(String x){
}
public String add(String x){
return output;
}
public String subtract(String x){
return output;
}
public String multiply(String x){
return output;
}
public String divide(String x){
return output;
}
public String Equals(String x){
return output;
}
public void opClean(){
}
class setOperate_Act implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class setLabel_Act implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
public static void main(String[] args) {
}
}
2.建立构造方法:
所谓构造方法就是,对自己的主类进行初始化,代码如下:
private MyCaculator(){
jpt.setLayout(new BorderLayout());
jpt.add(jtf);
this.add(jpt,BorderLayout.NORTH);
jpb.setLayout(new GridLayout(4,4));
for(int i=0;ijba.length;i++){
jpb.add(jba[i]);
if(i==3||i==7||i==11||i==15||i==14)
jba[i].addActionListener(new setOperate_Act());
else
jba[i].addActionListener(new setLabel_Act());
}
this.add(jpb,BorderLayout.CENTER);
this.setSize(250, 200);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
3.建立数据计算方法
这里的数据计算方法有6个,一个是主方法其他几个是加减乘除的处理方法,代码如下:
private void operate(String x){
double x1=Double.valueOf(x);
double y=Double.valueOf(output);
switch(op){
case 0:output=x;break;
case 1:output=String.valueOf(y+x1);break;
case 2:output =String.valueOf(y-x1);break;
case 3:output =String.valueOf(y*x1);break;
case 4:
if(x1!=0) output=String.valueOf(y/x1);
else output="不能为0";
break;
}
}
public String add(String x){
operate(x);
op=add;
return output;
}
public String subtract(String x){
operate(x);
op=sub;
return output;
}
public String multiply(String x){
operate(x);
op=mul;
return output;
}
public String divide(String x){
operate(x);
op=div;
return output;
}
public String Equals(String x){
operate(x);
op=0;
return output;
}
public void opClean(){
op=0;
output ="0";
}
4.事件处理方法
这里的时间处理方法,没有建立一个整体的方法,二是在为了便于处理的方法,将按钮事件分成两个部分,并采用两个子类来实现,这两个类时内部类要写在主类内部的,代码如下:
class setOperate_Act implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jba[3]){
jtf.setText(add(jtf.getText()));
ifOp=true;
}
else if(e.getSource()==jba[7]){
jtf.setText(subtract(jtf.getText()));
ifOp=true;
}
else if(e.getSource()==jba[11]){
jtf.setText(multiply(jtf.getText()));
ifOp=true;
}
else if(e.getSource()==jba[15]){
jtf.setText(divide(jtf.getText()));
ifOp=true;
}
else if(e.getSource()==jba[14]){
jtf.setText(Equals(jtf.getText()));
ifOp=true;
}
}
}
class setLabel_Act implements ActionListener{
public void actionPerformed(ActionEvent e) {
Button tempb=(Button)e.getSource();
if(ifOp){
jtf.setText(tempb.getLabel());
ifOp=false;
}else {
jtf.setText(jtf.getText()+tempb.getLabel());
}
}
}
5.建立main方法:
要想实现我们的代码,我们需在main方法中,实例化我们的对象。
public static void main(String[] args) {
new MyCaculator();
}