繁体   English   中英

为什么我的舞台无法点击

[英]Why is my stage not clickable

这是我的第一个应用程序,我不知道为什么菜单和菜单栏一起无法单击。 我使用SceneBuilder创建fxml,并在控制器的initialize方法中添加了树项。

MainWindowController.java

    package gui;


import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.MenuBar;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.net.URL;
import java.util.ResourceBundle;

public class MainWindowController extends AbstractController implements Initializable {

    private Stage stage = null;

    @FXML
    private AnchorPane mainContainer;

    @FXML
    private VBox vBoxContainer;

    @FXML
    private MenuBar menuBarTop;

    @FXML
    private HBox hBoxContainer;

    @FXML
    private StackPane navigationSection;

    @FXML
    private TreeView<String> treeView;



    final private TreeItem<String> rootIssues = new TreeItem<String>("IssueTracker");
    final private TreeItem<String> issuesTable = new TreeItem<String>("IssuesTable");
    final private TreeItem<String> stickers = new TreeItem<String>("Stickers");

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        rootIssues.setExpanded(true);
        rootIssues.getChildren().addAll(issuesTable, stickers);
        treeView.setRoot(rootIssues);

    }

    public void setStage(Stage stage)   {
        this.stage = stage;
        stage.setResizable(true);
        stage.setTitle("SoloStats - Welcome");
    }

    public void closeStage()    {
        if (this.stage != null) {
            this.stage.close();
        }
    }
}

MainWindow.fxml

    <?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TreeView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>

<AnchorPane fx:id="mainContainer" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gui.MainWindowController">
   <children>
      <VBox fx:id="vBoxContainer" layoutX="530.0" layoutY="230.0" prefHeight="25.0" prefWidth="1200.0" AnchorPane.bottomAnchor="572.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <children>
            <MenuBar fx:id="menuBarTop" prefHeight="25.0">
              <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>
         </children>
      </VBox>
      <HBox fx:id="hBoxContainer" layoutX="384.0" layoutY="238.0" prefHeight="600.0" prefWidth="1200.0" style="-fx-background-color: rgb(247, 247, 247);" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="25.0">
         <children>
            <StackPane fx:id="navigationSection" prefHeight="150.0" prefWidth="300.0" style="-fx-background-color: #222;">
               <children>
                  <TreeView fx:id="treeView" fixedCellSize="24.0" prefHeight="200.0" prefWidth="200.0" />
               </children>
            </StackPane>
         </children>
      </HBox>
   </children>
</AnchorPane>

与我的其他阶段唯一不同的是TreeView。 如果我在主要班级做到这一点,那就可以了。 我认为问题出在initialize方法和构建treeView的方法之内,但我不知道它可能是什么。

在尝试了Gash的建议并从Main运行我的MainWindow.fxml之后,MainWindowController和MainWindow.fxml正常运行。 现在,介于工作示例和我的版本之间的唯一另一件事是我开始阶段的方式,并且在应用程序的初始阶段成功登录后,我正在执行此操作。

主屏幕控制器

    package gui;

