2017-06-20 22 views
0

我有幾個類。 他們中的一些java - javafx.event.EventHandler是一個接口,並且JAXB無法處理接口

類遊戲

@XmlRootElement(name = "game") 
public class Game { 
private int scores = 0; 
private Field field; 

... 

@XmlElement 
public int getScores() { 
    return scores; 
} 

public void setScores(int scores) { 
    this.scores = scores; 
} 

@XmlElement 
public Field getField() { 
    return field; 
} 

public void setField(Field field) { 
    this.field = field; 
} 


@XmlElement(name="NewShape") 
public ArrayList<Shapes> getNewShapes() { 
    return newShapes; 
} 

public void setNewShapes(ArrayList<Shapes> newShapes) { 
    this.newShapes = newShapes; 
} 
... 

類形狀

@XmlSeeAlso({RectangleVertic.class, RectangleHoriz.class, Square.class, Angle.class}) 
public abstract class Shapes { 
    private double widthOfSquare, widthBetweenSquares, totalWidth, totalHeight; 
    private int countOfSquaresInWidth, countOfSquaresInHeight; 
    private ArrayList<Rectangle> rectangles= new ArrayList(); 
    private Group group; 

public Shapes(){ 

} 

public Shapes(double widthOfSquare, double widthBetweenSquares){ 
    this.widthOfSquare=widthOfSquare; 
    this.widthBetweenSquares=widthBetweenSquares; 
} 


@XmlElement(type = Group.class) 
public Group getGroup() { 
    return group; 
} 

public void setGroup(Group group){ 
    this.group=group; 
} 

@XmlTransient 
public double getWidthBetweenSquares() { 
    return widthBetweenSquares; 
} 

@XmlTransient 
public int getCountOfSquaresInWidth() { 
    return countOfSquaresInWidth; 
} 

@XmlTransient 
public int getCountOfSquaresInHeight() { 
    return countOfSquaresInHeight; 
} 

@XmlTransient 
public ArrayList<Rectangle> getRectangles() { 
    return rectangles; 
} 

public void setRectangles(ArrayList<Rectangle> rectangles) { 
    this.rectangles = rectangles; 
} 

@XmlTransient 
public double getTotalWidth() { 
    return totalWidth; 
} 

@XmlTransient 
public double getTotalHeight() { 
    return totalHeight; 
} 

@XmlTransient 
public double getWidthOfSquare() { 
    return widthOfSquare; 
} 

... 

public abstract boolean putRectOnField(int startI, int startJ, ArrayList<Object[]> horizLinesOfSquare, ArrayList<Object[]> verticLinesOfSquare); 
public abstract boolean checkPutRectOnField(int startI, int startJ, ArrayList<Object[]> horizLinesOfSquare, ArrayList<Object[]> verticLinesOfSquare); 

public abstract int countOfShape(); 

}

班編組

public class JaxbParser implements Parser { 
@Override 
public Object getObject(File file, Class c) throws JAXBException { 
    JAXBContext context = JAXBContext.newInstance(c); 
    Unmarshaller unmarshaller = context.createUnmarshaller(); 
    Object object = unmarshaller.unmarshal(file); 
    return object; 
} 

@Override 
public void saveObject(File file, Object o) throws JAXBException { 
    JAXBContext context = JAXBContext.newInstance(o.getClass()); 
    Marshaller marshaller = context.createMarshaller(); 
    marshaller.marshal(o,file); 
} 

}

當我嘗試元帥,我得到以下錯誤:

javafx.event.EventDispatcher is an interface, and JAXB can't handle interfaces. this problem is related to the following location: at javafx.event.EventDispatcher at public final javafx.event.EventDispatcher javafx.scene.Node.getEventDispatcher() at javafx.scene.Node at javafx.scene.shape.Shape at javafx.scene.shape.Rectangle at private java.util.ArrayList graphic.Shapes.rectangles at graphic.Shapes at private java.util.ArrayList game.Game.newShapes at game.Game

javafx.scene.input.InputMethodRequests is an interface, and JAXB can't handle interfaces. this problem is related to the following location: at javafx.scene.input.InputMethodRequests at public final javafx.scene.input.InputMethodRequests javafx.scene.Node.getInputMethodRequests() at javafx.scene.Node at javafx.scene.shape.Shape at javafx.scene.shape.Rectangle at private java.util.ArrayList graphic.Shapes.rectangles at graphic.Shapes at private java.util.ArrayList game.Game.newShapes at game.Game

javafx.event.EventHandler is an interface, and JAXB can't handle interfaces. this problem is related to the following location: at javafx.event.EventHandler at public final javafx.event.EventHandler javafx.scene.Node.getOnContextMenuRequested() at javafx.scene.Node at javafx.scene.shape.Shape at javafx.scene.shape.Rectangle at private java.util.ArrayList graphic.Shapes.rectangles at graphic.Shapes at private java.util.ArrayList game.Game.newShapes at game.Game

如果從類形狀編組刪除元素組羣,也沒有任何錯誤。 我錯過了什麼? 謝謝。

回答

0

您的Group類必須是javafx.scene.Group類。

來源:https://docs.oracle.com/javase/8/javafx/api/javafx/scene/Group.html

正如你Oracle文檔上看到,這個類繼承其父類,其中一些使用接口特性的某些電話號碼。 JAXB不管理接口。

不幸的是,如果您希望將XML存儲到完整的組對象中,我看不到任何解決方案。然而,如果你只想編組一些屬性(你需要重新創建你的Group對象),你可以使用一個適配器來保存這個屬性。

來源:https://docs.oracle.com/javase/7/docs/api/javax/xml/bind/annotation/adapters/XmlAdapter.html

如果您選擇適配器解決方案,需要幫助,請告訴我。

+0

非常感謝。如果不是爲了你,我會一直試着去理解爲什麼會發生這種情況。 –

+0

不客氣;) –

相關問題