Java FXML:ImageView显示在SceneBuilder中,但不显示在实际应用中
我正试图编写一个简单的Java应用程序,通过拖动门和连接来修改和可视化逻辑电路。我正在使用SceneBuilder将界面组装在一起。现在,我一直在努力让可用的基本逻辑门显示在适当的栏中,并对与之交互做出响应。更准确地说,我试图让一个gate只显示一些控制台输出,以确认GUI逻辑连接正在工作 我遇到的最大问题是,gates的ImageView,可能还有其他一些FXML元素,由于某种原因拒绝在实际编译的应用程序中显示,即使它们在SceneBuilder及其“预览”功能中正常工作和反应 我不得不做一些实验,将它们包装在各种其他FXML元素中,我并不真正理解这些元素,因为显然ImageWiew没有onDragDetected()方法,即使它的文本输入字段在SceneBuilder中可用。在第一张图片上,可以直接从SceneBuilder中清楚地看到预期的“正在工作”应用程序布局。与实际运行的应用程序的第二个进行比较 可能相关代码: Main.javaJava FXML:ImageView显示在SceneBuilder中,但不显示在实际应用中,java,fxml,scenebuilder,Java,Fxml,Scenebuilder,我正试图编写一个简单的Java应用程序,通过拖动门和连接来修改和可视化逻辑电路。我正在使用SceneBuilder将界面组装在一起。现在,我一直在努力让可用的基本逻辑门显示在适当的栏中,并对与之交互做出响应。更准确地说,我试图让一个gate只显示一些控制台输出,以确认GUI逻辑连接正在工作 我遇到的最大问题是,gates的ImageView,可能还有其他一些FXML元素,由于某种原因拒绝在实际编译的应用程序中显示,即使它们在SceneBuilder及其“预览”功能中正常工作和反应 我不得不做一些
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
}
封装门;
导入javafx.fxml.fxml;
导入javafx.fxml.Initializable;
导入javafx.scene.input.MouseEvent;
导入javafx.scene.layout.ancorpane;
导入java.net.URL;
导入java.util.ArrayList;
导入java.util.ResourceBundle;
/**
*用于保存有关当前电路中的门、连接以及输入和输出引脚的所有信息的类
*/
电路控制器实现可初始化的公共类{
@FXML
私人锚烷锚烷和;
//待办事项暂时公开,以后保密
public ArrayList allCircuitElements=新ArrayList();
public ArrayList theCircuitInputPins=new ArrayList();
public ArrayList theCircuitOutputPins=new ArrayList();
ArrayList allCircuitConnections=新建ArrayList();
public ArrayList allCircuitGateInputPins=new ArrayList();
公共ArrayList allCircuitGateOutputPins=新ArrayList();
public ArrayList allCircuitGates=new ArrayList();
私有内置网关类型currentDragGateType;
@凌驾
公共void初始化(URL fxmlFileLocation,ResourceBundle资源){
//在此处初始化逻辑:所有@FXML变量都将被注入
AnchorPane和setOnDragDetected(此::HandleDragDetected和);
}
@FXML
专用无效句柄DetectedAnd(MouseEvent MouseEvent){
System.out.println(“拖动检测到的nand!”);
}
//类的其他内容,与FXML无关
}
RootLayout.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中那样显示滑块?为了让这些门显示出来并正确地进行交互,我需要做哪些不同的设置或调整呢?在进行了一些随机的胡闹之后,我找到了一个解决方案
首先,我查看了
视图->显示示例控制器框架
。在那里,我注意到handleDragDetectedNAND()
方法没有任何修改器,而我的方法有private
,是从某个教程或其他教程中提前复制的。我删除了修改器,应用程序现在可以运行了。如果路过的人愿意解释为什么会这样(我不知道,也没有时间研究,最后期限快到了),这个答案的价值会显著提高。确保src
文件夹中的所有图像。(已测试)
src
文件夹外的图像不会出现
检查场景图是否已正确构建的最佳方法是使用。如果有滚动窗格和VBox,请检查高度值。这还取决于您如何构建应用程序。在构建之后,图像可能不在正确的位置
+ MyProject
+ not_working_dir
+ src
+ com.stackoverflow
+ working_dir