求只需要加减乘除功能的简单的java计算器小程序!

一个简单的用JAVA编写的计算器小程序只需要有加减乘除功能就可以了!急用请各位帮帮忙撒!先谢了!... 一个简单的用JAVA编写的计算器小程序 只需要有加减乘除功能就可以了!
急用 请各位帮帮忙撒! 先谢了!
展开
 我来答
xiaojvzi520
推荐于2017-10-06 · TA获得超过911个赞
知道小有建树答主
回答量:765
采纳率:0%
帮助的人:262万
展开全部
package jisuanqi;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
import javax.swing.*;

/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Jisuan extends JFrame {
JPanel contentPane;
JButton btnmultiply = new JButton();
JButton btn3 = new JButton();
JButton btndivide = new JButton();
JButton btn2 = new JButton();
JButton btn5 = new JButton();
JButton btn0 = new JButton();
JButton btnclear = new JButton();
JButton btnequal = new JButton();
JButton btn1 = new JButton();
JButton btn6 = new JButton();
JButton bntplus = new JButton();
JButton btnminus = new JButton();
JButton btn4 = new JButton();
JButton btn9 = new JButton();
JButton btn8 = new JButton();
JButton btn7 = new JButton();
JPanel jPanel1 = new JPanel();
JTextField txt = new JTextField();
boolean flag=false;
String operand1;
String operand2;
double result;
String action;

public Jisuan() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}

/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(300, 400));
setTitle("Frame Title");
btnmultiply.setBounds(new Rectangle(227, 322, 42, 35));
btnmultiply.setText("+");
btnmultiply.addActionListener(new Jisuan_jButton1_actionAdapter(this));
contentPane.setBorder(BorderFactory.createEtchedBorder());
contentPane.setToolTipText("");
btn3.setBounds(new Rectangle(160, 263, 42, 35));
btn3.setText("3");
btn3.addActionListener(new Jisuan_btn3_actionAdapter(this));
btndivide.setBounds(new Rectangle(227, 263, 42, 35));
btndivide.setText("-");
btndivide.addActionListener(new Jisuan_jButton3_actionAdapter(this));
btn2.setBounds(new Rectangle(93, 263, 42, 35));
btn2.setText("2");
btn2.addActionListener(new Jisuan_btn2_actionAdapter(this));
btn5.setBounds(new Rectangle(93, 201, 42, 35));
btn5.setText("5");
btn5.addActionListener(new Jisuan_btn5_actionAdapter(this));
btn0.setBounds(new Rectangle(25, 322, 42, 35));
btn0.setText("0");
btn0.addActionListener(new Jisuan_btn0_actionAdapter(this));
btnclear.setBounds(new Rectangle(93, 322, 42, 35));
btnclear.setText("c");
btnclear.addActionListener(new Jisuan_jButton7_actionAdapter(this));
btnequal.setBounds(new Rectangle(160, 322, 42, 35));
btnequal.setText("=");
btnequal.addActionListener(new Jisuan_btnequal_actionAdapter(this));
btn1.setBounds(new Rectangle(25, 263, 42, 35));
btn1.setText("1");
btn1.addActionListener(new Jisuan_btn1_actionAdapter(this));
btn6.setBounds(new Rectangle(160, 201, 42, 35));
btn6.setText("6");
btn6.addActionListener(new Jisuan_btn6_actionAdapter(this));
bntplus.setBounds(new Rectangle(227, 201, 42, 35));
bntplus.setText("*");
bntplus.addActionListener(new Jisuan_btnminus_actionAdapter(this));
btnminus.setBounds(new Rectangle(227, 137, 42, 35));
btnminus.setText("/");
btnminus.addActionListener(new Jisuan_bntplus_actionAdapter(this));
btn4.setBounds(new Rectangle(25, 201, 42, 35));
btn4.setText("4");
btn4.addActionListener(new Jisuan_btn4_actionAdapter(this));
btn9.setBounds(new Rectangle(160, 137, 42, 35));
btn9.setText("9");
btn9.addActionListener(new Jisuan_btn9_actionAdapter(this));
btn8.setBounds(new Rectangle(93, 137, 42, 35));
btn8.setText("8");
btn8.addActionListener(new Jisuan_btn8_actionAdapter(this));
btn7.setBounds(new Rectangle(25, 137, 42, 35));
btn7.setText("7");
btn7.addActionListener(new Jisuan_btn7_actionAdapter(this));
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setBounds(new Rectangle(13, 128, 270, 244));
txt.setHorizontalAlignment(SwingConstants.RIGHT);
txt.setBounds(new Rectangle(13, 61, 270, 46));
contentPane.add(btndivide);
contentPane.add(btn3);
contentPane.add(btn2);
contentPane.add(btnclear);
contentPane.add(btnmultiply, null);
contentPane.add(btnequal);
contentPane.add(btn1);
contentPane.add(btn0);
contentPane.add(btn5);
contentPane.add(btn6);
contentPane.add(bntplus);
contentPane.add(btn4);
contentPane.add(btn7);
contentPane.add(btn8);
contentPane.add(btn9);
contentPane.add(btnminus);
contentPane.add(jPanel1);
contentPane.add(txt);
}

