在我的应用程序中,我希望有一个borderpane作为根,在左侧我有三个按钮,根据我按下的按钮,一个新的带有tabpane的fxml应该加载在borderpane的中心,当我按下按钮时,我会得到一个npe,我认为这是由于我没有引用右边的borderpane,但我看不出哪里出错了。
我要做的是,当我按下按钮custMenuButton时,fxml应该加载并设置为BorderPane中的中心。
主类
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Main extends Application {
@FXML
private BorderPane root;
Stage window;
@Override
public void start(Stage primaryStage) throws IOException {
window = primaryStage;
FXMLLoader loader = new FXMLLoader(Main.class.getResource("MainWindowView.fxml"));
this.root = loader.load();
MainWindowController mwc = new MainWindowController();
mwc.setMain(this);
Scene scene = new Scene(root);
window.setTitle("JavaFX");
window.setScene(scene);
window.show();
}
public static void main(String[] args) {
launch(args);
}
public BorderPane getBorderPane() {
return root;
}
}
import java.io.IOException;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.TabPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
public class MainWindowController {
private Main main;
@FXML private BorderPane root;
@FXML private VBox sideBox;
@FXML private Button custMenuButton;
@FXML private Button accMenuButton;
@FXML private Button transMenuButton;
public void setMain(Main main) {
this.main = main;
}
public void handleCustMenuButton() throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("CustomerView.fxml"));
TabPane pane = (TabPane) loader.load();
CustomerViewController cvc = loader.getController();
cvc.setMain(main);
main.getBorderPane().setCenter(pane);
}
}
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
public class CustomerViewController implements Initializable {
Main main;
@FXML private TabPane custTabPane;
@FXML private Tab createTab;
@FXML private Tab listAllTab;
@FXML private Tab deleteTab;
public void setMain(Main main){
this.main = main;
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="firstfx.MainWindowController">
<top>
<MenuBar BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
<left>
<VBox fx:id="sideBox" prefHeight="200.0" prefWidth="150.0" spacing="2.0" BorderPane.alignment="CENTER">
<children>
<Button fx:id="custMenuButton" mnemonicParsing="false" onAction="#handleCustMenuButton" prefHeight="50.0" prefWidth="130.0" text="Kundmeny" />
<Button fx:id="accMenuButton" mnemonicParsing="false" prefHeight="50.0" prefWidth="130.0" text="Kontomeny" />
<Button fx:id="transMenuButton" mnemonicParsing="false" prefHeight="50.0" prefWidth="130.0" text="Transaktioner" />
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</VBox>
</left>
</BorderPane>
客户视图fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.AnchorPane?>
<TabPane fx:id="custTabPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="firstfx.CustomerViewController">
<tabs>
<Tab fx:id="createTab" text="Skapa kund">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
<Tab fx:id="listAllTab" text="Lista alla kunder">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
<Tab fx:id="deleteTab" text="Radera kund">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
</tabs>
</TabPane>
斯塔克特莱斯
Executing /home/garnbutik/NetBeansProjects/FirstFX/dist/run1223809748/FirstFX.jar using platform /home/garnbutik/jdk1.8.0_151/jre/bin/java
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
... 48 more
Caused by: java.lang.NullPointerException
at firstfx.MainWindowController.handleCustMenuButton(MainWindowController.java:31)
... 58 more
BUILD STOPPED (total time: 4 minutes 14 seconds)
在加载MainWindowview.fxml
时创建的控制器上,不能调用setmain(...)
。因此,在MainWindowController
中,当您尝试执行
main.getBorderPane()...
您将得到一个空指针异常。
在start()
方法中,需要以与后面在HandleCustMenuButton(...)
中相同的方式获取控制器,即。
FXMLLoader loader = new FXMLLoader(Main.class.getResource("MainWindowView.fxml"));
this.root = loader.load();
// MainWindowController mwc = new MainWindowController();
MainWindowController mwc = loader.getController();
mwc.setMain(this);
在我的应用程序中,我希望有一个borderpane作为根,在左侧我有三个按钮,根据我按下的按钮,一个新的带有tabpane的fxml应该加载在borderpane的中心,当我按下按钮时,我会得到一个npe,我认为这是由于我没有引用右边的borderpane,但我看不出哪里出错了。 我要做的是,当我按下按钮custMenuButton时,fxml应该加载并设置为BorderPane中的中心。 主类
所以,我一直试图将一个边框放在另一个边框(中心部分)中,但到目前为止没有成功。 我尝试过(在从FXML加载的窗格和父场景中): 使用并设置顶部、底部、左侧和右侧锚 在主边框的顶部和左侧添加(无效且在调整舞台大小时不起作用) 当前布局: 我正在寻找的结果(大致) 这个想法是,即使当我调整窗口大小时,FXML布局也将留在MainBorderPane的中心; LoginController.java l
为了完整起见,下面是toolbarview.fxml:
我目前正在使用JavaFX和JavaFX Scene Builder开发一些GUI。我遇到了一个问题,要为它找到解决方案并不容易(至少对我来说)。我想得到这样的东西: http://i.imgur.com/oN4mIVl.png 关键是单击TreeItem“Sample”后,只有窗口的右侧会重新加载。我也想使用fxml文件来完成它。我知道我可以在另一个fxml中包含一个fxml,但这不是解决方案。
我有2个控制器和3个fxml文件。 1。控制器: MainController.java 在这个控制器中,我使用BorderPane创建舞台和场景。在BorderPane的中心,我想改变布局(GridPane,HBox等) java 在SecondController中,我想用帮助RadioButton更改borderPane MainController的中心。 2。fxml-file: 0.b
问题内容: 我有2个fxml文件: 布局(标题,菜单栏和内容) Anchorpane(应该放在另一个fxml文件的内容中) 我想知道如何从“主”场景将第二个文件加载到内容空间内。在javaFX中工作是一件好事,还是加载一个新场景更好? 我正在尝试做这样的事情,但是不起作用: 谢谢您的帮助。 问题答案: 为什么您的代码不起作用 加载程序会创建一个新的AnchorPane,但是您绝不会将新窗格添加到场