java 计算器api_用JAVA编写一个简单的计算器~要使用接口的~急啊~

这篇博客介绍了如何使用Java编写一个简单的计算器,包括加、减、乘、除以及清空功能。博主提供了两个版本的实现,一个是基础版,另一个增加了数字按键,使得用户可以直接输入数字进行计算。代码中实现了事件监听,当用户点击按钮时,根据选择的运算符进行相应的计算操作,并防止除数为零的情况发生。
摘要由CSDN通过智能技术生成

展开全部

简单写的 可以有不对的地方

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.text.*;

public class calDemo extends JFrame implements ActionListener{

Container c;

JPanel p1,p2;

JButton button1,button2,button3,button4,button;

myDocument my1=new myDocument();

myDocument my2=new myDocument();

JTextField t1=new JTextField(my1,"",10);

JTextField t2=new JTextField(my2,"",10);

JTextField t3=new JTextField("",10);

public calDemo() {

super("win");

button1=new JButton("+");

button2=new JButton("-");

button3=new JButton("*");

button4=new JButton("/");

button=new JButton("清空");

button1.addActionListener(this);

button2.addActionListener(this);

button3.addActionListener(this);

button4.addActionListener(this);

button.addActionListener(this);

p1=new JPanel();

p1.add(t1);

p1.add(t2);

p1.add(t3);

p2=new JPanel();

p2.add(button1);

p2.add(button2);

p2.add(button3);

p2.add(button4);

p2.add(button);

c=getContentPane();

c.add(p2,BorderLayout.CENTER);

c.add(p1,BorderLayout.NORTH);

setDefaultCloseOperation(EXIT_ON_CLOSE);

this.pack();

setVisible(true);

}

public void actionPerformed(ActionEvent e) {

if(e.getSource()==button1){

String str1=t1.getText();

int num1=Integer.parseInt(str1);

String str2=t2.getText();

int num2=Integer.parseInt(str2);

t3.setText(""+(num1+num2));

}

if(e.getSource()==button2){

String str1=t1.getText();

int num1=Integer.parseInt(str1);

String str2=t2.getText();

int num2=Integer.parseInt(str2);

t3.setText(""+(num1-num2));

}

if(e.getSource()==button3){

String str1=t1.getText();

int num1=Integer.parseInt(str1);

String str2=t2.getText();

int num2=Integer.parseInt(str2);

t3.setText(""+(num1*num2));

}

if(e.getSource()==button4){

String str1=t1.getText();

int num1=Integer.parseInt(str1);

String str2=t2.getText();

int num2=Integer.parseInt(str2);

if(num2==0){

JOptionPane.showMessageDialog(this,"除数不能为零");

}

else

t3.setText(""+(1.0*num1/num2));

}

if(e.getSource()==button){

t1.setText("");t2.setText("");t3.setText("");

}

}

public static void main(String[] args) {

new calDemo();

}

}

class myDocument extends PlainDocument

