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

如题所述

第1个回答  2007-11-05
简单写的 可以有不对的地方

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<ch.length;i++)
if(!Character.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<ch.length;i++)
if(!Character.isDigit(ch[i]))
return;
super.insertString(offs,new String(ch),a);
}

}本回答被提问者采纳
相似回答