编程语言
首页 > 编程语言> > JavaFX、计算贷款界面和计算能量界面转换(Java8版本)

JavaFX、计算贷款界面和计算能量界面转换(Java8版本)

作者:互联网

package pack2;

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

public class LoanAndEnergy extends Application {
	private TextField n1 = new TextField();
	private TextField n2 = new TextField();
	private TextField n3 = new TextField();
	private TextField n4 = new TextField();
	private TextField n5 = new TextField();
	private Button switchToWater = new Button("Switch");
	private Button switchToLoan = new Button("Switch");
	private BorderPane pane = new BorderPane();
	
	@Override
	public void start(Stage primaryStage) throws Exception {//---
		pane.setCenter(getComputeLoan());
		pane.setBottom(switchToWater);
		pane.setStyle("-fx-background-color: grey; ");
		BorderPane.setAlignment(switchToWater, Pos.CENTER);
		BorderPane.setAlignment(switchToLoan, Pos.CENTER);
		
		n1.setAlignment(Pos.BOTTOM_RIGHT); //设置对齐为底部右对齐
		n2.setAlignment(Pos.BOTTOM_RIGHT);
		n3.setAlignment(Pos.BOTTOM_RIGHT);
		n4.setAlignment(Pos.BOTTOM_RIGHT);
		n5.setAlignment(Pos.BOTTOM_RIGHT);
		n4.setEditable(false); //设置为不可编辑
		n5.setEditable(false);
		
		switchToWater.setOnAction(e->switchToWater()); //注册了switchToWater()方法
		switchToLoan.setOnAction(e->switchToLoan());   //注册了switchToLoan()方法
		
		Scene scene = new Scene(pane,420,350);
		primaryStage.setTitle("Calculator");
		primaryStage.setScene(scene);
		primaryStage.setResizable(false); //设置舞台大小不可改变
		primaryStage.show();
	}//---
	
	//转换成计算能量界面方法
	private void switchToWater() {
		pane.setCenter(getComputeWater()); //设置中部为计算能量界面
		pane.setBottom(switchToLoan);      //设置底部为转向贷款界面的按钮
	}
	
	//转换成计算贷款界面方法
	private void switchToLoan() {
		pane.setCenter(getComputeLoan()); //设置中部为计算贷款界面
		pane.setBottom(switchToWater);	  //设置底部为转向能量界面的按钮
	}
	
	//返回计算能量界面方法
	private BorderPane getComputeWater() {
		GridPane gridPane = new GridPane();
		Button calculateWater = new Button("Calculate");
		Button resetWater = new Button("Reset");
		gridPane.setStyle("-fx-hgap: 25; -fx-vgap: 5; -fx-alignment: center; -fx-background-color: beige; ");
		
		gridPane.addColumn(0, new Label("Weight of Water(kg):"),new Label("Initial Temperature(℃):"),
				new Label("Final Temperature(℃):"),new Label("Energy Needed(KJ):"),resetWater);
		gridPane.addColumn(1, n1,n2,n3,n4,calculateWater);
		GridPane.setHalignment(resetWater, HPos.RIGHT);
		
		resetWater.setOnAction(e->new ComputeWater().reset(n1,n2,n3,n4));
		calculateWater.setOnAction(e->new ComputeWater().computePower(n1,n2,n3,n4));
		
		Label label = new Label("     计算能量\n------------------");
		label.setFont(Font.font("华文中宋", FontWeight.BOLD, 44));
		label.setTextFill(Color.WHITESMOKE);
		
		BorderPane borderPane = new BorderPane(gridPane);
		borderPane.setStyle("-fx-background-color: grey; ");
		borderPane.setTop(label);
		BorderPane.setAlignment(borderPane.getTop(), Pos.BOTTOM_CENTER);
		
		return borderPane;
	}
	
	//返回计算贷款界面方法
	private BorderPane getComputeLoan() {
		GridPane pane = new GridPane();
		Button calculateLoan = new Button("Calculate");
		Button resetLoan = new Button("Reset");
		pane.setStyle("-fx-hgap: 25; -fx-vgap: 5; -fx-alignment: center; -fx-background-color: beige; ");
		
		pane.addColumn(0, new Label("Annual Interest Rate:"),new Label("Number of Years:"),
				new Label("Loan Amount:"),new Label("Monthly Payment:"),new Label("Total Payment:"),resetLoan);
		pane.addColumn(1, n1,n2,n3,n4,n5,calculateLoan);
		GridPane.setHalignment(resetLoan, HPos.RIGHT);
		
		resetLoan.setOnAction(e->new ComputeWater().reset(n1, n2, n3, n4, n5));
		calculateLoan.setOnAction(e->new Loan().computeLoan(n1, n2, n3, n4, n5));
		
		Label label = new Label("     计算贷款\n------------------");
		label.setFont(Font.font("华文中宋", FontWeight.BOLD, 44));
		label.setTextFill(Color.WHITESMOKE);
		
		BorderPane borderPane = new BorderPane(pane);
		borderPane.setStyle("-fx-background-color: grey; ");
		borderPane.setTop(label);
		BorderPane.setAlignment(borderPane.getTop(), Pos.BOTTOM_CENTER);
		
		return borderPane;
	}
	
