2个回答
2014-06-12
展开全部
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener
{
private JTextField text;
StringBuffer sb1 = new StringBuffer(); //参数一
StringBuffer sb2 = new StringBuffer(); //参数二
private double b;
private double d = 0.0f;
private static int j = 0;
private static char f;
private static boolean D = false;
private final String[] KEYS = {"sqrt","+/-","CE","C", "7", "8"简稿改, "9", "/",
"4", "5", "6"敬数,"*", "1", "2", "3","-", "0",".", "=", "+" };
private JButton keys[] = new JButton[KEYS.length];
//构造函数
public Calculator()
{
//设置JFrame
super("Calculator");
this.setBounds(480, 320, 240, 220);
this.setResizable(false);
this.setBackground(Color.lightGray);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new FlowLayout());//内容窗格流布局
//文本行
text = new JTextField("0.", 20);
text.setHorizontalAlignment(JTextField.RIGHT);
this.getContentPane().add(text);
text.addActionListener(this);
//面板
JPanel panel = new JPanel(new GridLayout(6, 4));
this.getContentPane().add(panel);
//定义按钮拦判
for(int i=0;i<KEYS.length;i++)
{
keys[i] = new JButton( KEYS[i]);//定义按钮
panel.add(keys[i]); //添加组件
keys[i].addActionListener(this);//注册事件监听器
}
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
JButton jb = (JButton)e.getSource();
char c = jb.getText().charAt(0);//给第一个数赋值
switch (c)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
SetNum(c);
break;
//运算符号
case '+':
case '-':
case '*':
case '/':
setOperation(c);
break;
//运算结果或归零
case '=':
Answers(f);
break;
case 'C':
restart();
}
}
//输入数据函数
private void SetNum(char y)
{
if (D == false)
{
sb1.append(y);
this.text.setText(sb1.toString());
}
else
{
sb2.append(y);
this.text.setText(sb2.toString());
}
}
//输入运算符
private void setOperation(char a)
{
f = a;
D = true;
this.text.setText("");
}
//输出答案函数
public void Answers(char k)
{
if (sb2.length() == 0)
{
this.text.setText(java.lang.String.valueOf(Double.parseDouble(sb1.toString())));
}
else
{
if (j == 0)
{
d = Double.parseDouble(sb1.toString());
}
b = Double.parseDouble(sb2.toString());
j = 1;
D = false;
sb1.delete(0, sb1.length() - 1);//删除字符串从0~a-1处(这个方法就是这么用地)
sb2 = new StringBuffer("");//重新新建一个对象覆盖
//sb2.delete(0,sb2.length()-1);//删除字符串从0~a-1处(这个方法就是这么用地)????????
switch (k)
{
case '+':
d = d + b;
this.text.setText(java.lang.String.valueOf(d));
break;
case '-':
d = d - b;
this.text.setText(java.lang.String.valueOf(d));
break;
case '*':
d = d * b;
this.text.setText(java.lang.String.valueOf(d));//在文本行输出数字string.valueof(c)
break;
case '/':
if (b == 0)
{
restart();
}
d = d / b;
this.text.setText(java.lang.String.valueOf(d));
break;
}
}
}
//}
//归零函数
private void restart()
{
Calculator.this.dispose();
j = 0;
d = 0;
D = false;
new Calculator();
}
public static void main(String[] args)
{
new Calculator();
}
}
很久以前写的,刚刚找出来你试试吧
import javax.swing.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener
{
private JTextField text;
StringBuffer sb1 = new StringBuffer(); //参数一
StringBuffer sb2 = new StringBuffer(); //参数二
private double b;
private double d = 0.0f;
private static int j = 0;
private static char f;
private static boolean D = false;
private final String[] KEYS = {"sqrt","+/-","CE","C", "7", "8"简稿改, "9", "/",
"4", "5", "6"敬数,"*", "1", "2", "3","-", "0",".", "=", "+" };
private JButton keys[] = new JButton[KEYS.length];
//构造函数
public Calculator()
{
//设置JFrame
super("Calculator");
this.setBounds(480, 320, 240, 220);
this.setResizable(false);
this.setBackground(Color.lightGray);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new FlowLayout());//内容窗格流布局
//文本行
text = new JTextField("0.", 20);
text.setHorizontalAlignment(JTextField.RIGHT);
this.getContentPane().add(text);
text.addActionListener(this);
//面板
JPanel panel = new JPanel(new GridLayout(6, 4));
this.getContentPane().add(panel);
//定义按钮拦判
for(int i=0;i<KEYS.length;i++)
{
keys[i] = new JButton( KEYS[i]);//定义按钮
panel.add(keys[i]); //添加组件
keys[i].addActionListener(this);//注册事件监听器
}
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
JButton jb = (JButton)e.getSource();
char c = jb.getText().charAt(0);//给第一个数赋值
switch (c)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
SetNum(c);
break;
//运算符号
case '+':
case '-':
case '*':
case '/':
setOperation(c);
break;
//运算结果或归零
case '=':
Answers(f);
break;
case 'C':
restart();
}
}
//输入数据函数
private void SetNum(char y)
{
if (D == false)
{
sb1.append(y);
this.text.setText(sb1.toString());
}
else
{
sb2.append(y);
this.text.setText(sb2.toString());
}
}
//输入运算符
private void setOperation(char a)
{
f = a;
D = true;
this.text.setText("");
}
//输出答案函数
public void Answers(char k)
{
if (sb2.length() == 0)
{
this.text.setText(java.lang.String.valueOf(Double.parseDouble(sb1.toString())));
}
else
{
if (j == 0)
{
d = Double.parseDouble(sb1.toString());
}
b = Double.parseDouble(sb2.toString());
j = 1;
D = false;
sb1.delete(0, sb1.length() - 1);//删除字符串从0~a-1处(这个方法就是这么用地)
sb2 = new StringBuffer("");//重新新建一个对象覆盖
//sb2.delete(0,sb2.length()-1);//删除字符串从0~a-1处(这个方法就是这么用地)????????
switch (k)
{
case '+':
d = d + b;
this.text.setText(java.lang.String.valueOf(d));
break;
case '-':
d = d - b;
this.text.setText(java.lang.String.valueOf(d));
break;
case '*':
d = d * b;
this.text.setText(java.lang.String.valueOf(d));//在文本行输出数字string.valueof(c)
break;
case '/':
if (b == 0)
{
restart();
}
d = d / b;
this.text.setText(java.lang.String.valueOf(d));
break;
}
}
}
//}
//归零函数
private void restart()
{
Calculator.this.dispose();
j = 0;
d = 0;
D = false;
new Calculator();
}
public static void main(String[] args)
{
new Calculator();
}
}
很久以前写的,刚刚找出来你试试吧
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询