Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Variables 使用FXML和JavaFX,如何使用切换按钮设置布尔变量?_Variables_Javafx_Boolean_Fxml_Togglebutton - Fatal编程技术网

Variables 使用FXML和JavaFX,如何使用切换按钮设置布尔变量?

Variables 使用FXML和JavaFX,如何使用切换按钮设置布尔变量?,variables,javafx,boolean,fxml,togglebutton,Variables,Javafx,Boolean,Fxml,Togglebutton,我是一个新的业余程序员。我搞不懂这个,搜索到目前为止都是徒劳的。我使用FXML文件中的两个切换按钮,单击时,我想定义一个布尔变量。然后我想用这个变量来驱动我执行代码的路径。我不知道如何定义这个变量。欢迎任何指导。我将在下面发布两个代码源: 场景生成器创建的FXML文件代码: <?xml version="1.0" encoding="UTF-8"?> <?import java.lang.*?> <?import java.net.*?> <?impo

我是一个新的业余程序员。我搞不懂这个,搜索到目前为止都是徒劳的。我使用FXML文件中的两个切换按钮,单击时,我想定义一个布尔变量。然后我想用这个变量来驱动我执行代码的路径。我不知道如何定义这个变量。欢迎任何指导。我将在下面发布两个代码源:

场景生成器创建的FXML文件代码:

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

<?import java.lang.*?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<GridPane alignment="CENTER" hgap="10" styleClass="root" vgap="10" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="bmi.calculator.BMICalculatorController">   

<padding><Insets bottom="10" left="25" right="25" top="25" /></padding>
    <children>


    <Text id="header-text" text="BMI Calculator" GridPane.columnIndex="0" GridPane.columnSpan="3" GridPane.rowIndex="0" />

    <Label text="Please enter your age:" GridPane.columnIndex="0" GridPane.rowIndex="1" />

    <TextField fx:id="ageBox" GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.rowIndex="1" />

    <Label text="Enter your height in inches:" GridPane.columnIndex="0" GridPane.rowIndex="2" />

    <TextField fx:id="heightBox" GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.rowIndex="2" />  

    <Label text="Enter your weight in pounds:" GridPane.columnIndex="0" GridPane.rowIndex="3" />

    <TextField fx:id="weightBox" GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.rowIndex="3" />  

    <Label text="Do you use tobbaco?" GridPane.columnIndex="0" GridPane.rowIndex="4" />  

    <ToggleButton onAction="#handleToggleYesAction" mnemonicParsing="false" prefHeight="31.0" prefWidth="85.0" text="Yes" GridPane.columnIndex="1" GridPane.rowIndex="4" />
    <ToggleButton onAction="#handleToggleNoAction" mnemonicParsing="false" prefHeight="31.0" prefWidth="85.0" text="No" GridPane.columnIndex="2" GridPane.rowIndex="4" />

    <HBox alignment="BOTTOM_RIGHT" spacing="10" GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.rowIndex="6">
        <children>
           <Button onAction="#handleCalculateButtonAction" text="Calculate" />
        </children>
    </HBox>

    <Text fx:id="outputText1" wrappingWidth="211.0" GridPane.columnIndex="1" GridPane.rowIndex="8" />

    </children>

    <stylesheets>
        <URL value="@Login.css" />
    </stylesheets>
    <columnConstraints>
        <ColumnConstraints maxWidth="200.0" minWidth="180.0" prefWidth="200.0" />
        <ColumnConstraints maxWidth="100.0" minWidth="35.0" prefWidth="85.0" />
        <ColumnConstraints maxWidth="100.0" minWidth="25.0" prefWidth="85.0" />
    </columnConstraints>
    <rowConstraints>
    <RowConstraints />
        <RowConstraints />
        <RowConstraints />
        <RowConstraints />
        <RowConstraints />
        <RowConstraints />
        <RowConstraints />
        <RowConstraints />
        <RowConstraints />
    </rowConstraints>

</GridPane>

控制器第36行出错:找不到符号,符号:Tobaccottoggle

