Java 编写简单的计算器(可视化)

编写一个简单的计算器能够计算加减乘除法

运行结果:

Java 编写简单的计算器(可视化)_第1张图片

package javafxTest;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.layout.*;
import javafx.scene.text.Font;
import javafx.stage.Stage;
//import javafx.event.ActionEvent;
//import javafx.event.EventHandler;

public class Calculator extends Application {
  
  double left;
  double right;
  char op;

  @Override // Override the start method in the Application class
  public void start(Stage primaryStage) {
    // Create a border pane 
	primaryStage.getIcons().add(new Image("file:F:\\预科\\timg.jpg"));//左上角的图标
    BorderPane pane = new BorderPane();

    // Place nodes in the pane

    TextField input =  new TextField();
    input.setPrefHeight(40);
    input.setFont(Font.font("",25));
    pane.setTop(input); 

    AnchorPane gridPane = new AnchorPane();
    
    // Place nodes in the pane

    Button[] buttonGroup = { 
       new Button("1") ,new Button("2"),new Button("3"),new Button("4"),
       new Button("5") ,new Button("6"),new Button("7"),new Button("8"),
       new Button("9") ,new Button("0"),new Button("."),new Button("/"),
       new Button("+") ,new Button("-"),new Button("*"),new Button("="),
       new Button("Ac"),new Button("Close")};
    
    gridPane.getChildren().addAll(buttonGroup);
    
    for(int i=0; i { 
             Button btn = (Button) e.getSource();         
             switch (btn.getText()) {
                 case "1":   case "2":   case "3":   case "4":   case "5":      case "6": 
                 case "7":   case "8":   case "9":   case "0":   case ".":
                     input.setText( input.getText()+ btn.getText()) ; break;
                 case "+" :   case "-" :   case "*" :  case "/" :  
                     left = Double.parseDouble(input.getText()) ;
                     op =   btn.getText().charAt(0);
                     input.setText("") ;
                     break;
                 case "=": 
                     right = Double.parseDouble(input.getText()) ;
                      switch(op) {
                          case '+' :    input.setText(Double.toString(left+right)); break;
                          case '-' :  input.setText(Double.toString(left-right)); break;
                          case '*' :   input.setText(Double.toString(left*right)); break;
                          case  '/' :  input.setText(Double.toString(left/right)); break;
                       }
                  }
             }
            );
      }
    
	
	
    pane.setCenter( gridPane); 

    // Create a scene and place it in the stage
    Scene scene = new Scene(pane);
    primaryStage.setTitle("计算器"); // Set the stage title
    primaryStage.setScene(scene); // Place the scene in the stage
    primaryStage.setHeight(325);
    primaryStage.setWidth(265);
    primaryStage.setResizable(false);//固定窗口大小不能改变
    primaryStage.show(); // Display the stage
    
    //Ac 按键 添加清空输入框
    buttonGroup[16].setOnAction(new EventHandler() {  
		
		@Override
		public void handle(ActionEvent arg0) {
			input.setText("");
		}
	});
    
    //close 按钮添加关闭事件
	buttonGroup[17].setOnAction(new EventHandler() {
			
			@Override
			public void handle(ActionEvent arg0) {
				primaryStage.close();
			}
		});
    
    
  }
  
  public static void main(String[] args)
  {
	  launch(args);
  }
}

备注:代码里有很多可以优化的地方

你可能感兴趣的:(java)