fileinputdemo.java

来自「That is some example about GUI. It is ve」· Java 代码 · 共 67 行

JAVA
67
字号
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*; 
import java.io.*;

class FileInputDemo extends JFrame
    implements  ActionListener {

    private JTextArea textArea;
    private JButton openButton;
    private BufferedReader inFile;
    private JTextField nameField;
    private JLabel nameLabel;

    public static void main (String [] args) {
        FileInputDemo frame = new FileInputDemo();

        frame.setSize(400, 300);
        frame.createGUI();
        frame.show();
    }

    private void createGUI() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container window = getContentPane();
        window.setLayout(new FlowLayout());

        nameLabel = new JLabel("File name:  ");
        window.add(nameLabel);

        nameField = new JTextField(20);
        window.add(nameField);
        nameField.addActionListener(this);

        textArea = new JTextArea("",10,30);
        JScrollPane scrollPane = new JScrollPane(textArea);
        window.add(scrollPane);

        openButton = new JButton("open");
        window.add(openButton);
        openButton.addActionListener(this);
    }

    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == openButton) {
            try {
                inFile = new BufferedReader(
                    new FileReader(nameField.getText()));
                textArea.setText( "");    // clear the input area
                String line;
                while ( ( line = inFile.readLine()) != null) {
                    textArea.append(line+"\n");
                }
                inFile.close();
            }
            catch (IOException e) {
                JOptionPane.showMessageDialog(null, 
                    "File Error: " + e.toString());
            }
        }
    }
}



⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?