必须在类BMICalculatorController中定义“Tobaccottoggle”字段。由于作用域问题,您将遇到错误。在代码中,“handleToggleYesAction”中的tobaccoToggle变量在函数调用时创建,在函数完成时终止。“Handletoglenoaction”中也有同样的问题

这是代码

package bmi.calculator;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.text.Text;

public class BMICalculatorController {
    @FXML private Text outputText1;
    @FXML private TextField ageBox;
    @FXML private TextField heightBox;
    @FXML private TextField weightBox;
    private boolean tobaccoToggle;

    @FXML protected void handleToggleYesAction(ActionEvent t) {
        tobaccoToggle = true;
    }

    @FXML protected void handleToggleNoAction(ActionEvent t) {
        tobaccoToggle = false;
    }

    @FXML protected void handleCalculateButtonAction(ActionEvent event) {

        //obtains the variables we need to work with
        boolean tobacco = tobaccoToggle; //error is here
        double weight = Double.parseDouble(weightBox.getText());
        double height = Double.parseDouble(heightBox.getText());
        int age = Integer.parseInt(ageBox.getText());

        //Disregard everything else below here for now - I'll be changing it up some later.
        //performs BMI calculation
        double bmi;
        double stepOne;
        stepOne = weight/height;
        double stepTwo;
        stepTwo = stepOne/height;
        bmi = stepTwo*703;

        //round BMI to two decimal places
        double roundedbmi = bmi;
        roundedbmi = roundedbmi * 100;
        roundedbmi = Math.round(roundedbmi);
        roundedbmi = roundedbmi/100;

        //transform height to feet and inches
        int height2 = (int)Math.round(height);
        int heightFeet = height2/12;
        int heightInches = height2%12;

        //transform weight to int
        int weight2 = (int)Math.round(weight);

        outputText1.setText("Your BMI is " + roundedbmi);

        //tell the user what they enetered
        System.out.println("Your height is " + heightFeet + " feet and " + heightInches + " inches.");
        System.out.println("Your weight is " + weight2 + " pounds.");
        System.out.println("Your BMI is " + roundedbmi);
        System.out.println("Your tobacco use is " + tobacco);
    }
}

工作得很有魅力。非常感谢。
package bmi.calculator;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.text.Text;

public class BMICalculatorController {
    @FXML private Text outputText1;
    @FXML private TextField ageBox;
    @FXML private TextField heightBox;
    @FXML private TextField weightBox;
    private boolean tobaccoToggle;

    @FXML protected void handleToggleYesAction(ActionEvent t) {
        tobaccoToggle = true;
    }

    @FXML protected void handleToggleNoAction(ActionEvent t) {
        tobaccoToggle = false;
    }

    @FXML protected void handleCalculateButtonAction(ActionEvent event) {

        //obtains the variables we need to work with
        boolean tobacco = tobaccoToggle; //error is here
        double weight = Double.parseDouble(weightBox.getText());
        double height = Double.parseDouble(heightBox.getText());
        int age = Integer.parseInt(ageBox.getText());

        //Disregard everything else below here for now - I'll be changing it up some later.
        //performs BMI calculation
        double bmi;
        double stepOne;
        stepOne = weight/height;
        double stepTwo;
        stepTwo = stepOne/height;
        bmi = stepTwo*703;

        //round BMI to two decimal places
        double roundedbmi = bmi;
        roundedbmi = roundedbmi * 100;
        roundedbmi = Math.round(roundedbmi);
        roundedbmi = roundedbmi/100;

        //transform height to feet and inches
        int height2 = (int)Math.round(height);
        int heightFeet = height2/12;
        int heightInches = height2%12;

        //transform weight to int
        int weight2 = (int)Math.round(weight);

        outputText1.setText("Your BMI is " + roundedbmi);

        //tell the user what they enetered
        System.out.println("Your height is " + heightFeet + " feet and " + heightInches + " inches.");
        System.out.println("Your weight is " + weight2 + " pounds.");
        System.out.println("Your BMI is " + roundedbmi);
        System.out.println("Your tobacco use is " + tobacco);
    }
}