Read the document on the hard disk and the JAVA program code displayed in the Text field __java

Source: Internet
Author: User

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 ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.