本人是Java初学者,今天做书上一例题,遇到问题(参见java源码):定义MyWindow类时,类名MyWindow下出现黄色波浪线,单击左侧“灯泡”,提示“The serializable class MyWindow does not declare a static final serialVersionUID field of type long Test.java ”,不知如何修改,特此请教。
另,按“运行”键,程序也能运行,但屏幕上没有变化,不显示新窗口。/*例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();
}
}
另,按“运行”键,程序也能运行,但屏幕上没有变化,不显示新窗口。/*例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();
}
}
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);
}
}
按#3楼和#4楼的方法,作了修改,运行,通过,谢谢两位的帮助。
虽然在源码中定义MyWindow类行中的MyWindow下还有黄色波浪线,但能运行正确,就不管它。
刚才,回头查看了一下书,书中例题在main()的方法中确实没有myWin.setVisible(true)这一行。
楼主反馈:
按楼#6楼的方法,我把类MyWindow的构造方法中的con.setVisible(true)改为this.setVisible(true),运行正确,两次修改的运行效果一样,见下图:
序列化时为了保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性。
有两种生成方式:
一个是默认的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,可避开不兼容性问题。
楼主反馈: 1、其实,当初我是先看#4楼的代码,按4#楼代码调试的,成功! 我发现:#3和#4提供的方法是一样的。
2、最后,感谢#9楼提供的有关“serialVersionUID作用”的知识,虽然,我现在看的有点云里雾里的不是及明白,但我会多看几次,尽管把它搞懂。
楼主反馈:
我承诺我当初没有仔细看#4楼(ioe_gaoyong)修改后的代码,当时,只是粗略看了下main()方法中的代码。刚才,我回头仔细看了“ioe_gaoyong”修改的代码,并上机测试,发现有3处改进:
1、在类MyWindow中增加了一行“static final long serialVersionUID=1L;”,这行语行消除了我遇到的第一个疑问,即MyWindows下有黄色波浪线的问题,增加这一行,黄色波浪线消失。
2、在catch(IOException e)方法体中增加了e.printStackTrac()。
3、在main()方法中增加了两个语句:
(1)myWin.setVisible(true)
(2)myWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) 经过这么一修改,测试,成功。修改后的程序看起来比较完整。新增加的语句的具体作用,值得我今后研究研究。通过这个题目与大家的交流,我收获也不小。 最后,为了表示感激“ioe_gaoyong”的帮助,最后结贴,给你的分最多,嘿嘿....
myWin.setVisible(true)显示窗口
myWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)窗口关闭时退出虚拟机