求一个java编辑的计算器小程序,最好带详细讲解的,瞎转的就算了。感激不尽
展开全部
import java.awt.*;
import java.awt.event.*;
class CalcAppDemo extends Frame{
private TextField t_result;
private Panel p_main;//主面板
private Panel p_num;//数字面板
private Panel p_oper;//操作符面板
private Panel p_show;//显示面板
private Button b_num[];//数字按钮
private Button b_oper[];//操作符按钮
public CalcAppDemo(String title){
setTitle(title);
t_result = new TextField("0.0",21);
p_main = new Panel();
p_num = new Panel();
p_oper = new Panel();
p_show = new Panel();
p_main.setLayout(new BorderLayout());
p_num.setLayout(new GridLayout(4,3,1,1));
p_oper.setLayout(new GridLayout(4,2,1,1));
b_num = new Button[12];
for(int i = 0;i < 9;i++){
b_num[i] = new Button(new Integer(i+1).toString());
}
b_num[9] = new Button("0");
b_num[10] = new Button("cls");
b_num[11] = new Button(".");
for(int i = 0;i < 12;i++){
p_num.add(b_num[i]);
}
b_oper = new Button[8];
b_oper[0] = new Button("+");
b_oper[1] = new Button("-");
b_oper[2] = new Button("*");
b_oper[3] = new Button("/");
b_oper[4] = new Button("pow");
b_oper[5] = new Button("sqrt");
b_oper[6] = new Button("+/-");
b_oper[7] = new Button("=");
for(int i = 0;i < 8;i++){
p_oper.add(b_oper[i]);
}
t_result.setEditable(false);
p_show.add(t_result,BorderLayout.NORTH); //文本框在显示面板的北边
p_main.add(p_show,BorderLayout.NORTH); //显示面板在主面板上的北边
p_main.add(p_num,BorderLayout.WEST);
p_main.add(p_oper,BorderLayout.EAST); //操作符面板在主面板上的东边
this.add(p_main,BorderLayout.CENTER); //主面板在框架(主窗口)的中间
setSize(400,400);
setResizable(false);
pack();
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
ButtonListener bl = new ButtonListener(); //监听器对象
for(int i=0;i<12;i++){
b_num[i].addActionListener(bl); //注册监听器
}
for(int i = 0;i < 8;i++){
b_oper[i].addActionListener(bl);
}
}
class ButtonListener implements ActionListener { //监听器类是内部类
private String lastOp; //存储上一次操作符
private String strVal; //存储数字对应的字符串
private double total; //总数
private double number; //存储新输入的数
private boolean firsttime;//判断是否第一次按下的是操作符按钮
private boolean operatorPressed;//判断是否已经按过操作符按钮
ButtonListener( ){
firsttime = true;
strVal = "";
}
//事件处理器
public void actionPerformed( ActionEvent e ){
String s = ((Button)e.getSource()).getLabel().trim();
if(Character.isDigit(s.charAt(0))){ //判断是操作数还是操作符
handleNumber(s);
}else{
calculate(s); //计算
}
}
//判断是一元操作符还是二元操作符,并根据操作符类型做计算
void calculate( String op ){
operatorPressed = true;
if(firsttime&&!isUnary(op)){
total = getNumberOnDisplay();
firsttime = false;
}
if (isUnary(op)){
handleUnaryOp(op);
}
else if (lastOp != null){
handleBinaryOp(lastOp);
}
if (!isUnary(op)){ //存储上一次按下的操作符
lastOp = op;
}
}
//判断是否是一元操作符
boolean isUnary(String s){
return s.equals( "=" )
|| s.equals( "cls" )
|| s.equals( "sqrt" )
|| s.equals( "+/-" )
|| s.equals( "." );
}
//处理一元操作符
void handleUnaryOp( String op ){
if ( op.equals( "+/-" ) ){
//将显示框中的数字取反
number = negate(getNumberOnDisplay()+"");
t_result.setText("");
t_result.setText( number + "");
return;
}else if (op.equals(".")){
handleDecPoint();
return;
}else if(op.equals("sqrt")){
number = Math.sqrt(getNumberOnDisplay());
t_result.setText("");
t_result.setText(number+"");
return;
}else if(op.equals("=")){
//在按下"="前已经按下一个二元运算符
if(lastOp!=null&&!isUnary(lastOp)){
handleBinaryOp(lastOp);
}
lastOp=null;
firsttime=true;
return;
}else{
clear();
}
}
//处理二元运算符
void handleBinaryOp(String op){
if(op.equals("+")){
total += number;
}else if(op.equals("-")){
total -= number;
}else if(op.equals("*")){
total *= number;
}else if(op.equals("/")){
try{ //异常处理
total /=number;
}catch(ArithmeticException ae){ }
}else if(op.equals("pow"))
total=Math.pow(total,number);
// t_result.setText("");
lastOp = null;
// strVal = "";
number = 0;
t_result.setText(total+"");
}
//该方法用于处理数字按钮
void handleNumber(String s){
if (!operatorPressed){ //连接按下的数字按钮的值
strVal+=s;
}
else{ //当按下操作符按钮时,清除strVal并存储输入的第一个数值
operatorPressed = false;
strVal = s;
}
//将strVal转换为double
number=new Double(strVal).doubleValue();
t_result.setText("");
t_result.setText(strVal);
}
//该方法用于按下"."按钮
void handleDecPoint(){
operatorPressed = false;
//如果该字符串中无".",放置一个"."在字符串末尾
if ( strVal.indexOf( "." ) < 0 ) {
strVal+=".";
}
t_result.setText("");
t_result.setText( strVal );
}
//该方法用于将一个数求反
double negate( String s ){
operatorPressed = false;
//如果是一个整数,去掉小数点后面的0
if ( number == ( int ) number ){
s = s.substring(0, s.indexOf( "." ) );
}
//如果无"-"增加在该数的前面
if ( s.indexOf( "-" ) < 0 ){
strVal = "-"+s;
}
else{//如果有"-"则去掉
strVal = s.substring( 1 );
}
return new Double( strVal ).doubleValue();
}
//将显示框中的值转换为Double
double getNumberOnDisplay() {
return new Double(t_result.getText()).doubleValue();
}
//清除屏幕并设置所有的标识
void clear(){
firsttime = true;
lastOp = null;
strVal = "";
total = 0;
number = 0;
t_result.setText("0");
}
}
public static void main(String args[]){
CalcAppDemo c = new CalcAppDemo("简单的计算器程序");
c.setVisible(true);
}
}
//这是我以前写的,参考一下吧
import java.awt.event.*;
class CalcAppDemo extends Frame{
private TextField t_result;
private Panel p_main;//主面板
private Panel p_num;//数字面板
private Panel p_oper;//操作符面板
private Panel p_show;//显示面板
private Button b_num[];//数字按钮
private Button b_oper[];//操作符按钮
public CalcAppDemo(String title){
setTitle(title);
t_result = new TextField("0.0",21);
p_main = new Panel();
p_num = new Panel();
p_oper = new Panel();
p_show = new Panel();
p_main.setLayout(new BorderLayout());
p_num.setLayout(new GridLayout(4,3,1,1));
p_oper.setLayout(new GridLayout(4,2,1,1));
b_num = new Button[12];
for(int i = 0;i < 9;i++){
b_num[i] = new Button(new Integer(i+1).toString());
}
b_num[9] = new Button("0");
b_num[10] = new Button("cls");
b_num[11] = new Button(".");
for(int i = 0;i < 12;i++){
p_num.add(b_num[i]);
}
b_oper = new Button[8];
b_oper[0] = new Button("+");
b_oper[1] = new Button("-");
b_oper[2] = new Button("*");
b_oper[3] = new Button("/");
b_oper[4] = new Button("pow");
b_oper[5] = new Button("sqrt");
b_oper[6] = new Button("+/-");
b_oper[7] = new Button("=");
for(int i = 0;i < 8;i++){
p_oper.add(b_oper[i]);
}
t_result.setEditable(false);
p_show.add(t_result,BorderLayout.NORTH); //文本框在显示面板的北边
p_main.add(p_show,BorderLayout.NORTH); //显示面板在主面板上的北边
p_main.add(p_num,BorderLayout.WEST);
p_main.add(p_oper,BorderLayout.EAST); //操作符面板在主面板上的东边
this.add(p_main,BorderLayout.CENTER); //主面板在框架(主窗口)的中间
setSize(400,400);
setResizable(false);
pack();
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
ButtonListener bl = new ButtonListener(); //监听器对象
for(int i=0;i<12;i++){
b_num[i].addActionListener(bl); //注册监听器
}
for(int i = 0;i < 8;i++){
b_oper[i].addActionListener(bl);
}
}
class ButtonListener implements ActionListener { //监听器类是内部类
private String lastOp; //存储上一次操作符
private String strVal; //存储数字对应的字符串
private double total; //总数
private double number; //存储新输入的数
private boolean firsttime;//判断是否第一次按下的是操作符按钮
private boolean operatorPressed;//判断是否已经按过操作符按钮
ButtonListener( ){
firsttime = true;
strVal = "";
}
//事件处理器
public void actionPerformed( ActionEvent e ){
String s = ((Button)e.getSource()).getLabel().trim();
if(Character.isDigit(s.charAt(0))){ //判断是操作数还是操作符
handleNumber(s);
}else{
calculate(s); //计算
}
}
//判断是一元操作符还是二元操作符,并根据操作符类型做计算
void calculate( String op ){
operatorPressed = true;
if(firsttime&&!isUnary(op)){
total = getNumberOnDisplay();
firsttime = false;
}
if (isUnary(op)){
handleUnaryOp(op);
}
else if (lastOp != null){
handleBinaryOp(lastOp);
}
if (!isUnary(op)){ //存储上一次按下的操作符
lastOp = op;
}
}
//判断是否是一元操作符
boolean isUnary(String s){
return s.equals( "=" )
|| s.equals( "cls" )
|| s.equals( "sqrt" )
|| s.equals( "+/-" )
|| s.equals( "." );
}
//处理一元操作符
void handleUnaryOp( String op ){
if ( op.equals( "+/-" ) ){
//将显示框中的数字取反
number = negate(getNumberOnDisplay()+"");
t_result.setText("");
t_result.setText( number + "");
return;
}else if (op.equals(".")){
handleDecPoint();
return;
}else if(op.equals("sqrt")){
number = Math.sqrt(getNumberOnDisplay());
t_result.setText("");
t_result.setText(number+"");
return;
}else if(op.equals("=")){
//在按下"="前已经按下一个二元运算符
if(lastOp!=null&&!isUnary(lastOp)){
handleBinaryOp(lastOp);
}
lastOp=null;
firsttime=true;
return;
}else{
clear();
}
}
//处理二元运算符
void handleBinaryOp(String op){
if(op.equals("+")){
total += number;
}else if(op.equals("-")){
total -= number;
}else if(op.equals("*")){
total *= number;
}else if(op.equals("/")){
try{ //异常处理
total /=number;
}catch(ArithmeticException ae){ }
}else if(op.equals("pow"))
total=Math.pow(total,number);
// t_result.setText("");
lastOp = null;
// strVal = "";
number = 0;
t_result.setText(total+"");
}
//该方法用于处理数字按钮
void handleNumber(String s){
if (!operatorPressed){ //连接按下的数字按钮的值
strVal+=s;
}
else{ //当按下操作符按钮时,清除strVal并存储输入的第一个数值
operatorPressed = false;
strVal = s;
}
//将strVal转换为double
number=new Double(strVal).doubleValue();
t_result.setText("");
t_result.setText(strVal);
}
//该方法用于按下"."按钮
void handleDecPoint(){
operatorPressed = false;
//如果该字符串中无".",放置一个"."在字符串末尾
if ( strVal.indexOf( "." ) < 0 ) {
strVal+=".";
}
t_result.setText("");
t_result.setText( strVal );
}
//该方法用于将一个数求反
double negate( String s ){
operatorPressed = false;
//如果是一个整数,去掉小数点后面的0
if ( number == ( int ) number ){
s = s.substring(0, s.indexOf( "." ) );
}
//如果无"-"增加在该数的前面
if ( s.indexOf( "-" ) < 0 ){
strVal = "-"+s;
}
else{//如果有"-"则去掉
strVal = s.substring( 1 );
}
return new Double( strVal ).doubleValue();
}
//将显示框中的值转换为Double
double getNumberOnDisplay() {
return new Double(t_result.getText()).doubleValue();
}
//清除屏幕并设置所有的标识
void clear(){
firsttime = true;
lastOp = null;
strVal = "";
total = 0;
number = 0;
t_result.setText("0");
}
}
public static void main(String args[]){
CalcAppDemo c = new CalcAppDemo("简单的计算器程序");
c.setVisible(true);
}
}
//这是我以前写的,参考一下吧
展开全部
import java.awt.*;
import java.awt.event.*;
import java.math.BigDecimal;
import javax.swing.*;
public class Calculater implements ActionListener
{
private String text=new String();//全局变量
private String buffer=new String();
private String[]names = {"0","1","2","3","4","5","6","7","8","9","back","rs","+","-","*","/","sin",
"cos","square","!","="};//定义变量和按钮
private JButton[] buttonArray = new JButton[names.length];
private boolean append = false;//默认追加的false
private String operator="+";//默认运算符是“+”
private Double num1;
private Double num2;
private Double num3;
private Integer n;
JTextField jtf=new JTextField(20);
JTextField jtf2=new JTextField(20);//创建可以显示20个字符的文本档
public Calculater(String title){
JFrame jf=new JFrame(title); //创建窗口
JPanel jp=new JPanel(); //创建面板
jp.setLayout(new GridLayout(7,3)); //创建面板为网格布局
for (int i=0;i<buttonArray.length;i++){
buttonArray[i]=new JButton(names[i]); //依次添加按钮到面板中
buttonArray[i].addActionListener(this); //为按钮注册监听器
jp.add(buttonArray[i]);
}
jf.add(jtf,BorderLayout.NORTH);
jf.add(jtf2,BorderLayout.EAST);//添加文本框到窗体中
jf.add(jp); //添加面板到窗体中
jtf.setHorizontalAlignment(JTextField.RIGHT); //设置文本框的水平方向为右
jtf.setText(""); //设置初始文本为0
jtf.setEditable(false);
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void Clean()
{
jtf.setText("");
}
public void actionPerformed(ActionEvent e){
String comn=e.getActionCommand();
JButton bv=(JButton)(e.getSource());
if(bv==buttonArray[0]){
jtf.setText(jtf.getText()+"0");
}
if(bv==buttonArray[1]){
jtf.setText(jtf.getText()+"1");
}
if(bv==buttonArray[2]){
jtf.setText(jtf.getText()+"2");
}
if(bv==buttonArray[3]){
jtf.setText(jtf.getText()+"3");
}
if(bv==buttonArray[4]){
jtf.setText(jtf.getText()+"4");
}
if(bv==buttonArray[5]){
jtf.setText(jtf.getText()+"5");
}
if(bv==buttonArray[6]){
jtf.setText(jtf.getText()+"6");
}
if(bv==buttonArray[7]){
jtf.setText(jtf.getText()+"7");
}
if(bv==buttonArray[8]){
jtf.setText(jtf.getText()+"8");
}
if(bv==buttonArray[9]){
jtf.setText(jtf.getText()+"9");
}
if(bv==buttonArray[10]){
String temp=jtf.getText();
if(temp.length()>0)
jtf.setText(temp.substring(0,temp.length()-1));
}
if(bv==buttonArray[11]){
jtf.setText("0");
}
if(bv==buttonArray[12]){
num1=Double.parseDouble(jtf.getText().trim());
n=1;
jtf2.setText("+");
Clean();
}
if(bv==buttonArray[13]){
num1=Double.parseDouble(jtf.getText());
n=2;
jtf.setText(jtf.getText()+"");
Clean();
}
if(bv==buttonArray[14]){
num1=Double.parseDouble(jtf.getText());
n=3;
text="*";
Clean();
}
if(bv==buttonArray[15]){
num1=Double.parseDouble(jtf.getText());
n=4;
text="/";
Clean();
}
if(bv==buttonArray[20]){
num2=Double.parseDouble(jtf.getText());
if(n==1){
num3=num1+num2;
String num=String.valueOf(num3);
jtf.setText(num);
}
else if(n==2){
num3=num1-num2;
String num=String.valueOf(num3);
jtf.setText(num);
}
else if(n==3){
num3=num1*num2;
String num=String.valueOf(num3);
jtf.setText(num);
}
else if(n==4){
num3=num1/num2;
String num=String.valueOf(num3);
jtf.setText(num);
}
else if(n==5){
num3=Math.sin(num2*Math.PI/180);
String num=String.valueOf(num3);
jtf.setText(num);
}
else if(n==6){
num3=Math.cos(num2*Math.PI/180);
String num=String.valueOf(num3);
jtf.setText(num);
}
else if(n==7){
num3=num2*num2;
String num=String.valueOf(num3);
jtf.setText(num);
}
else if(n==8){
int num3=1;
for(int i=1;i<=num2;i++)
{
num3*=i;
}
String num=String.valueOf(num3);
jtf.setText(num);
}
}
if(bv==buttonArray[16]){
n=5;
jtf2.setText("sin");
}
if(bv==buttonArray[17]){
n=6;
jtf2.setText("cos");
}
if(bv==buttonArray[18]){
n=7;
jtf2.setText("square");
}
if(bv==buttonArray[19]){
n=8;
jtf2.setText("!");
}
}
public static void main(String[] args) {
new Calculater("Calculater");
// TODO Auto-generated method stub
}
}
我们刚做的java计算器。。。。给分吧
import java.awt.event.*;
import java.math.BigDecimal;
import javax.swing.*;
public class Calculater implements ActionListener
{
private String text=new String();//全局变量
private String buffer=new String();
private String[]names = {"0","1","2","3","4","5","6","7","8","9","back","rs","+","-","*","/","sin",
"cos","square","!","="};//定义变量和按钮
private JButton[] buttonArray = new JButton[names.length];
private boolean append = false;//默认追加的false
private String operator="+";//默认运算符是“+”
private Double num1;
private Double num2;
private Double num3;
private Integer n;
JTextField jtf=new JTextField(20);
JTextField jtf2=new JTextField(20);//创建可以显示20个字符的文本档
public Calculater(String title){
JFrame jf=new JFrame(title); //创建窗口
JPanel jp=new JPanel(); //创建面板
jp.setLayout(new GridLayout(7,3)); //创建面板为网格布局
for (int i=0;i<buttonArray.length;i++){
buttonArray[i]=new JButton(names[i]); //依次添加按钮到面板中
buttonArray[i].addActionListener(this); //为按钮注册监听器
jp.add(buttonArray[i]);
}
jf.add(jtf,BorderLayout.NORTH);
jf.add(jtf2,BorderLayout.EAST);//添加文本框到窗体中
jf.add(jp); //添加面板到窗体中
jtf.setHorizontalAlignment(JTextField.RIGHT); //设置文本框的水平方向为右
jtf.setText(""); //设置初始文本为0
jtf.setEditable(false);
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void Clean()
{
jtf.setText("");
}
public void actionPerformed(ActionEvent e){
String comn=e.getActionCommand();
JButton bv=(JButton)(e.getSource());
if(bv==buttonArray[0]){
jtf.setText(jtf.getText()+"0");
}
if(bv==buttonArray[1]){
jtf.setText(jtf.getText()+"1");
}
if(bv==buttonArray[2]){
jtf.setText(jtf.getText()+"2");
}
if(bv==buttonArray[3]){
jtf.setText(jtf.getText()+"3");
}
if(bv==buttonArray[4]){
jtf.setText(jtf.getText()+"4");
}
if(bv==buttonArray[5]){
jtf.setText(jtf.getText()+"5");
}
if(bv==buttonArray[6]){
jtf.setText(jtf.getText()+"6");
}
if(bv==buttonArray[7]){
jtf.setText(jtf.getText()+"7");
}
if(bv==buttonArray[8]){
jtf.setText(jtf.getText()+"8");
}
if(bv==buttonArray[9]){
jtf.setText(jtf.getText()+"9");
}
if(bv==buttonArray[10]){
String temp=jtf.getText();
if(temp.length()>0)
jtf.setText(temp.substring(0,temp.length()-1));
}
if(bv==buttonArray[11]){
jtf.setText("0");
}
if(bv==buttonArray[12]){
num1=Double.parseDouble(jtf.getText().trim());
n=1;
jtf2.setText("+");
Clean();
}
if(bv==buttonArray[13]){
num1=Double.parseDouble(jtf.getText());
n=2;
jtf.setText(jtf.getText()+"");
Clean();
}
if(bv==buttonArray[14]){
num1=Double.parseDouble(jtf.getText());
n=3;
text="*";
Clean();
}
if(bv==buttonArray[15]){
num1=Double.parseDouble(jtf.getText());
n=4;
text="/";
Clean();
}
if(bv==buttonArray[20]){
num2=Double.parseDouble(jtf.getText());
if(n==1){
num3=num1+num2;
String num=String.valueOf(num3);
jtf.setText(num);
}
else if(n==2){
num3=num1-num2;
String num=String.valueOf(num3);
jtf.setText(num);
}
else if(n==3){
num3=num1*num2;
String num=String.valueOf(num3);
jtf.setText(num);
}
else if(n==4){
num3=num1/num2;
String num=String.valueOf(num3);
jtf.setText(num);
}
else if(n==5){
num3=Math.sin(num2*Math.PI/180);
String num=String.valueOf(num3);
jtf.setText(num);
}
else if(n==6){
num3=Math.cos(num2*Math.PI/180);
String num=String.valueOf(num3);
jtf.setText(num);
}
else if(n==7){
num3=num2*num2;
String num=String.valueOf(num3);
jtf.setText(num);
}
else if(n==8){
int num3=1;
for(int i=1;i<=num2;i++)
{
num3*=i;
}
String num=String.valueOf(num3);
jtf.setText(num);
}
}
if(bv==buttonArray[16]){
n=5;
jtf2.setText("sin");
}
if(bv==buttonArray[17]){
n=6;
jtf2.setText("cos");
}
if(bv==buttonArray[18]){
n=7;
jtf2.setText("square");
}
if(bv==buttonArray[19]){
n=8;
jtf2.setText("!");
}
}
public static void main(String[] args) {
new Calculater("Calculater");
// TODO Auto-generated method stub
}
}
我们刚做的java计算器。。。。给分吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个你HI我吧,我告诉你方法,光在这里写,比较麻烦
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询