📄 example10_17.java
字号:
import java.awt.*;import java.awt.event.*;
import java.io.*;
class MyWin extends Frame implements ActionListener
{ TextArea text=null; Button 读入=null,写出=null;
FileInputStream file_in=null; FileOutputStream file_out=null;
ObjectInputStream object_in=null; //对象输入流。
ObjectOutputStream object_out=null; //对象输出流。
MyWin()
{ setLayout(new FlowLayout()); text=new TextArea(6,10);
读入=new Button("读入对象"); 写出=new Button("写出对象");
读入.addActionListener(this);写出.addActionListener(this);
text.addTextListener(new Boy(text));
setVisible(true); add(text);add(读入);add(写出);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
});
validate();setSize(300,300);setVisible(true);
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==写出)
{ try{ file_out=new FileOutputStream("tom.txt");
object_out=new ObjectOutputStream(file_out);//创建对象输出流。
object_out.writeObject(text); //写对象到文件中。
object_out.close();
}
catch(IOException event){}
}
else if(e.getSource()==读入)
{ try{ file_in=new FileInputStream("tom.txt");
object_in=new ObjectInputStream(file_in); //创建对象输入流。
TextArea temp=(TextArea)object_in.readObject();//从文件中读入对象。
temp.setBackground(Color.pink); this.add(temp);//添加该对象到窗口。
this.pack();this.setSize(600,600);
object_in.close();
}
catch(ClassNotFoundException event)
{ System.out.println("不能读出对象");
}
catch(IOException event)
{ System.out.println("can not read file");
}
}
}
}
class Boy implements TextListener,Serializable
{
TextArea text;
int i=10;
Boy(TextArea text)
{
this.text=text;
}
public void textValueChanged(TextEvent e)
{
i++;
text.setBackground(new Color((i*3)%255,(i*7)%255,(i*17)%255) );
}
}
public class Example10_17
{ public static void main(String args[])
{ MyWin win=new MyWin();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -