如何在 javafx 中更改屏幕以检测按钮单击侦听器中的单选按钮选择



为了进一步阐述我的问题,我正在用JavaFX开发一个问题库。在主屏幕上,我希望根据按钮单击中的单选按钮选择提供导航。

例如,如果我选择一个单选按钮选项,然后单击按钮继续,它应该将我定向到我创建的 FXML 屏幕文件。为了进一步解释我想要保留的内容,下面是 GUI 快照。

题库图形用户界面链接

我正在下面粘贴我的代码:

主屏幕FXML文件:

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>

<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane id="AnchorPane" prefHeight="548.0" prefWidth="721.0" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="controller.HomefxmlController">
  <children>
    <GridPane alignment="CENTER" gridLinesVisible="false" layoutX="210.0" layoutY="149.0" prefHeight="171.0" prefWidth="373.0" visible="true">
      <children>
        <RadioButton fx:id="radioBlanks" alignment="CENTER" contentDisplay="CENTER" mnemonicParsing="false" onAction="#onRadioBlankClick" text="Fill in the Blank" GridPane.columnIndex="0" GridPane.halignment="LEFT" GridPane.rowIndex="0" GridPane.valignment="CENTER" />
        <RadioButton fx:id="radioMcq" mnemonicParsing="false" onAction="#onRadioMcqClick" text="MCQ" GridPane.columnIndex="1" GridPane.halignment="LEFT" GridPane.rowIndex="0" />
        <RadioButton fx:id="radioshortNote" alignment="CENTER" contentDisplay="CENTER" mnemonicParsing="false" onAction="#onRadioSNclick" text="ShortNote" GridPane.columnIndex="0" GridPane.halignment="LEFT" GridPane.rowIndex="1" />
        <RadioButton fx:id="radioLongAnswer" mnemonicParsing="false" onAction="#onRadioLNclick" text="LongAnswer" GridPane.columnIndex="1" GridPane.halignment="LEFT" GridPane.rowIndex="1" />
        <RadioButton fx:id="radioScenario" mnemonicParsing="false" onAction="#onRadioScenariocClick" text="Scenario" GridPane.columnIndex="0" GridPane.halignment="LEFT" GridPane.rowIndex="2" />
        <RadioButton fx:id="radioTF" mnemonicParsing="false" onAction="#onRadioTFclick" text="True/False" GridPane.columnIndex="1" GridPane.halignment="LEFT" GridPane.rowIndex="2" />
      </children>
      <columnConstraints>
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
      </columnConstraints>
      <rowConstraints>
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      </rowConstraints>
    </GridPane>
    <Label alignment="CENTER" layoutX="234.0" layoutY="47.0" prefHeight="73.0" prefWidth="211.0" text="Question Bank" textAlignment="CENTER" underline="true" wrapText="false">
      <font>
        <Font name="Chiller" size="35.0" />
      </font>
    </Label>
    <Button fx:id="btnProceed" layoutX="275.0" layoutY="378.0" mnemonicParsing="false" onAction="#onBtntProceed" prefHeight="48.0" prefWidth="129.0" text="Proceed" textFill="#252285">
      <font>
        <Font name="Linux Libertine G Regular" size="20.0" />
      </font>
    </Button>
  </children>
  <stylesheets>
    <URL value="@homefxml.css" />
  </stylesheets>
</AnchorPane>

FXML 控制器文件 :

package controller;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.stage.Stage;
/**
 * FXML Controller class
 *
 * @author Vishal
 */
public class HomefxmlController implements Initializable {
    @FXML
    private RadioButton radioBlanks;
    @FXML
    private RadioButton radioMcq;
    @FXML
    private RadioButton radioshortNote;
    @FXML
    private RadioButton radioLongAnswer;
    @FXML
    private RadioButton radioScenario;
    @FXML
    private RadioButton radioTF;
    @FXML
    private Button btnProceed;
    ToggleGroup group;
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        toggleGroupAssign();
    }
    public void toggleGroupAssign() {
        group = new ToggleGroup();
        radioBlanks.setToggleGroup(group);
        radioMcq.setToggleGroup(group);
        radioshortNote.setToggleGroup(group);
        radioLongAnswer.setToggleGroup(group);
        radioScenario.setToggleGroup(group);
        radioTF.setToggleGroup(group);
    }
    @FXML
    private void onBtntProceed(ActionEvent event) throws IOException {
         // I am selecting just one checkbox at the moment for testing purpose..
         if(radioTF.isSelected()){
             FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/tester.fxml"));     
             loader.load();
         } 
    }
}

问题是根本没有出现任何错误。没有编译时错误,没有运行时,什么都没有!它只是不起作用!

请帮我到底哪里出了问题?

loader.load() 返回一个 Node,你不赋值

看看这个链接在同一场景中加载新的 fxml

相关内容

  • 没有找到相关文章

最新更新