{

public void insertString(int offs,String str,AttributeSet a) throws

BadLocationException

{

my1(offs, str, a);

}

private void my1(int offs, String str, AttributeSet a) throws

BadLocationException {

if(str==null)

return;

char[]ch=str.toCharArray();

for(int i=0;i

if(!e69da5e6ba9062616964757a686964616f31333234326331Character.isDigit(ch[i]))

return;

super.insertString(offs,new String(ch),a);

}

}

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.text.*;

public class calDemo2 extends JFrame implements ActionListener{

Container c;

JPanel p1,p2;

JButton button1,button2,button3,button4,button,button5;

JButton button01,button02,button03,button04,button05,button06,button07,button08,button09,button00;

myDocument2 my=new myDocument2();

JTextField t1=new JTextField(my,"",10);

int num1,num2,flag=0;

public calDemo2() {

super("win");

button1=new JButton("+");

button2=new JButton("-");

button3=new JButton("*");

button4=new JButton("/");

button5=new JButton("=");

button=new JButton("清空");

button1.addActionListener(this);

button2.addActionListener(this);

button3.addActionListener(this);

button4.addActionListener(this);

button5.addActionListener(this);

button.addActionListener(this);

button01=new JButton("1");

button02=new JButton("2");

button03=new JButton("3");

button04=new JButton("4");

button05=new JButton("5");

button06=new JButton("6");

button07=new JButton("7");

button08=new JButton("8");

button09=new JButton("9");

button00=new JButton("0");

button01.addActionListener(this);

button02.addActionListener(this);

button03.addActionListener(this);

button04.addActionListener(this);

button05.addActionListener(this);

button06.addActionListener(this);

button07.addActionListener(this);

button08.addActionListener(this);

button09.addActionListener(this);

button00.addActionListener(this);

p1=new JPanel();

p1.add(t1);

p2=new JPanel();

p2.add(button1);

p2.add(button2);

p2.add(button3);

p2.add(button4);

p2.add(button5);

p2.add(button);

p2.add(button01);

p2.add(button02);

p2.add(button03);

p2.add(button04);

p2.add(button05);

p2.add(button06);

p2.add(button07);

p2.add(button08);

p2.add(button09);

p2.add(button00);

c=getContentPane();

c.add(p2,BorderLayout.CENTER);

c.add(p1,BorderLayout.NORTH);

setDefaultCloseOperation(EXIT_ON_CLOSE);

this.pack();

setVisible(true);

}

public void actionPerformed(ActionEvent e) {

if(e.getSource()==button01){

String str=t1.getText();

t1.setText(str+"1");

}

if(e.getSource()==button02){

String str=t1.getText();

t1.setText(str+"2");

}

if(e.getSource()==button03){

String str=t1.getText();

t1.setText(str+"3");

}

if(e.getSource()==button04){

String str=t1.getText();

t1.setText(str+"4");

}

if(e.getSource()==button05){

String str=t1.getText();

t1.setText(str+"5");

}

if(e.getSource()==button06){

String str=t1.getText();

t1.setText(str+"6");

}

if(e.getSource()==button07){

String str=t1.getText();

t1.setText(str+"7");

}

if(e.getSource()==button08){

String str=t1.getText();

t1.setText(str+"8");

}

if(e.getSource()==button09){

String str=t1.getText();

t1.setText(str+"9");

}

if(e.getSource()==button00){

String str=t1.getText();

t1.setText(str+"0");

}

if(e.getSource()==button1){

String str1=t1.getText();

num1=Integer.parseInt(str1);

t1.setText("");

flag=1;

}

if(e.getSource()==button2){

String str1=t1.getText();

num1=Integer.parseInt(str1);

t1.setText("");

flag=2;

}

if(e.getSource()==button3){

String str1=t1.getText();

num1=Integer.parseInt(str1);

t1.setText("");

flag=3;

}

if(e.getSource()==button4){

String str1=t1.getText();

num1=Integer.parseInt(str1);

t1.setText("");

flag=4;

}

if(e.getSource()==button5){

String str2=t1.getText();

num2=Integer.parseInt(str2);

if(flag==1){

t1.setText(""+(num1+num2));

}

else if(flag==2){

t1.setText(""+(num1-num2));

}

else if(flag==3){

t1.setText(""+(num1*num2));

}

else if(flag==4){

if(num2==0){

JOptionPane.showMessageDialog(this, "除数不能为零");

}

else

t1.setText(""+(num1/num2));

}

else{

JOptionPane.showMessageDialog(this, "请选择运算符号");

}

}

if(e.getSource()==button){

num1=0;num2=0;flag=0;

t1.setText("");

}

}

public static void main(String[] args) {

new calDemo2();

}

}

class myDocument2 extends PlainDocument

{

public void insertString(int offs,String str,AttributeSet a) throws

BadLocationException

{

my1(offs, str, a);

}

private void my1(int offs, String str, AttributeSet a) throws

BadLocationException {

if(str==null)

return;

char[]ch=str.toCharArray();

for(int i=0;i

if(!Character.isDigit(ch[i]))

return;

super.insertString(offs,new String(ch),a);

}

}

本回答被提问者采纳

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值