[请教]做书上一例题遇到的问题(附Java源码)
本人是Java初学者,今天做书上一例题,遇到问题(参见java源码):定义MyWindow类时,类名MyWindow下出现黄色波浪线,单击左侧“灯泡”,提示“The serializable class MyWindow does not declare a static final serialVersionUID field of type longTest.java”,不知如何修改,特此请教。
另,按“运行”键,程序也能运行,但屏幕上没有变化,不显示新窗口。
- Java code
/*例9.3(P179) 说明BufferedWriter类用法的应用程序*/import java.io.*;import java.awt.*;import javax.swing.*;import java.awt.event.*;class MyWindow extends JFrame implements ActionListener{ JTextArea text; JButton button; FileWriter writefile; BufferedWriter out; MyWindow() { super("缓冲式样流的输出"); Container con=this.getContentPane();//获得内容面板 text=new JTextArea(20,30); text.setBackground(Color.cyan); button=new JButton("写文件"); button.addActionListener(this); con.setLayout(new BorderLayout()); con.setSize(40,40); con.setVisible(true); con.add(text,"Center"); con.add(button, "South"); try { writefile=new FileWriter("d:\\file2.txt"); out=new BufferedWriter(writefile); } catch(IOException e) {} } public void actionPerformed(ActionEvent e) { if(e.getSource()==button) //将文本区内容采用缓冲式输出到文件 {try { out.write(text.getText(),0,(text.getText()).length()); out.flush(); text.setText(null); System.exit(0); } catch (IOException exp) { text.setText("文件写出错!\n"); System.exit(-1); } } }}public class Test{ public static void main(String args[]) { MyWindow myWin=new MyWindow(); myWin.pack(); }}[解决办法]
黄色的是警告,可以不用管它
[解决办法]
[解决办法]
myWin.setVisible(true);
[解决办法]
- Java code
import java.io.*;import java.awt.*;import javax.swing.*;import java.awt.event.*;class MyWindow extends JFrame implements ActionListener{ static final long serialVersionUID=1L; JTextArea text; JButton button; FileWriter writefile; BufferedWriter out; MyWindow() { super("缓冲式样流的输出"); Container con=this.getContentPane();//获得内容面板 text=new JTextArea(20,30); text.setBackground(Color.cyan); button=new JButton("写文件"); button.addActionListener(this); con.setLayout(new BorderLayout()); con.setSize(40,40); con.setVisible(true); con.add(text,"Center"); con.add(button, "South"); try { writefile=new FileWriter("d:\\file2.txt"); out=new BufferedWriter(writefile); } catch(IOException e) { e.printStackTrace(); } } public void actionPerformed(ActionEvent e) { if(e.getSource()==button) //将文本区内容采用缓冲式输出到文件 {try { out.write(text.getText(),0,(text.getText()).length()); out.flush(); text.setText(null); System.exit(0); } catch (IOException exp) { text.setText("文件写出错!\n"); System.exit(-1); } } }}public class Test2{ public static void main(String args[]) { MyWindow myWin=new MyWindow(); myWin.pack(); myWin.setVisible(true); myWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}
[解决办法]
[解决办法]
serialVersionUID作用:
序列化时为了保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性。
有两种生成方式:
一个是默认的1L,比如:private static final long serialVersionUID = 1L;
一个是根据类名、接口名、成员方法及属性等来生成一个64位的哈希字段,比如:
private static final long serialVersionUID = xxxxL;
当你一个类实现了Serializable接口,如果没有定义serialVersionUID,Eclipse会提供这个
提示功能告诉你去定义 。在Eclipse中点击类中warning的图标一下,Eclipse就会
自动给定两种生成的方式。如果不想定义它,在Eclipse的设置中也
可以把它关掉的,设置如下:
Window ==> Preferences ==> Java ==> Compiler ==> Error/Warnings ==>
Potential programming problems
将Serializable class without serialVersionUID的warning改成ignore即可。
如果你没有考虑到兼容性问题时,就把它关掉,不过有这个功能是好的,只要任何类别实现了Serializable这个接口的话,如果没有加入serialVersionUID,Eclipse都会给你warning提示,这个serialVersionUID为了让该类别Serializable向后兼容。
如果你的类Serialized存到硬盘上面后,可是后来你却更改了类别的field(增加或减少或改名),当你Deserialize时,就会出现Exception的,这样就会造成不兼容性的问题。
但当serialVersionUID相同时,它就会将不一样的field以type的预设值Deserialize,可避开不兼容性问题。
[解决办法]
仔细看看警告的信息
你是不是把Test这个类写成内部类了? 如果是 那么请写到外面试试,或者直接在MyWindow 这个类里面写main方法。
[解决办法]