Guang Wai Information College level 12 Jerry
Import Java.awt.BorderLayout; Import Java.awt.Font;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import java.io.UnsupportedEncodingException;
Import Javax.swing.JButton;
Import Javax.swing.JFileChooser;
Import Javax.swing.JFrame;
Import Javax.swing.JPanel;
Import Javax.swing.JScrollPane;
Import Javax.swing.JTextArea;
Import Javax.swing.JTextField;
Import Org.omg.CosNaming.NamingContextExtPackage.AddressHelper;
public class exercise17_4{
/**
* @param args
*/
public static void Main (string[] args) {
JFrame jframe=new JFrame ();
Textshowpanel textshowpanel=new Textshowpanel ();
Jframe.add (Textshowpanel);
Jframe.pack ();
Jframe.setlocationrelativeto (NULL);
Jframe.setdefaultcloseoperation (Jframe.exit_on_close);
Jframe.settitle ("Text View");
Jframe.setvisible (TRUE);
Jframe.setsize (400,300);
}
}
Class Textshowpanel extends JPanel implements ActionListener
{
Private JButton jbtchose=new JButton ("chose");
Private JButton jbtview=new JButton ("View");
Private JTextField textfield=new JTextField (20);
Private JTextArea textarea=new JTextArea ();
Private JFileChooser filechooser=new JFileChooser ();
Public Textshowpanel () {
JPanel panel=new JPanel ();
Panel.add (Jbtchose);
Panel.add (TextField);
Panel.add (Jbtview);
Textarea.setfont (New Font ("Serif", font.plain,16));
Textarea.setlinewrap (TRUE);
Textarea.setwrapstyleword (TRUE);
Textarea.seteditable (FALSE);
SetLayout (New BorderLayout (1,1));
JScrollPane jscrollpane=new JScrollPane (TextArea);
Add (Jscrollpane,borderlayout.center);
Add (Panel,borderlayout.south);
Jbtchose.addactionlistener (this);
Jbtview.addactionlistener (this);
}
@Override
public void actionperformed (ActionEvent e) {
if (E.getsource () ==jbtchose) {
int Intreval=filechooser.showopendialog (this);
if (intreval==jfilechooser.approve_option) {
Textfield.settext (Filechooser.getselectedfile (). GetPath ());
}
}
if (E.getsource () ==jbtview) {
String Text=readtext (Textfield.gettext ());
Textarea.settext (text);
}
}
private string ReadText (string path) {
if (path== "" | |). Equals (path)) {
Return "";
}
StringBuffer sb=new StringBuffer ();
File File=new file (path);
InputStreamReader Read=null;
BufferedReader Reader=null;
try {
Read = new InputStreamReader (new FileInputStream (file), "gb2312");
reader = new BufferedReader (read);
String Line;
while (line = Reader.readline ())!= null) {
Sb.append (line);
Sb.append ("\ n");
}
catch (Unsupportedencodingexception e) {
E.printstacktrace ();
catch (FileNotFoundException e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
finally {
if (read!= null) {
try {
Read.close ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
if (reader!= null) {
try {
Reader.close ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
return sb.tostring ();
}
}