package Chap13;import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;public class TxtBrowser extends JFrame implements ActionListener {
JTextField jtf1;
JTextArea jta1;
Scanner scaner; public TxtBrowser() { JButton jb1 = new JButton("View");
jb1.addActionListener(this); Container container = this.getContentPane();
setTitle("文本浏览器");
setSize(600, 500);
JPanel jp1 = new JPanel();
jp1.setLayout(new GridLayout(1, 3, 10, 10));
JLabel jlb1 = new JLabel("Filename:");
jtf1 = new JTextField();
jp1.add(jlb1);
jp1.add(jtf1);
jp1.add(jb1);
container.add(jp1, BorderLayout.SOUTH);
jta1 = new JTextArea();
JScrollPane jsp1 = new JScrollPane(jta1);
container.add(jsp1, BorderLayout.CENTER); } public static void main(String[] args) {
TxtBrowser tbr1 = new TxtBrowser();
tbr1.setVisible(true); } public void actionPerformed(ActionEvent e) { File file = new File(jtf1.getText()); try {
scaner = new Scanner(file);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
System.out.println(jtf1.getText());
} // TODO Auto-generated catch block String str1 = null;
while (scaner.hasNextLine()) {
str1 += scaner.nextLine(); jta1.setText(str1);
scaner.close(); } }
}
在线等!

解决方案 »

  1.   


    package Chap13;import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;public class TxtBrowser extends JFrame implements ActionListener {
    JTextField jtf1;
    JTextArea jta1;
    Scanner scaner; public TxtBrowser() { JButton jb1 = new JButton("View");
    jb1.addActionListener(this); Container container = this.getContentPane();
    setTitle("文本浏览器");
    setSize(600, 500);
    JPanel jp1 = new JPanel();
    jp1.setLayout(new GridLayout(1, 3, 10, 10));
    JLabel jlb1 = new JLabel("Filename:");
    jtf1 = new JTextField();
    jp1.add(jlb1);
    jp1.add(jtf1);
    jp1.add(jb1);
    container.add(jp1, BorderLayout.SOUTH);
    jta1 = new JTextArea();
    JScrollPane jsp1 = new JScrollPane(jta1);
    container.add(jsp1, BorderLayout.CENTER); } public static void main(String[] args) {
    TxtBrowser tbr1 = new TxtBrowser();
    tbr1.setVisible(true);
    } public void actionPerformed(ActionEvent e) { File file = new File(jtf1.getText()); try {
    scaner = new Scanner(file);
    } catch (FileNotFoundException e1) {
    // TODO Auto-generated catch block
    System.out.println(jtf1.getText());
    } // TODO Auto-generated catch block String str1 = null;
    while (scaner.hasNextLine()) {
    str1 += scaner.nextLine(); jta1.setText(str1);
    }
    scaner.close();
    }
    }
      

  2.   

    还是能显示的吧 就像1楼所指出的那样
    只不过在scanner关闭之后继续使用会报IllegalStateExceptionAttempting to perform search operations after a scanner has
    been closed will result in an {@link IllegalStateException}