请各位高手帮忙用Java编一个计算器的程序,给点提示也行
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.lang.String;
public class CalcDemo extends JApplet implements ActionListener{
JButton btn0=new JButton("0");
JButton btn1=new JButton("1");
JButton btn2=new JButton("2");
JButton btn3=new JButton("3");
JButton btn4=new JButton("4");
JButton btn5=new JButton("5");
JButton btn6=new JButton("6");
JButton btn7=new JButton("7");
JButton btn8=new JButton("8");
JButton btn9=new JButton("9");
JButton btnPoint=new JButton(".");
JButton btnAdd=new JButton("+");
JButton btnSubtract=new JButton("-");
JButton btnMultiply=new JButton("*");
JButton btnDivide=new JButton("/");
JButton btnEqual=new JButton("=");
JButton btnClear=new JButton("清除");
JTextField textField=new JTextField(" ");
String type;//用于标示点了那种计算方法
double a=0,b=0;//定义两个变量,用于获取文本框内容
public void init(){
Container pane=getContentPane();
pane.setLayout(new GridLayout(5,2,10,10));
// pane.setBackground(getColor(blue));
pane.add(textField);//将各个组件放入容器pane
pane.add(btnClear);
pane.add(btnEqual);
pane.add(btnAdd);
pane.add(btn7);
pane.add(btn8);
pane.add(btn9);
pane.add(btnSubtract);
pane.add(btn4);
pane.add(btn5);
pane.add(btn6);
pane.add(btnMultiply);
pane.add(btn1);
pane.add(btn2);
pane.add(btn3);
pane.add(btnDivide);
pane.add(btn0);
pane.add(btnPoint);
btn0.addActionListener(this);//注册动作
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn7.addActionListener(this);
btn8.addActionListener(this);
btn9.addActionListener(this);
btnPoint.addActionListener(this);
btnAdd.addActionListener(this);
btnSubtract.addActionListener(this);
btnMultiply.addActionListener(this);
btnDivide.addActionListener(this);
btnClear.addActionListener(this);
btnEqual.addActionListener(this);
}
public Insets getInsets(){
return new Insets(20,20,20,20);
}
public void actionPerformed(ActionEvent e){//定义动作行为
if(e.getSource()==btn0){
textField.setText(textField.getText()+"0");
}
else if(e.getSource()==btn1){
textField.setText(textField.getText()+"1");
}
else if(e.getSource()==btn2){
textField.setText(textField.getText()+"2");
}
else if(e.getSource()==btn3){
textField.setText(textField.getText()+"3");
}
else if(e.getSource()==btn4){
textField.setText(textField.getText()+"4");
}
else if(e.getSource()==btn5){
textField.setText(textField.getText()+"5");
}
else if(e.getSource()==btn6){
textField.setText(textField.getText()+"6");
}
else if(e.getSource()==btn7){
textField.setText(textField.getText()+"7");
}
else if(e.getSource()==btn8){
textField.setText(textField.getText()+"8");
}
else if(e.getSource()==btn9){
textField.setText(textField.getText()+"9");
}
else if(e.getSource()==btnPoint){
textField.setText(textField.getText()+".");
}
else if(e.getSource()==btnAdd){
type="add";
a=Double.parseDouble(textField.getText());
textField.setText("");
}
else if(e.getSource()==btnSubtract){
type="subtract";
a=Double.parseDouble(textField.getText());
textField.setText("");
}
else if(e.getSource()==btnMultiply){
type="multiply";
a=Double.parseDouble(textField.getText());
textField.setText("");
}
else if(e.getSource()==btnDivide){
type="divide";
a=Double.parseDouble(textField.getText());
textField.setText("");
}
else if(e.getSource()==btnEqual){
if(type=="add")
{
b=Double.parseDouble(textField.getText());
textField.setText(Double.toString(a+b));
}
if(type=="subtract")
{
b=Double.parseDouble(textField.getText());
textField.setText(Double.toString(a-b));
}
if(type=="multiply")
{
b=Double.parseDouble(textField.getText());
textField.setText(Double.toString(a*b));
}
if(type=="divide")
{
b=Double.parseDouble(textField.getText());
textField.setText(Double.toString(a/b));
}
}
else if(e.getSource()==btnClear){
textField.setText("");
}
}
}
不过要再写一个html文件
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator1 extends JFrame {
private Container container;
private GridBagLayout layout;
private GridBagConstraints constraints;
private JTextField displayField;//计算结果显示区
private String lastCommand;//保存+,-,*,/,=命令
private double result;//保存计算结果
private boolean start;//判断是否为数字的开始
public Calculator1() {
super("Calculator");
container=getContentPane();
layout=new GridBagLayout();
container.setLayout(layout);
constraints=new GridBagConstraints();
start=true;
result=0;
lastCommand = "=";
displayField=new JTextField(20);
displayField.setHorizontalAlignment(JTextField.RIGHT);
constraints.gridx=0;
constraints.gridy=0;
constraints.gridwidth=4;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.BOTH;
constraints.weightx=100;
constraints.weighty=100;
layout.setConstraints(displayField,constraints);
container.add(displayField);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
addButton("Backspace",0,1,2,1,insert);
addButton("CE",2,1,1,1,insert);
addButton("C",3,1,1,1,insert);
addButton("7",0,2,1,1,insert);
addButton("8",1,2,1,1,insert);
addButton("9",2,2,1,1,insert);
addButton("/",3,2,1,1,command);
addButton("4",0,3,1,1,insert);
addButton("5",1,3,1,1,insert);
addButton("6",2,3,1,1,insert);
addButton("*",3,3,1,1,command);
addButton("1",0,4,1,1,insert);
addButton("2",1,4,1,1,insert);
addButton("3",2,4,1,1,insert);
addButton("-",3,4,1,1,command);
addButton("0",0,5,1,1,insert);
addButton("+/-",1,5,1,1,insert);//只显示"-"号,"+"没有实用价值
addButton(".",2,5,1,1,insert);
addButton("+",3,5,1,1,command);
addButton("=",0,6,4,1,command);
setSize(300,300);
setVisible(true);
}
private void addButton(String label,int row,int column,int with,int height,ActionListener listener) {
JButton button=new JButton(label);
constraints.gridx=row;
constraints.gridy=column;
constraints.gridwidth=with;
constraints.gridheight=height;
constraints.fill=GridBagConstraints.BOTH;
button.addActionListener(listener);
layout.setConstraints(button,constraints);
container.add(button);
}
private class InsertAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
String input=event.getActionCommand();
if (start)
{
displayField.setText("");
start=false;
if(input.equals("+/-"))
displayField.setText(displayField.getText()+"-");
}
if(!input.equals("+/-")) {
if(input.equals("Backspace")) {
String str=displayField.getText();
if(str.length()>0)
displayField.setText(str.substring(0,str.length()-1));
}
else if(input.equals("CE")||input.equals("C")) {
displayField.setText("0");
start=true;
}
else
displayField.setText(displayField.getText()+input);
}
}
}
private class CommandAction implements ActionListener {
public void actionPerformed(ActionEvent evt) {
String command=evt.getActionCommand();
if(start) {
lastCommand=command;
}
else {
calculate(Double.parseDouble(displayField.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;
displayField.setText(""+ result);
}
public static void main(String []args) {
Calculator1 calculator=new Calculator1();
calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
[此贴子已经被作者于2006-11-28 9:27:02编辑过]