JavaFX - 保存/加载 - 形状/布局

Posted

技术标签:

【中文标题】JavaFX - 保存/加载 - 形状/布局【英文标题】:JavaFX - Saving/Loading - Shapes/Layouts 【发布时间】:2017-01-16 14:23:49 【问题描述】:

有什么方法可以将 javafx 中的形状和布局保存到外部文件中。然后可以在以后恢复。我曾尝试关注 YouTube 视频,并且知道如何使用“Text/Int”字段保存/加载文件。但是,我需要一种加载上次保存的布局的方法。

例如说我要保存一个多边形,我创建了以下类:

import javafx.scene.shape.Polygon;

public class saveFile implements java.io.Serializable 
    private static final long serialVersionUID = 1L;
    public Polygon fp; 
    

资源管理器类:

import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Paths;

public class ResourceManager 

public static void save(Serializable data, String fileName) throws Exception  
    try (ObjectOutputStream oos = new ObjectOutputStream(Files.newOutputStream(Paths.get(fileName)))) 
        oos.writeObject(data);
    


public static Object load(String fileName) throws Exception 
    try (ObjectInputStream ois = new ObjectInputStream(Files.newInputStream(Paths.get(fileName)))) 
        return ois.readObject();
    


控制器类:

//Field
private Polygon fp = new Polygon();
@FXML
private AnchorPane container2;

//Methods
public void saveLayout()
    saveFile data = new saveFile();
    data.fp = fp;
    try 
        ResourceManager.save(data, "savefile");
     catch (Exception e) 
        System.out.println("Error");
    


public void loadLayout() throws IOException 
    try 
        saveFile data = (saveFile) ResourceManager.load("save");
        container2.getChildren().add(data.fp);
    
    catch(Exception e)
        System.out.println("Error");
    

我现在知道加载方法是完全错误的,但是我认为无论如何提供它会很好。

编辑:e.printStackTrace();返回:

java.io.NotSerializableException: javafx.scene.shape.Polygon
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at Model.ResourceManager.save(ResourceManager.java:14)
at Controller.NewLayoutController.saveLayout(NewLayoutController.java:191)
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:1771)
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.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
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.control.MenuItem.fire(MenuItem.java:462)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(ContextMenuContent.java:1358)
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.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:352)
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$355(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
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.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)

【问题讨论】:

具体的问题是什么?您的代码现在应该可以正常工作了。 问题是当我点击程序中的“保存”按钮时,它返回“错误”异常。它不会将其保存到外部文件中。 还有什么例外? 异常打印我在异常中声明的“错误”消息。我不明白为什么它不保存它。一整天都让我困惑 好吧,你应该打印e,以及解开这个谜))System.out.println("Error", e);,但你最好使用logback或其他东西 【参考方案1】:

问题是Polygon 类没有实现Serializable 接口,这就是为什么它不能用标准的java 方式序列化。 对于Polygon 类,只保存它的点以便以后检索它们就足够了:

public static void save(ObservableList<Double> points, String fileName) throws Exception  
    try (ObjectOutputStream oos = new ObjectOutputStream(Files.newOutputStream(Paths.get(fileName)))) 
          oos.writeObject(new ArrayList<>(points));
    

然后再读回来:

public static ObservableList<Double> load(String fileName) throws Exception 
    try (ObjectInputStream ois = new ObjectInputStream(Files.newInputStream(Paths.get(fileName)))) 
        List<Double> list = (List<Double>) ois.readObject();
        return FXCollections.observableList(list);
    

然后你可以像这样构造新的多边形:

ObservableList<Double> points = ResourceManager.load("save");
Polygon polygon = new Polygon()
polygon.getPoints().addAll(points);

【讨论】:

再一次,对于更复杂的序列化,你应该看看更复杂的工具(比如 gson)。对于 Polygon,它相当简单,但是对于需要序列化的其他形状,这种方法可能就足够了。 很高兴听到这个消息)

以上是关于JavaFX - 保存/加载 - 形状/布局的主要内容,如果未能解决你的问题,请参考以下文章

如何保存 html 画布,然后在不丢失形状的情况下重新加载它?

如何在JavaFX中将变换应用于形状?

如何扩展 JavaFX Shape 类以实现自定义形状

如何使用 JavaFX 实现放大/缩小?

使用 JavaFX 检查形状的碰撞

JavaFX/CSS 在组合框列表单元格的右侧显示 svg 形状