java - FXML:ImageViews 显示在 SceneBuilder 中但不在实际应用程序中

标签 java fxml scenebuilder

我正在尝试编写一个简单的 Java 应用程序,通过拖动门和连接来修改和可视化逻辑电路。我正在使用 SceneBuilder 将界面组合在一起。现在,我一直致力于让可用的基本逻辑门显示在适当的栏中并响应交互。更准确地说,我试图让一个门只显示一些控制台输出,以确认 GUI 逻辑连接正常工作。

我遇到的最大问题是,门的 ImageViews(可能与其他一些 FXML 元素一起)由于某种原因拒绝在实际编译的应用程序中显示,即使它们在 SceneBuilder 及其中正确工作和 react “预览”功能。

我必须尝试将它们包装在各种其他 FXML 元素中,但我并不真正理解这些元素,因为显然 ImageWiew 没有 onDragDetected() 方法,即使它的文本输入字段在 SceneBuilder 中可用。可以直接从第一张图片上的 SceneBuilder 中清楚地看到预期的正在进行的应用程序布局。与第二个比较,这是实际运行的应用程序。

As seen in SceneBuilder GUI of the running app

可能相关的代码:

Main.java

package main;

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

    public class Main extends Application {

        @Override
        public void start(Stage primaryStage) throws Exception{
            Parent root = FXMLLoader.load(getClass().getResource("RootLayout.fxml"));
            primaryStage.setTitle("Hello World");
            primaryStage.setScene(new Scene(root, 640, 450));
            primaryStage.show();
        }

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

TheCircuitController.java

package Gates;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;

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


/**
 * The class for holding all the information about gates, connections, and in and out pins in the current circuit
 */
public class TheCircuitController implements Initializable{

    @FXML
    private AnchorPane anchorPaneNAND;

    //TODO temporarily public, make private later
    public ArrayList<CircuitElement> allCircuitElements= new ArrayList<CircuitElement>();
    public ArrayList<Pin> theCircuitInputPins = new ArrayList<Pin>();
    public ArrayList<Pin> theCircuitOutputPins = new ArrayList<Pin>();
    ArrayList<Connection> allCircuitConnections = new ArrayList<Connection>();
    public ArrayList<Pin> allCircuitGateInputPins = new ArrayList<Pin>();
    public ArrayList<Pin> allCircuitGateOutputPins = new ArrayList<Pin>();
    public ArrayList<Gate> allCircuitGates = new ArrayList<Gate>();

    private InbuiltGateType currentDragGateType;

    @Override
    public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
        // initialize your logic here: all @FXML variables will have been injected
        anchorPaneNAND.setOnDragDetected(this::handleDragDetectedNAND);
    }

    @FXML
    private void handleDragDetectedNAND(MouseEvent mouseEvent) {
        System.out.println("drag detected nand!");
    }

//other stuff of the class, unrelated to FXML
}

根布局.fxml

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

<?import javafx.geometry.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="450.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Gates.TheCircuitController">
   <children>
      <MenuBar prefHeight="27.0" prefWidth="562.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>
      <SplitPane dividerPositions="0.2413793103448276" prefHeight="402.0" prefWidth="640.0">
         <items>
            <ScrollPane fitToHeight="true" fitToWidth="true" prefHeight="400.0" prefWidth="122.0">
               <content>
                  <VBox prefHeight="400.0" prefWidth="208.0" spacing="10.0">
                     <children>
                        <AnchorPane fx:id="anchorPaneNAND" onDragDetected="#handleDragDetectedNAND">
                           <children>
                              <ImageView>
                                 <image>
                                    <Image url="@../../resources/100px-NAND_ANSI.svg.png" />
                                 </image>
                              </ImageView>
                           </children>
                        </AnchorPane>
                        <ImageView>
                           <image>
                              <Image url="@../../resources/100px-NOT_ANSI.svg.png" />
                           </image>
                        </ImageView>
                        <ImageView>
                           <image>
                              <Image url="@../../resources/100px-AND_ANSI.svg.png" />
                           </image>
                        </ImageView>
                        <ImageView>
                           <image>
                              <Image url="@../../resources/OR_ANSI.svg.png" />
                           </image>
                        </ImageView>
                        <ImageView>
                           <image>
                              <Image url="@../../resources/100px-NOR_ANSI.svg.png" />
                           </image>
                        </ImageView>
                        <ImageView>
                           <image>
                              <Image url="@../../resources/100px-XOR_ANSI.svg.png" />
                           </image>
                        </ImageView>
                        <ImageView>
                           <image>
                              <Image url="@../../resources/100px-XNOR_ANSI.svg.png" />
                           </image>
                        </ImageView>
                     </children>
                     <padding>
                        <Insets left="20.0" right="20.0" />
                     </padding></VBox>
               </content></ScrollPane>
            <ScrollPane prefHeight="400.0" prefWidth="406.0" />
         </items>
      </SplitPane>
   </children>
</VBox>

因此我需要知道:

为什么这些门(或至少一个)没有按预期显示? ScrollPane 是怎么回事,为什么它不像 SceneBuilder 中那样显示其 slider ?我需要以不同的方式设置或调整哪些内容才能使这些门正确显示并交互?

最佳答案

经过一些随机的废话后,我找到了解决方案。

首先,我查看了View->Show Sample Controller Skeleton。在那里,我注意到 handleDragDetectedNAND() 方法没有任何修饰符,而我的方法有 private,是从某些教程或其他教程中早期复制的。我删除了修改器,应用程序现在可以运行了。如果路过的人愿意解释一下为什么会这样(我不知道也没有时间研究,截止日期快到了),这个答案的值(value)将大大提高。

关于java - FXML:ImageViews 显示在 SceneBuilder 中但不在实际应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42491077/

相关文章:

运行 Jar 后 Java Ant 无法完成任务

java - 在 webview 中更改字体

IntelliJ IDEA 中的 JAVA 11 和 JavaFX

java - 如何使用相同的模型对象初始化 JavaFX Controller ?

java - 文本字段的 CSS 编辑到语音气泡

java - 和之间的区别

java - ftp 上的同时连接数

java - 无法将 javafx.scene.control.TextField 设置为字段 'user'

java.lang.NoClassDefFoundError : com/sun/javafx/css/converters/SizeConverter

java - TabView : Have some of the tabs on the left side, 和其中一些在右侧(中间有空格)