public void jButton1_actionPerformed(ActionEvent e) {
action="plus";
operand1=txt.getText();
txt.setText("");
flag=true;
}

public void jButton3_actionPerformed(ActionEvent e) {
action="minus";
operand1=txt.getText();
txt.setText("");
flag=true;
}

public void btn1_actionPerformed(ActionEvent e) {
if(flag){
txt.setText(btn1.getActionCommand());
flag=false;
}
else{
txt.setText(txt.getText()+btn1.getActionCommand());
}
}
public void btn6_actionPerformed(ActionEvent e) {
if(flag){
txt.setText(btn6.getActionCommand());
flag=false;
}
else{
txt.setText(txt.getText()+btn6.getActionCommand());
}
}
public void btn2_actionPerformed(ActionEvent e) {
if(flag){
txt.setText(btn2.getActionCommand());
flag=false;
}
else{
txt.setText(txt.getText()+btn2.getActionCommand());
}
}
public void btn3_actionPerformed(ActionEvent e) {
if(flag){
txt.setText(btn3.getActionCommand());
flag=false;
}
else{
txt.setText(txt.getText()+btn3.getActionCommand());
}
}
public void btn4_actionPerformed(ActionEvent e) {
if(flag){
txt.setText(btn4.getActionCommand());
flag=false;
}
else{
txt.setText(txt.getText()+btn4.getActionCommand());
}
}
public void btn5_actionPerformed(ActionEvent e) {
if(flag){
txt.setText(btn5.getActionCommand());
flag=false;
}
else{
txt.setText(txt.getText()+btn5.getActionCommand());
}
}
public void btn7_actionPerformed(ActionEvent e) {
if(flag){
txt.setText(btn7.getActionCommand());
flag=false;
}
else{
txt.setText(txt.getText()+btn7.getActionCommand());
}
}
public void btn8_actionPerformed(ActionEvent e) {
if(flag){
txt.setText(btn8.getActionCommand());
flag=false;
}
else{
txt.setText(txt.getText()+btn8.getActionCommand());
}
}
public void btn9_actionPerformed(ActionEvent e) {
if(flag){
txt.setText(btn9.getActionCommand());
flag=false;
}
else{
txt.setText(txt.getText()+btn9.getActionCommand());
}
}
public void btn0_actionPerformed(ActionEvent e) {
if(flag){
txt.setText(btn0.getActionCommand());
flag=false;
}
else{
txt.setText(txt.getText()+btn0.getActionCommand());
}
}

public void jButton7_actionPerformed(ActionEvent e) {
txt.setText("");
}

public void bntplus_actionPerformed(ActionEvent e) {
/*
operand1=Integer.parseInt(txt.getText());
action="plus";
flag=true;
*/
action="divide";
operand1=txt.getText();
txt.setText("");
flag=true;
}

public void btnminus_actionPerformed(ActionEvent e) {
action="multiply";
operand1=txt.getText();
txt.setText("");
flag=true;
}

public void btnequal_actionPerformed(ActionEvent e) {
double digit1;
double digit2;
operand2=txt.getText();
if(flag==false){
if(action.equals("divide")){
digit1=Integer.parseInt(operand1);
digit2=Integer.parseInt(operand2);
result=digit1 / digit2;
txt.setText(new Double(result).toString());
flag=true;
}else if(action.equals("plus")){
digit1=Integer.parseInt(operand1);
digit2=Integer.parseInt(operand2);
result=digit1 + digit2;
txt.setText(""+(int)result);
flag=true;
}else if(action.equals("multiply")){
digit1=Integer.parseInt(operand1);
digit2=Integer.parseInt(operand2);
result=digit1 * digit2;
txt.setText(""+(int)result);
flag=true;
}else if(action.equals("minus")){
digit1=Integer.parseInt(operand1);
digit2=Integer.parseInt(operand2);
result=digit1-digit2;
txt.setText(""+(int)result);
flag=true;
}
}
}

}

class Jisuan_btnminus_actionAdapter implements ActionListener {
private Jisuan adaptee;
Jisuan_btnminus_actionAdapter(Jisuan adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btnminus_actionPerformed(e);
}
}

class Jisuan_bntplus_actionAdapter implements ActionListener {
private Jisuan adaptee;
Jisuan_bntplus_actionAdapter(Jisuan adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.bntplus_actionPerformed(e);
}
}

class Jisuan_jButton7_actionAdapter implements ActionListener {
private Jisuan adaptee;
Jisuan_jButton7_actionAdapter(Jisuan adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton7_actionPerformed(e);
}
}

class Jisuan_btn0_actionAdapter implements ActionListener {
private Jisuan adaptee;
Jisuan_btn0_actionAdapter(Jisuan adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btn0_actionPerformed(e);
}
}

class Jisuan_btn9_actionAdapter implements ActionListener {
private Jisuan adaptee;
Jisuan_btn9_actionAdapter(Jisuan adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btn9_actionPerformed(e);
}
}

class Jisuan_btn8_actionAdapter implements ActionListener {
private Jisuan adaptee;
Jisuan_btn8_actionAdapter(Jisuan adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btn8_actionPerformed(e);
}
}

class Jisuan_btn7_actionAdapter implements ActionListener {
private Jisuan adaptee;
Jisuan_btn7_actionAdapter(Jisuan adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btn7_actionPerformed(e);
}
}

class Jisuan_btn6_actionAdapter implements ActionListener {
private Jisuan adaptee;
Jisuan_btn6_actionAdapter(Jisuan adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btn6_actionPerformed(e);
}
}

class Jisuan_btn5_actionAdapter implements ActionListener {
private Jisuan adaptee;
Jisuan_btn5_actionAdapter(Jisuan adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btn5_actionPerformed(e);
}
}

class Jisuan_btn4_actionAdapter implements ActionListener {
private Jisuan adaptee;
Jisuan_btn4_actionAdapter(Jisuan adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btn4_actionPerformed(e);
}
}

class Jisuan_btn3_actionAdapter implements ActionListener {
private Jisuan adaptee;
Jisuan_btn3_actionAdapter(Jisuan adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btn3_actionPerformed(e);
}
}

class Jisuan_btn2_actionAdapter implements ActionListener {
private Jisuan adaptee;
Jisuan_btn2_actionAdapter(Jisuan adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btn2_actionPerformed(e);
}
}

class Jisuan_btn1_actionAdapter implements ActionListener {
private Jisuan adaptee;
Jisuan_btn1_actionAdapter(Jisuan adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btn1_actionPerformed(e);
}
}

class Jisuan_jButton3_actionAdapter implements ActionListener {
private Jisuan adaptee;
Jisuan_jButton3_actionAdapter(Jisuan adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton3_actionPerformed(e);
}
}

class Jisuan_jButton1_actionAdapter implements ActionListener {
private Jisuan adaptee;
Jisuan_jButton1_actionAdapter(Jisuan adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}

class Jisuan_btnequal_actionAdapter implements ActionListener {
private Jisuan adaptee;
Jisuan_btnequal_actionAdapter(Jisuan adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btnequal_actionPerformed(e);
}
}
这个是实现的方法,按钮的名字不一定一样,最主要是这个要一样 action="minus";

main方法 用JB他自己就生成啦
wildhorse2005
2007-12-17
知道答主
回答量:28
采纳率:0%
帮助的人:17.1万
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator
{
public static void main(String[] args)
{
CalculatorFrame frame = new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

class CalculatorFrame extends JFrame
{
public CalculatorFrame()
{
setTitle("Calculator");
CalculatorPanel panel = new CalculatorPanel();
add(panel);
pack();
}
}

class CalculatorPanel extends JPanel
{
public CalculatorPanel()
{
setLayout(new BorderLayout());

result = 0;
lastCommand = "=";
start = true;

display = new JButton("0");
display.setEnabled(false);
add(display, BorderLayout.NORTH);

ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();

panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));

addButton("7", insert);
addButton("8", insert);
addButton("9", insert);
addButton("/", command);

addButton("4", insert);
addButton("5", insert);
addButton("6", insert);
addButton("*", command);

addButton("1", insert);
addButton("2", insert);
addButton("3", insert);
addButton("-", command);

addButton("0", insert);
addButton(".", insert);
addButton("=", command);
addButton("+", command);

add(panel, BorderLayout.CENTER);
}

private void addButton(String label, ActionListener listener)
{
JButton button = new JButton(label);
button.addActionListener(listener);
panel.add(button);
}

private class InsertAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String input = event.getActionCommand();
if (start)
{
display.setText("");
start = false;
}
display.setText(display.getText() + input);
}
}

private class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();

if (start)
{
if (command.equals("-"))
{
display.setText(command);
start = false;
}
else
lastCommand = command;
}
else
{
calculate(Double.parseDouble(display.getText()));
lastCommand = command;
start = true;
}
}
}

public void calculate(double x)
{
if (lastCommand.equals("+")) result += x;
else if (lastCommand.equals("-")) result -= x;
else if (lastCommand.equals("*")) result *= x;
else if (lastCommand.equals("/")) result /= x;
else if (lastCommand.equals("=")) result = x;
display.setText("" + result);
}

private JButton display;
private JPanel panel;
private double result;
private String lastCommand;
private boolean start;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式