做一个计算器,有界面要求的!

xcjrong 2010-09-25 11:32:04
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Calculator extends JFrame {

private Container container;
private GridBagLayout layout;
private GridBagConstraints constraints;
private JTextField displayField;//计算结果显示区
private String lastCommand;//保存+,-,*,/,=命令
private double result;//保存计算结果
private boolean start;//判断是否为数字的开始

public Calculator() {

super("计算器");
container=getContentPane();
layout=new GridBagLayout();
container.setLayout(layout);
constraints=new GridBagConstraints();
start=true;
result=0;
lastCommand = "=";
displayField=new JTextField(20);
displayField.setText("0.0");
//Containe.setColor(Color.red);
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("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("C",4,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("S",4,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("%",4,4,1,1,command);
addButton("0",0,5,1,1,insert);
addButton(".",1,5,1,1,command);
addButton("←",2,5,1,1,insert);
addButton("+",3,5,1,1,command);
addButton("=",4,5,1,1,command);
setSize(280,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;
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) {
Calculator calculator=new Calculator();
calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}


请用这段代码修改做成,是做两个计算器,一个计算器的界面图片是http://hi.csdn.net/space-4006252-do-album-picid-627546-goto-up.html。还有一个计算器界面是http://hi.csdn.net/space-4006252-do-album-picid-642488-goto-up.html
修改的部分请标注说明和注释,这样我才知道哪些代码是加上和被修改的,这段代码有些Button还没写好的请留意一下。我是刚学的所以拜托各位大大了!
...全文
168 点赞 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
xcjrong 2010-09-25
额,我是刚学的所以是有很多问题啦,非常抱歉!麻烦各位大大啦,呵呵!
回复
ptcoding 2010-09-25
- -!飘过。。。。感觉是在招免费劳动力~
回复
相关推荐
发帖
J2ME
创建于2007-09-28

1.3w+

社区成员

Java J2ME
申请成为版主
帖子事件
创建了帖子
2010-09-25 11:32
社区公告
暂无公告