欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JAVAFX编写程序,实现加法、减法、乘法和除法操作编写程序,实现加法、减法、乘法和除法操作

程序员文章站 2022-03-30 19:21:28
...
编写程序,实现加法、减法、乘法和除法操作。运行效果如图所示。

JAVAFX编写程序,实现加法、减法、乘法和除法操作编写程序,实现加法、减法、乘法和除法操作

package application2;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
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.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class Calculation extends Application{
	public static void main(String[] args) {
		launch(args);
	}
	@Override
	public void start(Stage stage){
		Label label1=new Label("数1"),
			  label2=new Label("数2"),
			  label3=new Label("=");
		TextField textField1=new TextField(),
				textField2=new TextField(),
				textField3=new TextField();
//				textField1.setPrefWidth(50);//设施输入框宽度,若无则默认大小
//				textField2.setPrefWidth(50);
//				textField3.setPrefWidth(50);
		Button button1=new Button("加法"),
				button2=new Button("减法"),
				button3=new Button("乘法"),
				button4=new Button("除法");
		//进行加法运算
		button1.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				double c=Double.parseDouble(textField1.getText())+Double.parseDouble(textField2.getText());
				textField3.setText(""+c);
			}
		});
		//进行减法运算
		button2.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				double c=Double.parseDouble(textField1.getText())-Double.parseDouble(textField2.getText());
				textField3.setText(""+c);
			}
		});
		//进行乘法运算
		button3.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				double c=Double.parseDouble(textField1.getText())*Double.parseDouble(textField2.getText());
				textField3.setText(""+c);
			}
		});
		//进行除法运算
		button4.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				double c=Double.parseDouble(textField1.getText())/Double.parseDouble(textField2.getText());
				textField3.setText(""+c);
			}
		});
		HBox hBox=new HBox();
			hBox.setPadding(new Insets(20,20,20,20 ));
			hBox.setSpacing(20);
			hBox.setAlignment(Pos.BOTTOM_CENTER);
			hBox.getChildren().addAll(button1,button2,button3,button4);
		GridPane rootPane=new GridPane();
			rootPane.setPadding(new Insets(10,10,10,10));
			rootPane.add(label1, 0, 0);
			rootPane.add(textField1, 1,0);
			rootPane.add(label2, 2,0);
			rootPane.add(textField2,3,0);
			rootPane.add(label3, 4,0);
			rootPane.add(textField3, 5, 0);
			rootPane.add(hBox, 0,1,6,2);
			Scene scene=new Scene(rootPane,400,120);
			stage.setTitle("计数器示例");
			stage.setScene(scene);
			stage.show();
	}
}

相关标签: javafx