JavaFX计算器应用程序在操作事件上设置

时间:2019-03-14 21:41:53

标签: java javafx

我正在尝试创建一个JavaFX程序,该程序从用户处获取第一个数字和第二个数字,并根据用户选择的按钮进行加,乘,减或除,然后在文本字段中显示结果。我已经创建了方法并正确设置了类,但是我不确定为什么单击按钮时事件不起作用。任何类型的方向将不胜感激。我也在使用Eclipse。谢谢

Main.java

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;


public class Main extends Application {
   @Override
   public void start(Stage primaryStage) {
       try {
           BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("Calculator.fxml"));
           Scene scene = new Scene(root,400,400);
           scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
           primaryStage.setScene(scene);
           primaryStage.show();
       } catch(Exception e) {
           e.printStackTrace();
       }
   }

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

Controller.java

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

import java.net.URL;
import java.util.ResourceBundle;

public class CalculatorController implements Initializable {

    @FXML
    private TextField tf3;
    @FXML
    private TextField tf2;
    @FXML
    private Button btnAdd;
    @FXML
    private Button btnDivide;
    @FXML
    private Button btnSubtract;
    @FXML
    private TextField tf1;
    @FXML
    private Button btnMultiply;

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        // TODO Auto-generated method stub
        btnAdd.setOnAction(e -> Add());
        btnSubtract.setOnAction(e -> Subtract());
        btnMultiply.setOnAction(e -> Multiply());
        btnDivide.setOnAction(e -> Divide());
    }

    private void Add() {

        int n1 = Integer.parseInt(tf1.getText());
        int n2 = Integer.parseInt(tf2.getText());
        int result = n1 + n2;

        tf3.setText(String.valueOf(result));
    }

    private void Subtract() {

        int n1 = Integer.parseInt(tf1.getText());
        int n2 = Integer.parseInt(tf2.getText());
        int result = n1 - n2;

        tf3.setText(String.valueOf(result));
    }

    private void Multiply() {

        int n1 = Integer.parseInt(tf1.getText());
        int n2 = Integer.parseInt(tf2.getText());
        int result = n1 * n2;

        tf3.setText(String.valueOf(result));

    }

    private void Divide() {

        int n1 = Integer.parseInt(tf1.getText());
        int n2 = Integer.parseInt(tf2.getText());
        int result = n1 / n2;

        tf3.setText(String.valueOf(result));

    }
}

fxml.java

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>

<SplitPane dividerPositions="0.5" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" orientation="VERTICAL" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
  <items>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
         <children>
            <Label layoutX="151.0" layoutY="54.0" text="Number 1:" />
            <Label layoutX="151.0" layoutY="98.0" text="Number 2:" />
            <Label layoutX="151.0" layoutY="140.0" text="Number 3:" />
            <TextField fx:id="tf1" layoutX="284.0" layoutY="49.0" />
            <TextField fx:id="tf2" layoutX="284.0" layoutY="93.0" />
            <TextField fx:id="tf3" layoutX="284.0" layoutY="135.0" />
         </children></AnchorPane>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
         <children>
            <Button fx:id="btnAdd" layoutX="108.0" layoutY="82.0" mnemonicParsing="false" text="Add" />
            <Button fx:id="btnSubtract" layoutX="234.0" layoutY="82.0" mnemonicParsing="false" text="Subtract" />
            <Button fx:id="btnMultiply" layoutX="362.0" layoutY="82.0" mnemonicParsing="false" text="Multiply" />
            <Button fx:id="btnDivide" layoutX="479.0" layoutY="82.0" mnemonicParsing="false" text="Divide" />
         </children></AnchorPane>
  </items>
</SplitPane>

1 个答案:

答案 0 :(得分:1)

您从未为FXML布局定义控制器类。

这可以通过以下两种方式之一完成:

1)在FXML文件本身内:

在FXML文件的根元素中,您需要指定一个fx:controller属性:

fx:controller="CalculatorController"

因此,您的根SplitPane元素应如下所示:

<SplitPane fx:controller="calculatorSample.CalculatorController" dividerPositions="0.5" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
           orientation="VERTICAL" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8"
           xmlns:fx="http://javafx.com/fxml/1">

如果您使用的是SceneBuilder来构建布局,则可以在左侧的“控制器”部分中设置控制器:

screenshot


2)在您的Main类中:

特别有用,如果您需要在加载布局之前配置控制器,则可以使用FXMLLoader指定控制器:

FXMLLoader loader = new FXMLLoader(getClass().getResource("Calculator.fxml");
loader.setController(new CalculatorController());
Scene scene = new Scene(loader.load());