나는 새로운 애호가 코더입니다. 나는 이것을 알아낼 수 없으며 지금까지 검색은 쓸모 없었다. FXML 파일에서 두 개의 토글 버튼을 사용하고 있는데 클릭하면 부울 변수를 정의하고 싶습니다. 그런 다음 나중에 해당 변수를 사용하여 일부 코드를 실행하기 위해 이동하는 경로를 나중에 드라이브하고 싶습니다. 변수를 정의하는 방법을 알 수 없습니다. 어떤 안내도 환영합니다.FXML과 JavaFX를 사용하여 토글 버튼을 사용하여 부울 변수를 설정하는 방법은 무엇입니까?
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>
및 컨트롤러 :
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;
@FXML protected void handleToggleYesAction(ActionEvent t) {
boolean tobaccoToggle = true;
}
@FXML protected void handleToggleNoAction(ActionEvent t) {
boolean 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);
}
}
오류의 라인 (36)에서 발생 나는 아래 코드의 두 가지 소스를 게시합니다 컨트롤러 : 기호, 기호를 찾을 수 없습니다. tobaccoToggle
매력처럼 작동했습니다. 고맙습니다! – stwinward