- import java.awt.Color;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import javax.swing.Icon;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JTextField;
- class Courser implements ActionListener{
- private JFrame jf=new JFrame("newComputer");
- private JTextField jte=new JTextField();
- private Icon icon=new ImageIcon("A:"+File.separator+"b.jpg");
- private JLabel jl=new JLabel(icon);
- private JButton j0,j1,j2,j3,j4,j5,j6,j7,j8,j9,j11,j12,j13,j14,j15,j16,j17,j18;
- private Double w=0.0;
- private String s1="";
- private Double d1=0.0;
- private String s2="";
- private Double d2=0.0;
- private String s3="";
- private Double d3=0.0;
- private boolean boo1=false;
- private boolean boo2=false;
- private boolean boo3=false;
- public Courser(){
- this.Jbutton();
- this.Setsize();
- jf.add(jl);
- jf.add(jte);
- jf.add(j1);
- jf.add(j2);
- jf.add(j3);
- jf.add(j4);
- jf.add(j5);
- jf.add(j6);
- jf.add(j7);
- jf.add(j8);
- jf.add(j9);
- jf.add(j0);
- jf.add(j11);
- jf.add(j12);
- jf.add(j13);
- jf.add(j14);
- jf.add(j15);
- jf.add(j16);
- jf.add(j17);
- jf.add(j18);
- jf.setVisible(true);
- }
- public void Setsize(){
- jte.setText("0.0");
- jf.setLayout(null);
- jte.setEnabled(false);
- jte.setHorizontalAlignment(JTextField.RIGHT);
- jf.setSize(390,350);
- jl.setSize(390, 350);
- jte.setBounds(10, 10, 350, 40);
- j1.setBounds(10, 60, 80, 40);
- j2.setBounds(100, 60, 80, 40);
- j3.setBounds(190, 60, 80, 40);
- j11.setBounds(280, 60, 80, 40);
- j4.setBounds(10, 110, 80, 40);
- j5.setBounds(100, 110, 80, 40);
- j6.setBounds(190, 110, 80, 40);
- j12.setBounds(280, 110, 80, 40);
- j7.setBounds(10, 160, 80, 40);
- j8.setBounds(100, 160, 80, 40);
- j9.setBounds(190, 160, 80, 40);
- j13.setBounds(280, 160, 80, 40);
- j0.setBounds(10, 210, 80, 40);
- j18.setBounds(100, 210, 80, 40);
- j17.setBounds(190, 210, 80, 40);
- j14.setBounds(280, 210, 80, 40);
- j15.setBounds(10, 260, 170, 40);
- j16.setBounds(190, 260, 170, 40);
- }
- public void Jbutton(){
- j0=JB("0");
- j1=JB("1");
- j2=JB("2");
- j3=JB("3");
- j4=JB("4");
- j5=JB("5");
- j6=JB("6");
- j7=JB("7");
- j8=JB("8");
- j9=JB("9");
- j11=JB("+");
- j13=JB("*");
- j14=JB("/");
- j15=JB("backspace");
- j16=JB("c");
- j17=JB("=");
- j18=JB(".");
- }
- public JButton JB(String s){
- JButton jbu=new JButton(s);
- jbu.addActionListener(this);
- return jbu;
- }
- public void actionPerformed(ActionEvent e) {
- JButton b=(JButton)e.getSource();
- if(Double.parseDouble(jte.getText())==0.0){
- boo1=false;
- }
- if(b==j1||b==j2||b==j3||b==j4||b==j5||b==j6||b==j7||b==j8||b==j9){/
- if(boo2==false){ //
- if(boo1){ //
- s1+=b.getText(); //
- jte.setText(jte.getText()+b.getText()); //
- }else{ //
- if(boo3){ //
- boo3=false; //
- s1+=b.getText(); //
- jte.setText(jte.getText()+b.getText()); //
- }else{ //
- s1=b.getText(); //
- jte.setText(s1); //
- boo1=true; //
- } //
- } //
- }if(boo2==true){ //
- if(boo1){ //
- s2+=b.getText(); //
- jte.setText(s2); //
- }else{ //
- if(boo3){ //
- boo3=false; //
- s2+=b.getText(); //
- jte.setText(s2); //
- }else{ //
- s2=b.getText(); //
- jte.setText(s2); //
- boo1=true; //
- } //
- } //
- }if("="==s3){ //
- boo2=false; //
- s1=""; //
- s2=""; //
- } //
- }
- if(b==j0){
- if(boo2==false){
- if(boo1){
- s1+=b.getText();
- jte.setText(jte.getText()+b.getText());
- }else{
- s1=b.getText();
- jte.setText("0");
- }
- }if(boo2==true){
- if(boo1){
- s2+=b.getText();
- jte.setText(s2);
- }else{
- s2=b.getText();
- jte.setText("0.0");
- }
- }if("="==s3){
- boo2=false;
- s1="";
- s2="";
- }
- }
- if(b==j18){
- if("="==s3){
- boo2=false;
- s1="";
- s2="";
- }
- if(boo2==false){
- if(s1!=""){
- if(!(s1.contains("."))){
- s1+=b.getText();
- jte.setText(s1);
- boo3=true;
- }
- }
- }
- if(boo2==true){
- if(s2!=""){
- if(!(s2.contains("."))){
- s2+=b.getText();
- jte.setText(s2);
- boo3=true;
- }
- }
- }
- }
- if(b==j11||b==j12||b==j13||b==j14||b==j17){
- if(s1!=""&&s2==""){
- boo2=true;
- }
- if(s2!=""){
- d1=Double.parseDouble(s1);
- d2=Double.parseDouble(s2);
- char c=s3.charAt(0);
- switch (c){
- case'+':{
- d3=d1+d2;
- break;
- }
- case'-':{
- d3=d1-d2;
- break;
- }
- case'*':{
- d3=d1*d2;
- break;
- }
- case'/':{
- d3=d1/d2;
- break;
- }
- }
- jte.setText(""+d3);
- s1=""+d3;
- s2="";
- }
- s3=b.getText();
- }
- if(b==j16){
- s1="0.0";
- s2="0.0";
- d1=0.0;
- d2=0.0;
- jte.setText("0.0") ;
- }
- if(b==j15){
- if(boo2==false){
- if(jte.getText().length()==1){
- s1="0.0";
- s2="0.0";
- d1=0.0;
- d2=0.0;
- jte.setText("0.0");
- }else{
- s1=jte.getText().substring(0, jte.getText().length()-1);
- jte.setText(jte.getText().substring(0, jte.getText().length()-1));
- }
- }else{
- if(jte.getText().length()==1){
- s2="0.0";
- d2=0.0;
- jte.setText("0.0");
- }else{
- s2=jte.getText().substring(0, jte.getText().length()-1);
- jte.setText(jte.getText().substring(0, jte.getText().length()-1));
- }
- }
- }
- }
- }
- public class NewCom {
- public static void main(String[] args) {
- new Courser();
- }
- }
来源: http://www.phpxs.com/code/1001739/