Demo entry 6644011

java

   

Submitted by anonymous on Sep 30, 2017 at 08:03
Language: Java. Code size: 5.9 kB.

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class Main extends Application {
    TextField textField=new TextField();
    @Override
    public void start(Stage primaryStage) throws Exception{
        Computer computer=new Computer();
        final int BW=100;
        final int BH=100;
        Label label=new Label();
        GridPane gridPane=new GridPane();
        Button button[]=new Button[15];
        for( int i =1 ; i <=button.length; ++i){
            button[i-1]=new Button();
            button[i-1].setText(String.valueOf(i));
            button[i-1].setPrefSize(BW, BH);
            button[i-1].setFont(new Font(25));      //字号大小
            gridPane.add(button[i-1], (i - 1) % 3, i>3?(i > 6 ? (i > 9?(i>12 ?4:3): 2):1):0);
        }
        button[9].setText("0");
        button[10].setText("+");
        button[11].setText("-");
        button[12].setText("*");
        button[13].setText("/");
        button[14].setText("√");
        button[0].setOnAction(e ->{
            textField.setText(textField.getText()+"1");
        });
        button[1].setOnAction(e ->{
            textField.setText(textField.getText()+"2");
        });
        button[2].setOnAction(e ->{
            textField.setText(textField.getText()+"3");
        });
        button[3].setOnAction(e ->{
            textField.setText(textField.getText()+"4");
        });
        button[4].setOnAction(e ->{
            textField.setText(textField.getText()+"5");
        });
        button[5].setOnAction(e ->{
            textField.setText(textField.getText()+"6");
        });
        button[6].setOnAction(e ->{
            textField.setText(textField.getText()+"7");
        });
        button[7].setOnAction(e ->{
            textField.setText(textField.getText()+"8");
        });
        button[8].setOnAction(e ->{
            textField.setText(textField.getText()+"9");
        });
        button[9].setOnAction(e ->{
            textField.setText(textField.getText()+"0");
        });
        Button buttonc=new Button();
        buttonc.setPrefSize(2*BW,BH);
        buttonc.setText("C");
        buttonc.setOnAction(e->{textField.setText("");});
        gridPane.add(buttonc,4,2);
        textField.setPrefSize(2*BW,BH);
        textField.setFont(new Font(30));

        Button buttond=new Button();
        buttond.setPrefSize(2*BW,BH);
        buttond.setText("=");
        buttond.setOnAction(e->{ double num2;
            num2=Double.parseDouble(textField.getText());
            computer.setNum2(num2);
            textField.setText(String.valueOf(computer.getResult()));
        });;
        gridPane.add(buttond,4,3);
        textField.setPrefSize(2*BW,BH);
        textField.setFont(new Font(30));

        gridPane.add(textField,4,0);

        button[10].setOnAction(e ->{
            double num1;
            num1=Double.parseDouble(textField.getText());
            computer.setNum1(num1);
            computer.setFlag('+');
            textField.setText("");
        });

        button[11].setOnAction(e ->{
            double num1;
            num1=Double.parseDouble(textField.getText());
            computer.setNum1(num1);
            computer.setFlag('-');
            textField.setText("");
        });
        button[12].setOnAction(e ->{
            double num1;
            num1=Double.parseDouble(textField.getText());
            computer.setNum1(num1);
            computer.setFlag('*');
            textField.setText("");
        });
        button[13].setOnAction(e ->{
            double num1;
            num1=Double.parseDouble(textField.getText());
            computer.setNum1(num1);
            computer.setFlag('/');
            textField.setText("");
        });
        button[14].setOnAction(e ->{
            int num3;
            num3= Integer.parseInt(textField.getText());
            computer.setNum3(num3);
            computer.setFlag('√');
            textField.setText("");
        });

        primaryStage.setTitle("计算器");
        primaryStage.setScene(new Scene(gridPane, 500,300 ));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

class Computer {
    private double num1;
    private double num2;
    private double num3;
    char flag;
    public void setFlag(char flag)
    {
        this.flag = flag;
    }
    public Computer()
    {
        num1 = 0;
        num2 = 0;
        num3 = 0;
    }
    public Computer(double n1,double n2,double n3,char flag){
        this.num1 = n1;
        this.num2 = n2;
        this.num3 = n3;
        this.flag = flag;    }
    public double getNum1() {
        return num1;
    }
    public void setNum1(double num1) {
        this.num1 = num1;
    }
    public double getNum2() {
        return num2;
    }
    public void setNum2(double num2) {
        this.num2 = num2;
    }
    public double getNum3() {
        return num3;
    }
    public void setNum3(double num3) {
        this.num3 =  Math.sqrt(num3);}

    public double getResult() {
        switch (this.flag) {
            case '+':
                return num1+num2;
            case '-':
                return num1-num2;
            case '*':
                return num1*num2;
            case '/':
                return num1/num2;
            case '√':
                return num3 ;

            default:
                return 0;
        }    }}

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).