如何在 运行 JavaFX 容器 class 之后立即在 GUI 中隐藏元素?
How do you hide elements in a GUI right after you run the JavaFX container class?
对于我的一个 Java 项目,我正在使用 JavaFX 来制作收银机。
要看GUI的图片,请
click here.
如 GUI 所示,程序将要求用户输入以查找用户购买的商品数量、用户支付的金额,并将在标签中显示必要的更改。但是,我想在某些时候隐藏此 UI 的某些元素。目前,我知道如何使用这些命令隐藏标签和按钮:
label.setVisible(false);
button.setVisible(false);
然而,我在这里需要的是让标签下面的所有元素说 + 7.00% Sales Tax
在我 运行 GUIController [=34= 开始时不可见] 这样我就不必单击任何其他按钮来进行这些更改。那么,你们有什么建议吗?感谢您的帮助!
CashRegister(包含主要 class)
package cashregister;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author Sammy
*/
public class CashRegister extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("RegisterGUI.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
注册器GUI控制器
(我省略了所有 "actual" 代码,这样我看起来就不会在我的项目中作弊)
package cashregister;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
/**
*
* @author Sammy
*/
public class RegisterGUIController implements Initializable {
@FXML
private Label title, lblSubtotal, lblTax, lblLine, lblTotalText, lblTotal,lblChangeText, lblChange;
@FXML
private Button handleNoodle, handlePizza, handleWing, handlePie, handleIce;
@FXML
private TextField inputNoodle, inputPizza, inputWings, inputPies, inputIce, inputPayment;
@FXML
private void handleNoodle(ActionEvent event) {
}
@FXML
private void handlePizza(ActionEvent event) {
}
@FXML
private void handleWing(ActionEvent event) {
}
@FXML
private void handlePie(ActionEvent event) {
}
@FXML
private void handleIce(ActionEvent event) {
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
}
注册GUI
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="415.0" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="cashregister.RegisterGUIController">
<children>
<Button fx:id="handlePizza" layoutX="27.0" layoutY="107.0" onAction="#handlePizza" text="Pizza" AnchorPane.topAnchor="110.0" />
<Label fx:id="title" layoutX="118.0" layoutY="14.0" minHeight="16" minWidth="69" text="Cash Register" textAlignment="CENTER" AnchorPane.rightAnchor="118.0">
<font>
<Font name="System Bold" size="14.0" />
</font></Label>
<Label layoutX="39.0" layoutY="40.0" text="Please select the items you would like to buy" />
<Button fx:id="handlePie" layoutX="27.0" layoutY="169.0" mnemonicParsing="false" onAction="#handlePie" text="Blueberry Pie" AnchorPane.topAnchor="180.0" />
<Button fx:id="handleWing" layoutX="27.0" layoutY="138.0" mnemonicParsing="false" onAction="#handleWings" text="Chicken Wings" AnchorPane.topAnchor="145.0" />
<Button fx:id="handleNoodle" layoutX="27.0" layoutY="75.0" mnemonicParsing="false" onAction="#handleNoodles" text="Noodles" AnchorPane.topAnchor="75.0" />
<Button fx:id="handleIce" layoutX="28.0" layoutY="199.0" mnemonicParsing="false" onAction="#handleIce" text="Ice Cream" AnchorPane.topAnchor="215.0" />
<Label fx:id="lblLine" layoutX="21.0" layoutY="308.0" text="_______________________________________________________" AnchorPane.bottomAnchor="95.0" />
<Label layoutX="57.0" layoutY="210.0" text="Subtotal:" underline="true" AnchorPane.bottomAnchor="121.0" AnchorPane.leftAnchor="57.0">
<font>
<Font size="12.5" />
</font>
</Label>
<TextField fx:id="inputPayment" layoutX="143.0" layoutY="282.0" promptText="Enter Your Payment Here" AnchorPane.bottomAnchor="43.0" />
<Label fx:id="lblubtotal" layoutX="113.0" layoutY="210.0" text="$0.00" AnchorPane.bottomAnchor="121.0" AnchorPane.leftAnchor="113.0">
<font>
<Font size="12.5" />
</font>
</Label>
<Label fx:id="lblTax" layoutX="48.0" layoutY="303.0" text="+ 7.00% Sales Tax" AnchorPane.bottomAnchor="98.0">
<font>
<Font size="12.5" />
</font>
</Label>
<Label fx:id="lblTotalText" layoutX="14.0" layoutY="272.0" text="Total:" underline="true" AnchorPane.bottomAnchor="60.0">
<font>
<Font size="14.0" />
</font>
</Label>
<Label fx:id="lblTotal" layoutX="57.0" layoutY="272.0" text="$0.00" AnchorPane.bottomAnchor="60.0">
<font>
<Font size="14.0" />
</font>
</Label>
<Button fx:id="handleDone" layoutX="177.0" layoutY="272.0" mnemonicParsing="false" onAction="#handleButton" text="Done Shopping" AnchorPane.bottomAnchor="118.0">
<effect>
<DropShadow blurType="GAUSSIAN" spread="0.1" />
</effect>
</Button>
<Label fx:id="lblChangeText" layoutX="38.0" layoutY="295.0" text="Your Change:" AnchorPane.bottomAnchor="36.0">
<font>
<Font size="13.0" />
</font>
</Label>
<Label fx:id="lblChange" layoutX="127.0" layoutY="296.0" text="$0.00" AnchorPane.bottomAnchor="36.0" />
<TextField fx:id="inputNoodles" layoutX="143.0" layoutY="75.0" onAction="#inputNoodles" promptText="# of Bowls" />
<TextField fx:id="inputPizza" layoutX="143.0" layoutY="110.0" onAction="#inputPizza" promptText="# of Boxes" />
<TextField fx:id="inputWings" layoutX="143.0" layoutY="145.0" onAction="#inputWings" promptText="# of Plates" />
<TextField fx:id="inputPies" layoutX="143.0" layoutY="180.0" onAction="#inputPies" promptText="# of Pies" />
<TextField fx:id="inputIce" layoutX="143.0" layoutY="215.0" promptText="# of Containers" />
</children>
</AnchorPane>
您实际上可以将需要的代码放在初始化方法中,该方法仅在 GUI 完全设置后运行。这意味着这将像构造函数一样工作,但不会抛出 nullPointerError,因为在这部分代码运行时 GUI 已经存在。
例如,您可以在 RegisterGUIController 中执行此操作:
@Override
public void initialize(URL url, ResourceBundle rb) {
lblTax.setVisible(false);
lblLine.setVisible(false);
lblTotalText.setVisible(false);
lblTotal.setVisible(false);
lblChangeText.setVisible(false);
lblChange.setVisible(false);
inputPayment.setVisible(false);
}
祝你项目顺利!
对于我的一个 Java 项目,我正在使用 JavaFX 来制作收银机。 要看GUI的图片,请 click here.
如 GUI 所示,程序将要求用户输入以查找用户购买的商品数量、用户支付的金额,并将在标签中显示必要的更改。但是,我想在某些时候隐藏此 UI 的某些元素。目前,我知道如何使用这些命令隐藏标签和按钮:
label.setVisible(false);
button.setVisible(false);
然而,我在这里需要的是让标签下面的所有元素说 + 7.00% Sales Tax
在我 运行 GUIController [=34= 开始时不可见] 这样我就不必单击任何其他按钮来进行这些更改。那么,你们有什么建议吗?感谢您的帮助!
CashRegister(包含主要 class)
package cashregister;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author Sammy
*/
public class CashRegister extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("RegisterGUI.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
注册器GUI控制器
(我省略了所有 "actual" 代码,这样我看起来就不会在我的项目中作弊)
package cashregister;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
/**
*
* @author Sammy
*/
public class RegisterGUIController implements Initializable {
@FXML
private Label title, lblSubtotal, lblTax, lblLine, lblTotalText, lblTotal,lblChangeText, lblChange;
@FXML
private Button handleNoodle, handlePizza, handleWing, handlePie, handleIce;
@FXML
private TextField inputNoodle, inputPizza, inputWings, inputPies, inputIce, inputPayment;
@FXML
private void handleNoodle(ActionEvent event) {
}
@FXML
private void handlePizza(ActionEvent event) {
}
@FXML
private void handleWing(ActionEvent event) {
}
@FXML
private void handlePie(ActionEvent event) {
}
@FXML
private void handleIce(ActionEvent event) {
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
}
注册GUI
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="415.0" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="cashregister.RegisterGUIController">
<children>
<Button fx:id="handlePizza" layoutX="27.0" layoutY="107.0" onAction="#handlePizza" text="Pizza" AnchorPane.topAnchor="110.0" />
<Label fx:id="title" layoutX="118.0" layoutY="14.0" minHeight="16" minWidth="69" text="Cash Register" textAlignment="CENTER" AnchorPane.rightAnchor="118.0">
<font>
<Font name="System Bold" size="14.0" />
</font></Label>
<Label layoutX="39.0" layoutY="40.0" text="Please select the items you would like to buy" />
<Button fx:id="handlePie" layoutX="27.0" layoutY="169.0" mnemonicParsing="false" onAction="#handlePie" text="Blueberry Pie" AnchorPane.topAnchor="180.0" />
<Button fx:id="handleWing" layoutX="27.0" layoutY="138.0" mnemonicParsing="false" onAction="#handleWings" text="Chicken Wings" AnchorPane.topAnchor="145.0" />
<Button fx:id="handleNoodle" layoutX="27.0" layoutY="75.0" mnemonicParsing="false" onAction="#handleNoodles" text="Noodles" AnchorPane.topAnchor="75.0" />
<Button fx:id="handleIce" layoutX="28.0" layoutY="199.0" mnemonicParsing="false" onAction="#handleIce" text="Ice Cream" AnchorPane.topAnchor="215.0" />
<Label fx:id="lblLine" layoutX="21.0" layoutY="308.0" text="_______________________________________________________" AnchorPane.bottomAnchor="95.0" />
<Label layoutX="57.0" layoutY="210.0" text="Subtotal:" underline="true" AnchorPane.bottomAnchor="121.0" AnchorPane.leftAnchor="57.0">
<font>
<Font size="12.5" />
</font>
</Label>
<TextField fx:id="inputPayment" layoutX="143.0" layoutY="282.0" promptText="Enter Your Payment Here" AnchorPane.bottomAnchor="43.0" />
<Label fx:id="lblubtotal" layoutX="113.0" layoutY="210.0" text="$0.00" AnchorPane.bottomAnchor="121.0" AnchorPane.leftAnchor="113.0">
<font>
<Font size="12.5" />
</font>
</Label>
<Label fx:id="lblTax" layoutX="48.0" layoutY="303.0" text="+ 7.00% Sales Tax" AnchorPane.bottomAnchor="98.0">
<font>
<Font size="12.5" />
</font>
</Label>
<Label fx:id="lblTotalText" layoutX="14.0" layoutY="272.0" text="Total:" underline="true" AnchorPane.bottomAnchor="60.0">
<font>
<Font size="14.0" />
</font>
</Label>
<Label fx:id="lblTotal" layoutX="57.0" layoutY="272.0" text="$0.00" AnchorPane.bottomAnchor="60.0">
<font>
<Font size="14.0" />
</font>
</Label>
<Button fx:id="handleDone" layoutX="177.0" layoutY="272.0" mnemonicParsing="false" onAction="#handleButton" text="Done Shopping" AnchorPane.bottomAnchor="118.0">
<effect>
<DropShadow blurType="GAUSSIAN" spread="0.1" />
</effect>
</Button>
<Label fx:id="lblChangeText" layoutX="38.0" layoutY="295.0" text="Your Change:" AnchorPane.bottomAnchor="36.0">
<font>
<Font size="13.0" />
</font>
</Label>
<Label fx:id="lblChange" layoutX="127.0" layoutY="296.0" text="$0.00" AnchorPane.bottomAnchor="36.0" />
<TextField fx:id="inputNoodles" layoutX="143.0" layoutY="75.0" onAction="#inputNoodles" promptText="# of Bowls" />
<TextField fx:id="inputPizza" layoutX="143.0" layoutY="110.0" onAction="#inputPizza" promptText="# of Boxes" />
<TextField fx:id="inputWings" layoutX="143.0" layoutY="145.0" onAction="#inputWings" promptText="# of Plates" />
<TextField fx:id="inputPies" layoutX="143.0" layoutY="180.0" onAction="#inputPies" promptText="# of Pies" />
<TextField fx:id="inputIce" layoutX="143.0" layoutY="215.0" promptText="# of Containers" />
</children>
</AnchorPane>
您实际上可以将需要的代码放在初始化方法中,该方法仅在 GUI 完全设置后运行。这意味着这将像构造函数一样工作,但不会抛出 nullPointerError,因为在这部分代码运行时 GUI 已经存在。
例如,您可以在 RegisterGUIController 中执行此操作:
@Override
public void initialize(URL url, ResourceBundle rb) {
lblTax.setVisible(false);
lblLine.setVisible(false);
lblTotalText.setVisible(false);
lblTotal.setVisible(false);
lblChangeText.setVisible(false);
lblChange.setVisible(false);
inputPayment.setVisible(false);
}
祝你项目顺利!