代码之家  ›  专栏  ›  技术社区  ›  Matt

操作按钮不工作(SceneBuilder JavaFX)

  •  1
  • Matt  · 技术社区  · 8 年前

    我正在尝试使用场景生成器创建的按钮切换场景。

    这是我的控制器类

    public class Controller implements Initializable {
    
        @FXML Button LoginButton;
    
           @FXML private void handleButtonAction(ActionEvent event) throws IOException {
    
                Parent tableViewParent = FXMLLoader.load(getClass().getResource("../FXML/MainMenu.fxml"));
                Scene tableViewScene = new Scene(tableViewParent);
    
                Stage window = (Stage) ((Node)event.getSource()).getScene().getWindow();
                window.setScene(tableViewScene);
                window.show();
            }
    
    
        @Override
        public void initialize(URL location, ResourceBundle resources) {
    
    
        }
    

    还有FXML

    <VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/9.0.4" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controllers.Controller">
    
        <MenuBar VBox.vgrow="NEVER" />
        <AnchorPane id="login" maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" style="-fx-background-color: #ccf2ff #ccf2ff;" VBox.vgrow="ALWAYS">
             <children>
                <AnchorPane id="login2" layoutX="320.0" prefHeight="371.0" prefWidth="316.0">
                   <children>
                      <TextField alignment="TOP_CENTER" layoutX="75.0" layoutY="95.0" promptText="Login">
                         <effect>
                            <InnerShadow />
                         </effect></TextField>
                      <PasswordField alignment="TOP_CENTER" layoutX="75.0" layoutY="159.0" promptText="Password">
                         <effect>
                            <InnerShadow blurType="TWO_PASS_BOX" />
                         </effect></PasswordField>
                      <Button layoutX="129.0" layoutY="220.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Button" />
    
                   </children>
                </AnchorPane>
             </children>
        </AnchorPane>
          <AnchorPane />
      </children>
    </VBox>
    

    由于某些原因,IntelliJ显示错误无法解析“handleButtonAction”

    onAction="#handleButtonAction"

    2 回复  |  直到 8 年前
        1
  •  2
  •   Pagbo    8 年前

    您可以使用属于它的任何FXML元素检索场景(和舞台)。你只需要做 anyElement.getScene().getWindow()

    public class Controller {
    
       @FXML 
       Button loginButton;
    
       @FXML 
       private void handleButtonAction() {
    
          Parent tableViewParent = FXMLLoader.load(getClass().getResource("../FXML/MainMenu.fxml"));
          Scene tableViewScene = new Scene(tableViewParent);
    
          // Instead of retrieving the stage by the event's source, you can do it by one of your FXML component.
          Stage window = (Stage) loginButton.getScene().getWindow();
          window.setScene(tableViewScene);
          window.show();
       }
    
    
       @FXML
       public void initialize() {
          //initialize the field you want here.
       }
    }
    

    和FXML:

    <VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/9.0.4" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controllers.Controller">
       <!-- Fabian was right, you forgot this children. -->
       <children>
          <MenuBar VBox.vgrow="NEVER" />
             <AnchorPane id="login" maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" style="-fx-background-color: #ccf2ff #ccf2ff;" VBox.vgrow="ALWAYS">
                <children>
                   <AnchorPane id="login2" layoutX="320.0" prefHeight="371.0" prefWidth="316.0">
                      <children>
                         <TextField alignment="TOP_CENTER" layoutX="75.0" layoutY="95.0" promptText="Login">
                            <effect>
                               <InnerShadow />
                            </effect>
                         </TextField>
                         <PasswordField alignment="TOP_CENTER" layoutX="75.0" layoutY="159.0" promptText="Password">
                            <effect>
                               <InnerShadow blurType="TWO_PASS_BOX" />
                            </effect>
                         </PasswordField>
                      <Button fx:id="loginButton" layoutX="129.0" layoutY="220.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Button" />
                   </children>
                </AnchorPane>
             </children>
          </AnchorPane>
          <AnchorPane />
       </children>
    </VBox>
    

    关于代码的一些解释/备注。

  • 对于自JavaFX 8以来的可初始化实现来说是无用的。 initialize 带有FXML注释的方法现在已经足够了(请参阅 note in interface definition )。
  • 将登录按钮的第一个字母改为小写( Java naming convention )
  • Fabian的评论是对的,您忘记了FXML中VBox节点的子元素起始元素。
  • 我没有找到登录按钮 fx:id 在FXML中。你忘了吗?我把它加进去了 Button FXML中的元素。(我不知道这是否是有效的登录按钮…)
  •     2
  •  0
  •   Night Programmer    8 年前

    在控制器中写入以下代码

    @FXML
    protected void handleButtonAction() {
        System.out.println("In handleButtonAction");
    // your logic 
    }
    

    和 FXML文件

    <Button layoutX="129.0" layoutY="220.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Button" />
    

    我希望它能起作用。。。我使用scenebuilder FXML ,如果不工作,请向我发送错误屏幕截图