	public static void main(String[] args) {
		launch(args);
	}
}
package pack2;

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

/**
 * 计算水从初始温度加热到最终温度所需的能量(能量(千焦) = 水的重量(kg)*(最终温度-初始温度)*4184)
 * 本程序共创建了两个面板--GridPane、BorderPane.
 * GridPane用于存放 标签 及 文本域 以及 按钮 .
 * BorderPane存放 GridPane 和一个 Label标签,其中GridPane放于中间,Label标签放于顶部
 * 本程序中的两个按钮分别注册了 ActionEvent 事件.reset按钮注册了 reset函数 ,
 * calculate按钮注册了 computePower函数 .两个按钮均使用了lambda表达式.
 * */
public class ComputeWater extends Application {
	private TextField weightOfWater = new TextField();
	private TextField startTemperature = new TextField();
	private TextField endTemperature = new TextField();
	private TextField powerNeeded = new TextField();
	private Button calculate = new Button("Calculate");
	private Button reset = new Button("Reset");
	
	@Override
	public void start(Stage primaryStage) throws Exception {
		GridPane pane = new GridPane();
		pane.setStyle("-fx-hgap: 25; -fx-vgap: 5; -fx-alignment: center; ");
		
		pane.addColumn(0, new Label("Weight of Water(kg):"),new Label("Initial Temperature:"),
				new Label("Final Temperature:"),new Label("Energy Needed(KJ):"),reset);
		pane.addColumn(1, weightOfWater,startTemperature,endTemperature,powerNeeded,calculate);
		
		GridPane.setHalignment(calculate, HPos.LEFT);
		GridPane.setHalignment(reset, HPos.RIGHT);
		weightOfWater.setAlignment(Pos.BOTTOM_RIGHT);
		startTemperature.setAlignment(Pos.BOTTOM_RIGHT);
		endTemperature.setAlignment(Pos.BOTTOM_RIGHT);
		powerNeeded.setAlignment(Pos.BOTTOM_RIGHT);
		powerNeeded.setEditable(false);

		reset.setOnAction(e->reset());
		calculate.setOnAction(e->computePower());
		
		Label label = new Label("     计算能量\n------------------");
		label.setFont(Font.font("华文中宋", FontWeight.BOLD, 44));
		label.setTextFill(Color.DARKGREY);
		
		BorderPane borderPane = new BorderPane(pane);
		borderPane.setStyle("-fx-background-color: beige; ");
		borderPane.setTop(label);
		BorderPane.setAlignment(borderPane.getTop(), Pos.BOTTOM_CENTER);
		
		Scene scene = new Scene(borderPane,420,350);
		primaryStage.setTitle("Compute Energy");
		primaryStage.setScene(scene);
		primaryStage.setResizable(false); //设置是否可改变大小
		primaryStage.show();
	}
	
	//Reset all of the text field
	private void reset() {
		startTemperature.setText(null);
		endTemperature.setText(null);
		weightOfWater.setText(null);
		powerNeeded.setText(null);
	}
	
	public void reset(TextField...textField) {
		for (TextField textField2 : textField) {
			textField2.setText(null);
		}
	}
	
	//Compute the energy needed heating the water
	private void computePower(){
		try {
			double start = Double.parseDouble(startTemperature.getText());
			double end = Double.parseDouble(endTemperature.getText());
			double weight = Double.parseDouble(weightOfWater.getText());
			double power = weight*(end-start)*4184;
			
			powerNeeded.setText(String.format("%.2fKJ", power));
		}catch(NumberFormatException ex) {
			//Do not process when text field is null among three above
		}
		catch(NullPointerException ex) {
			//Do not any process when text field is null
		}
	}

	public void computePower(TextField n1, TextField n2, TextField n3, TextField n4){
		try {
			double weight = Double.parseDouble(n1.getText());
			double start = Double.parseDouble(n2.getText());
			double end = Double.parseDouble(n3.getText());
			double power = weight*(end-start)*4184;
			
			n4.setText(String.format("%.2fKJ", power));
		}catch(NumberFormatException ex) {
			//Do not any process when text field is null among three above
		}
		catch(NullPointerException ex) {
			//Do not any process when text field is null
		}
	}
	
	public static void main(String[] args) {
		launch(args);
	}

}

 

标签:界面,javafx,JavaFX,scene,Label,new,import,TextField,Java8
来源: https://blog.csdn.net/m0_62659797/article/details/120922992