我需要將LookAndFeel接口的副本序列化到文件中。因此,我已經寫了下面的代碼:可序列化的LookAndFeel - ClassCastException
public class SerializableLookAndFeel extend LookAndFeel implement Serializable {
public String getID() {
return "";
}
public String getDescription() {
return "";
}
public String getName() {
return "";
}
public boolean isSupportedLookAndFeel() {
return false;
}
public boolean isNativeLookAndFeel() {
return false;
}
}
在運行時,我得到
異常在線程 「主要」 java.lang.ClassCastException: javax.swing.plaf.metal.MetalLookAndFeel中不能轉換爲 test.SerializableLookAndFeel
我試圖擴大MetalLookAndFeel,但在運行時我得到ClassCastException異常。
我如何設法序列化LookAndFeel文件呢?
我想要做的是從我的電腦中獲取當前的外觀,並使應用程序在每次啓動時加載此設置。所以,我決定把它保存爲一個二進制對象。但是,有一個問題需要你做的建議 - 你不能做像BasicLookAndFeel lookAndFeel = UIManager.getLookAndFeel(); IDE不會讓我去。 – ilja
'UIManager.getLookAndFeel()'返回一個Abstract類的LookAndFeel。所以你的代碼應該工作,你有沒有嘗試過鑄造? 'BasicLookAndFeel lookAndFeel =(BasicLookAndFeel)UIManager.getLookAndFeel();'你在用什麼IDE?你可以發佈我可以運行的代碼嗎?如果沒有看到代碼,我無法得到很多幫助。另外,如果問題仍然存在,請查看調試器並查看getLookAndFeel()返回的結果。 – Ali
public static void main(String [] args)throws Exception JFrame frame = new JFrame(「Test」); frame.setSize(600,400); TextArea textArea = new TextArea(20,10); frame.add(textArea); frame.setVisible(true); BasicLookAndFeel blaf =(BasicLookAndFeel)UIManager.getLookAndFeel(); s(blaf); (BasicLookAndFeel blaf){ 嘗試FileOutputStream fout = new FileOutputStream(「basiclook.dat」); ObjectOutputStream oos = new ObjectOutputStream(fout); oos.writeObject(basicLookAndFeel); oos.close(); (例外e){ e.printStackTrace(); } }' java.io.NotSerializableException:java.lang.Object.I use IDEA – ilja