0

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

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

题库 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();
         } 
    }

}

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

请帮助我到底哪里出错了?

4

1 回答 1

1

loader.load() 返回一个节点,你没有分配

看看这个链接 Loading new fxml in the same scene

于 2016-03-19T08:46:06.963 回答