当前位置: 代码迷 >> J2SE >> [请问]做书上一例题遇到的有关问题(附Java源码)
  详细解决方案

[请问]做书上一例题遇到的有关问题(附Java源码)

热度:60   发布时间:2016-04-24 12:14:12.0
[请教]做书上一例题遇到的问题(附Java源码)
本人是Java初学者,今天做书上一例题,遇到问题(参见java源码):定义MyWindow类时,类名MyWindow下出现黄色波浪线,单击左侧“灯泡”,提示“The serializable class MyWindow does not declare a static final serialVersionUID field of type long Test.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);    }}
  相关解决方案