import connectivity.DataBaseHandler;
import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class HomeScreenController extends AbstractController implements Initializable {
    private DataBaseHandler dbHandler = new DataBaseHandler();

    @FXML
    private AnchorPane homeWindow;

    @FXML
    private TextField insertNameField;

    @FXML
    private PasswordField insertPasswordField;

    @FXML
    private TextField insertDepartmentField;

    @FXML
    private Button signInButton;

    @FXML
    private Button signupButton;
    private Main main;

    @FXML
    private Label mainAlertText;
    private Stage stage = null;

    @Override
    public void initialize(URL url, ResourceBundle rb)  {
        signupButton.setOnAction((event)->{
            showPopupWindow();
        });

        signInButton.disableProperty().bind(Bindings.createBooleanBinding( () -> (insertNameField.getText().isEmpty()
                || insertPasswordField.getText().isEmpty() || insertDepartmentField.getText().isEmpty()),
                insertNameField.textProperty(), insertPasswordField.textProperty(), insertDepartmentField.textProperty()));

        signInButton.setOnAction((event -> {
            if  (dbHandler.login(insertNameField.getText(), insertDepartmentField.getText(), insertPasswordField.getText()))    {
                mainAlertText.setTextFill(Color.GREEN);
                mainAlertText.setText("Login Successfull");
                closeStage();
                showMainWidow();
            } else  {
                mainAlertText.setTextFill(Color.RED);
                mainAlertText.setText("One or more of the values are not correct");
            }
        }));
    }

    private void showPopupWindow() {

        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("SignUpPopUp.fxml"));
        // initializing the controller
        SignUpPopUpController popupController = new SignUpPopUpController();
        loader.setController(popupController);
        Parent layout;
        try {
            layout = loader.load();
            Scene scene = new Scene(layout);
            // this is the popup stage
            Stage popupStage = new Stage();
            popupStage.setResizable(false);
            // Giving the popup controller access to the popup stage (to allow the controller to close the stage)
            popupController.setStage(popupStage);
            if(this.main != null) {
                popupStage.initOwner(main.getPrimaryStage());
            }
            popupStage.initModality(Modality.APPLICATION_MODAL);
            popupStage.setScene(scene);
            popupStage.showAndWait();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void showMainWidow() {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("MainWindow.fxml"));
        //initializing the controller
        MainWindowController mainWindowController = new MainWindowController();
        Parent layout;
        try {
            layout = loader.load();
            Scene scene = new Scene(layout);
            //the main stage
            Stage mainStage = new Stage();
            mainWindowController.setStage(mainStage);
            if (this.main != null)  {
                mainStage.initOwner(main.getPrimaryStage());
            }
            mainStage.initModality(Modality.NONE);
            mainStage.setScene(scene);
            mainStage.showAndWait();
        } catch (IOException e)   {
            e.printStackTrace();
        }
    }

    public void showHomeScreen() {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("HomeScreen.fxml"));
        loader.setController(this);
        Parent layout;
        try {
            layout = loader.load();
            Scene scene = new Scene(layout);
            //this is the stage
            Stage mainStage = new Stage();
            this.setStage(mainStage);
            mainStage.initModality(Modality.APPLICATION_MODAL);
            mainStage.setScene(scene);
            mainStage.showAndWait();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void setStage(Stage stage)   {
        this.stage = stage;
        this.stage.setResizable(false);
        this.stage.setTitle("SoloStats");
    }

    public Stage getStage() {
        return this.stage;
    }

    public void closeStage()    {
        this.stage.close();
    }
}

我应该早点提供这个课程。 抱歉

看来您缺少AbstractController的导入,应该会引发编译错误。 我将您的代码粘贴到NetBeans中并进行了更改

public class MainWindowController extends AbstractController implements Initializable {

public class MainWindowController implements Initializable

一切正常。

如果需要AbstractController,则需要添加导入。 例如:

import org.springframework.web.servlet.mvc.AbstractController;

我的工作代码看起来几乎与您的相同。 我看不出有什么区别,但是下面是您的代码副本对我有用。

MainWindow.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TreeView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>

<AnchorPane fx:id="mainContainer" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gui.MainWindowController">
   <children>
      <VBox fx:id="vBoxContainer" layoutX="530.0" layoutY="230.0" prefHeight="25.0" prefWidth="1200.0" AnchorPane.bottomAnchor="572.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <children>
            <MenuBar fx:id="menuBarTop" prefHeight="25.0">
              <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>
         </children>
      </VBox>
      <HBox fx:id="hBoxContainer" layoutX="384.0" layoutY="238.0" prefHeight="600.0" prefWidth="1200.0" style="-fx-background-color: rgb(247, 247, 247);" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="25.0">
         <children>
            <StackPane fx:id="navigationSection" prefHeight="150.0" prefWidth="300.0" style="-fx-background-color: #222;">
               <children>
                  <TreeView fx:id="treeView" fixedCellSize="24.0" prefHeight="200.0" prefWidth="200.0" />
               </children>
            </StackPane>
         </children>
      </HBox>
   </children>
</AnchorPane>

MainWindowController:

package gui;


import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.MenuBar;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.net.URL;
import java.util.ResourceBundle;

public class MainWindowController implements Initializable {

    private Stage stage = null;

    @FXML
    private AnchorPane mainContainer;

    @FXML
    private VBox vBoxContainer;

    @FXML
    private MenuBar menuBarTop;

    @FXML
    private HBox hBoxContainer;

    @FXML
    private StackPane navigationSection;

    @FXML
    private TreeView<String> treeView;



    final private TreeItem<String> rootIssues = new TreeItem<String>("IssueTracker");
    final private TreeItem<String> issuesTable = new TreeItem<String>("IssuesTable");
    final private TreeItem<String> stickers = new TreeItem<String>("Stickers");

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        rootIssues.setExpanded(true);
        rootIssues.getChildren().addAll(issuesTable, stickers);
        treeView.setRoot(rootIssues);

    }

    public void setStage(Stage stage)   {
        this.stage = stage;
        stage.setResizable(true);
        stage.setTitle("SoloStats - Welcome");
    }

    public void closeStage()    {
        if (this.stage != null) {
            this.stage.close();
        }
    }
}

Gui.java: 在此处添加了stage.setTitle,以使窗口标题起作用。

package gui;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Gui extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));
        stage.setTitle("SoloStats - Welcome");
        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

我的closeStage()调用出现了空指针异常。 代替使用this.stage ,尝试使用signInButton设置舞台,然后将其关闭。 我猜您的登录窗口为APPLICATION_MODAL,并且无法正确关闭,这会阻止输入事件进入新窗口。 您可能会发现Stage Docs非常有用。

这是您closeStage的一个片段:

public void closeStage() {
        Stage stage = (Stage) signInButton.getScene().getWindow();
        stage.close();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM