用Java编写一个计算机
作者:互联网
用Java编写一个计算机
package com;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.GridLayout;
public class GUIDemol {
public static void main(String[] args){
Calculator f=new Calculator();
f.setTitle(“计算机”);
f.setSize(250,180);
f.setLocation(400,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
class Calculator extends JFrame implements ActionListener{
JTextField jt;
JButton jb1,jb2,jb3,jb4,jb5,jb6,jb7,jb8,jb9,jb0,jbdian,jbjia,jbjian,jbcheng,jbchu,jbdeng;
String flag;
double result=0;
double num1,num2;
Calculator(){
JPanel jp=new JPanel();
jp.setLayout(new GridLayout(4,4,4,4));
jb1=new JButton(“1”);
jb2=new JButton(“2”);
jb3=new JButton(“3”);
jb4=new JButton(“4”);
jb5=new JButton(“5”);
jb6=new JButton(“6”);
jb7=new JButton(“7”);
jb8=new JButton(“8”);
jb9=new JButton(“9”);
jb0=new JButton(“0”);
jbjia=new JButton("+");
jbjian=new JButton("-");
jbcheng=new JButton("*");
jbchu=new JButton("/");
jbdeng=new JButton("=");
jbdian=new JButton(".");
jp.add(jb1);
jp.add(jb2);
jp.add(jb3);
jp.add(jbjia);
jp.add(jb4);
jp.add(jb5);
jp.add(jb6);
jp.add(jbjian);
jp.add(jb7);
jp.add(jb8);
jp.add(jb9);
jp.add(jbcheng);
jp.add(jbdian);
jp.add(jb0);
jp.add(jbdeng);
jp.add(jbchu);
jt=new JTextField(20);
jt.setHorizontalAlignment(JTextField.RIGHT);
add(jt,BorderLayout.NORTH);
add(jp);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
jb5.addActionListener(this);
jb6.addActionListener(this);
jb7.addActionListener(this);
jb8.addActionListener(this);
jb9.addActionListener(this);
jb0.addActionListener(this);
jbdian.addActionListener(this);
jbjia.addActionListener(this);
jbjian.addActionListener(this);
jbcheng.addActionListener(this);
jbchu.addActionListener(this);
jbdeng.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
String s=jt.getText();
if(e.getSource()==jb1){
jt.setText(s+“1”);
} else if(e.getSource()==jb2){
jt.setText(s+“2”);
} else if(e.getSource()==jb3){
jt.setText(s+“3”);
} else if(e.getSource()==jb4){
jt.setText(s+“4”);
} else if(e.getSource()==jb5){
jt.setText(s+“5”);
} else if(e.getSource()==jb6){
jt.setText(s+“6”);
} else if(e.getSource()==jb7){
jt.setText(s+“7”);
} else if(e.getSource()==jb8){
jt.setText(s+“8”);
} else if(e.getSource()==jb9){
jt.setText(s+“9”);
} else if(e.getSource()==jb0){
jt.setText(s+“0”);
} else if(e.getSource()==jbdian){
jt.setText(s+".");
}
else if(e.getSource()==jbjia){
num1=Double.parseDouble(s);
flag=“jia”;
jt.setText("");
}else if(e.getSource()==jbjian){
num1=Double.parseDouble(s);
flag=“jian”;
jt.setText("");
}else if(e.getSource()==jbcheng){
num1=Double.parseDouble(s);
flag=“cheng”;
jt.setText("");
}else if(e.getSource()==jbchu){
num1=Double.parseDouble(s);
flag=“chu”;
jt.setText("");
}else if(e.getSource()==jbdeng){
num2=Double.parseDouble(s);
if(“jia”.equals(flag))
result=num1+num2;
else if(“jian”.equals(flag))
result=num1-num2;
else if(“cheng”.equals(flag))
result=num1*num2;
else if(“chu”.equals(flag))
result=num1/num2;
s=result+"";
jt.setText(s);
}
}
}
这个有一点缺陷麻烦有人帮我解决(实践后可知)
标签:Java,计算机,jt,jp,else,add,new,编写,JButton 来源: https://blog.csdn.net/yibmfff/article/details/111349463