//广外信息学院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();
}
}