JavaFX - 控制器看不到按钮或事件

时间:2017-01-11 18:11:43

标签: javafx

为什么当我点击“新建”按钮时,文字没有改变?

主要课程:

public class MainApp extends Application {

@Override
public void start(Stage primaryStage) throws IOException {
    BorderPane rootLayout = FXMLLoader.load(getClass().getResource("view/RootLayout.fxml"));

    AnchorPane code = FXMLLoader.load(getClass().getResource("view/DeskLayout.fxml"));
    rootLayout.setCenter(code);

    GridPane tool = FXMLLoader.load(getClass().getResource("view/ToolLayout.fxml"));
    rootLayout.setBottom(tool);

    primaryStage.setTitle("MyLittleIDE");
    primaryStage.setScene(new Scene(rootLayout));
    primaryStage.show();
}

public static void main(String[] args) {
    launch(args);
}

ToolLayoutController:

public class ToolLayoutController {
@FXML
private Button newButton;

@FXML
protected void handleNewProject(ActionEvent event) {
    newButton.setText("123");
}

ToolLaout.fxml

<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ru.mrchebik.view.ToolLayoutController">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
  <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>
<children>
  <Button fx:id="newButton" mnemonicParsing="false" onAction="#handleNewProject" prefHeight="30.0" prefWidth="150.0" text="New" GridPane.columnIndex="0" />
  <Button fx:id="compile" mnemonicParsing="false" prefHeight="51.0" prefWidth="150.0" text="Compile" GridPane.columnIndex="1" />
  <Button fx:id="run" mnemonicParsing="false" prefHeight="54.0" prefWidth="150.0" text="Run" GridPane.columnIndex="2" />
  <Button fx:id="save" mnemonicParsing="false" prefHeight="59.0" prefWidth="150.0" text="Save" GridPane.columnIndex="3" />
</children>
</GridPane>

RootLaout.fxml:

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">
<top>
  <MenuBar BorderPane.alignment="CENTER">
    <menus>
      <Menu mnemonicParsing="false" text="File">
        <items>
          <MenuItem mnemonicParsing="false" text="Close" />
        </items>
      </Menu>
      <Menu mnemonicParsing="false" text="Edit">
        <items>
          <MenuItem mnemonicParsing="false" text="Delete" />
        </items>
      </Menu>
      <Menu mnemonicParsing="false" text="Help">
        <items>
          <MenuItem mnemonicParsing="false" text="About" />
        </items>
      </Menu>
    </menus>
  </MenuBar>
</top>
</BorderPane>

DeskLayout.fxml

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">
<children>
  <SplitPane dividerPositions="0.8015075376884422" layoutX="196.0" layoutY="87.0" orientation="VERTICAL" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
    <items>
      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
           <children>
              <TextArea fx:id="code" layoutX="39.0" layoutY="-27.0" prefHeight="315.0" prefWidth="598.0" promptText="Code" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
           </children>
        </AnchorPane>
      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
           <children>
              <TextArea fx:id="inOutPut" layoutX="-21.0" layoutY="-73.0" prefHeight="75.0" prefWidth="598.0" promptText="Input/Output" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
           </children>
        </AnchorPane>
    </items>
  </SplitPane>
</children>
</AnchorPane>

我还尝试使用javafx制作示例应用程序,并仅添加toollayout.fxml,控制器并且它可以正常工作。但在这种情况下,我不能这样做。

0 个答案:

没有答案