请用JAVA编写一个计算器,面向对象思想,只实现加,减,乘即可。

 我来答
L6jun123
2012-10-22 · 超过13用户采纳过TA的回答
知道答主
回答量:55
采纳率:50%
帮助的人:20.9万
展开全部
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class CalcFrame extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
JButton bt[];
JTextField tf ;
StringBuffer buf ;
char sign;
float No1 = 0;
float No2 = 0;
public CalcFrame(){
bt = new JButton[16];
tf = new JTextField();
this.setTitle("简单计算器");
for(int i = 0;i<10;i++){
bt[i] = new JButton(""+i);
}
bt[10] = new JButton(" + ");
bt[11] = new JButton(" - ");
bt[12] = new JButton(" * ");
bt[13] = new JButton(" / ");
bt[14] = new JButton(" =");
bt[15] = new JButton(" C ");
AddPanel();
ActionListener();
this.add(tf,BorderLayout.NORTH);
this.setBounds(80, 90, 300, 300);
this.setVisible(true);
}
public void AddPanel(){
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4,4,8,8));
for(int i=0;i<16;i++){
panel.add(bt[i]);
}
this.add(panel,BorderLayout.CENTER);
}
public void ActionListener(){
buf= new StringBuffer();
bt[0].addMouseListener(
new MouseAdapter(){
public void mouseClicked(MouseEvent e){
buf.append(0);
tf.setText(new String(buf));
}
});
bt[1].addMouseListener(
new MouseAdapter(){
public void mouseClicked(MouseEvent e){
buf.append(1);
tf.setText(new String(buf));
}
});
bt[2].addMouseListener(
new MouseAdapter(){
public void mouseClicked(MouseEvent e){
buf.append(2);
tf.setText(new String(buf));
}
});
bt[3].addMouseListener(
new MouseAdapter(){
public void mouseClicked(MouseEvent e){
buf.append(3);
tf.setText(new String(buf));
}
});
bt[4].addMouseListener(
new MouseAdapter(){
public void mouseClicked(MouseEvent e){
buf.append(4);
tf.setText(new String(buf));
}
});
bt[5].addMouseListener(
new MouseAdapter(){
public void mouseClicked(MouseEvent e){
buf.append(5);
tf.setText(new String(buf));
}
});
bt[6].addMouseListener(
new MouseAdapter(){
public void mouseClicked(MouseEvent e){
buf.append(6);
tf.setText(new String(buf));
}
});
bt[7].addMouseListener(
new MouseAdapter(){
public void mouseClicked(MouseEvent e){
buf.append(7);
tf.setText(new String(buf));
}
});
bt[8].addMouseListener(
new MouseAdapter(){
public void mouseClicked(MouseEvent e){
buf.append(8);
tf.setText(new String(buf));
}
});
bt[9].addMouseListener(
new MouseAdapter(){
public void mouseClicked(MouseEvent e){
buf.append(9);
tf.setText(new String(buf));
}
});
bt[10].addMouseListener(
new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if(buf.length()!=0){
No1=Integer.parseInt(new String(buf));
tf.setText("");
sign = '+';
buf = new StringBuffer();
}
}
});
bt[11].addMouseListener(
new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if(buf.length()!=0){
No1=Integer.parseInt(new String(buf));
tf.setText("");
sign = '-';
buf = new StringBuffer();
}
}
});
bt[12].addMouseListener(
new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if(buf.length()!=0){
No1=Integer.parseInt(new String(buf));
tf.setText("");
sign = '*';
buf = new StringBuffer();
}
}
});
bt[13].addMouseListener(
new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if(buf.length()!=0){
No1=Integer.parseInt(new String(buf));
tf.setText("");
sign = '/';
buf = new StringBuffer();
}
}
});
bt[14].addMouseListener(
new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if(buf.length()!=0){
No2 = Integer.parseInt(new String(buf));
buf = new StringBuffer();
float result=0;
switch(sign){
case '+':
result = No1+No2;
tf.setText(No1+"+"+No2+"="+(result)+"");
break;
case '-':
result = No1-No2;
tf.setText(No1+"-"+No2+"="+(result)+"");
break;
case '*':
result = No1*No2;
tf.setText(No1+"*"+No2+"="+(result)+"");
break;
case '/':
result = No1/No2;
tf.setText(No1+"/"+No2+"="+(result)+"");
break;
}
}
}
});
bt[15].addMouseListener(
new MouseAdapter(){
public void mouseClicked(MouseEvent e){
tf.setText("");
buf = new StringBuffer();
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class CalcMain {
public static void main(String[] args) {
new CalcFrame();
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式