当我进行鼠标单击事件时,我想更改 VBox 的背景(聚焦)颜色,而其他(兄弟)应该更改为另一种(未聚焦)颜色。
所以,我在下面这样编码..
首先,我用一些 VBox 制作了一个 ObservableList。
public class ManagerWorldController implements Initializable {
@FXML
private BorderPane borderPane;
@FXML
private AnchorPane anchorPane;
@FXML
private VBox menuVBox1;
@FXML
private VBox menuVBox2;
@FXML
private VBox menuVBox3;
@FXML
private VBox menuVBox4;
@FXML
private VBox menuVBox5;
@FXML
private VBox menuVBox6;
@FXML
private VBox menuVBox7;
@FXML
private VBox menuVBox8;
@FXML
private VBox menuVBox9;
@FXML
private VBox menuVBox10;
@FXML
private VBox menuVBox11;
// ObservableList<VBox> menuVBoxList = FXCollections
// .observableArrayList(menuVBox1, menuVBox2, menuVBox3, menuVBox4, menuVBox5, menuVBox6, menuVBox7, menuVBox8, menuVBox9, menuVBox10, menuVBox11);
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
@FXML
private void handleHyperLinkOnAction(MouseEvent event) {
if (event.getSource().equals(menuVBox1)) {
String viewerPath = "/managerworld/viewer/ConnectionViewer.fxml";
changeScreenOfCenter(viewerPath, menuVBox1);
} else if (event.getSource().equals(menuVBox2)) {
String viewerPath = "/managerworld/viewer/ConnectionViewer.fxml";
changeScreenOfCenter(viewerPath, menuVBox2);
} else if (event.getSource().equals(menuVBox3)) {
String viewerPath = "/managerworld/viewer/ConnectionViewer.fxml";
changeScreenOfCenter(viewerPath, menuVBox3);
} else if (event.getSource().equals(menuVBox4)) {
String viewerPath = "/managerworld/viewer/ConnectionViewer.fxml";
changeScreenOfCenter(viewerPath, menuVBox4);
} else if (event.getSource().equals(menuVBox5)) {
String viewerPath = "/managerworld/viewer/ConnectionViewer.fxml";
changeScreenOfCenter(viewerPath, menuVBox5);
} else if (event.getSource().equals(menuVBox6)) {
String viewerPath = "/managerworld/viewer/ConnectionViewer.fxml";
changeScreenOfCenter(viewerPath, menuVBox6);
} else if (event.getSource().equals(menuVBox7)) {
String viewerPath = "/managerworld/viewer/ConnectionViewer.fxml";
changeScreenOfCenter(viewerPath, menuVBox7);
} else if (event.getSource().equals(menuVBox8)) {
String viewerPath = "/managerworld/viewer/ConnectionViewer.fxml";
changeScreenOfCenter(viewerPath, menuVBox8);
} else if (event.getSource().equals(menuVBox9)) {
String viewerPath = "/managerworld/viewer/ConnectionViewer.fxml";
changeScreenOfCenter(viewerPath, menuVBox9);
} else if (event.getSource().equals(menuVBox10)) {
String viewerPath = "/managerworld/viewer/ConnectionViewer.fxml";
changeScreenOfCenter(viewerPath, menuVBox10);
} else if (event.getSource().equals(menuVBox11)) {
String viewerPath = "/managerworld/viewer/ConnectionViewer.fxml";
changeScreenOfCenter(viewerPath, menuVBox11);
} else {
}
}
private void changeScreenOfCenter(String path, VBox menuVBox) {
VBox getVbox = menuVBox;
try {
anchorPane = FXMLLoader.load(getClass().getResource(path));
} catch (Exception e) {
System.out.println(e.getStackTrace().toString());
System.out.println(e.getMessage());
}
// for (VBox vbox : this.menuVBoxList) {
// System.out.println(vbox.getId());
// if (vbox.getId().equals(getVbox.getId())) {
// menuVBox.setStyle("-fx-background-color: #D3D3D3");
// } else {
// //Change the background color to white on other VBoxes..
// }
// }
menuVBox.setStyle("-fx-background-color: #D3D3D3");
borderPane.setCenter(anchorPane);
}
}
但是,一旦我单击 menuVBox1,就会发生空指针异常。
Executing G:\JamesProjects\ManagerWorld\dist\run1616043779\ManagerWorld.jar using platform C:\Program Files\Java\jdk1.8.0_40\jre/bin/java
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1770)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1653)
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.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$ClickGenerator.postProcess(Scene.java:3471)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3399)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3767)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3486)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2495)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(GlassViewEventHandler.java:385)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$148/1745970415.get(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:927)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/1963387170.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
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:497)
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:497)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1765)
... 31 more
Caused by: java.lang.NullPointerException
at managerworld.controller.ManagerWorldController.changeScreenOfCenter(ManagerWorldController.java:111)
at managerworld.controller.ManagerWorldController.handleHyperLinkOnAction(ManagerWorldController.java:66)
... 41 more
请帮我这个..