⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pminputstreamfixed.java

📁 《精通Java Swing程序设计S》这本书所附带的JAVA程序写得很漂亮,都是SWING的实例子,很适合初学者。
💻 JAVA
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

class PMInputStreamFixed implements ActionListener,Runnable
{
    JFrame f = null;
    JLabel label = null;
    JTextArea textarea = null;
    JFileChooser fileChooser = null;

    public PMInputStreamFixed()
    {
        f = new JFrame("ProgressMonitorInputStream Example");
        Container contentPane = f.getContentPane();
        textarea = new JTextArea();
        JScrollPane scrollPane = new JScrollPane(textarea);
        scrollPane.setPreferredSize(new Dimension(350,350));
        JButton b = new JButton("读取文件");
        b.addActionListener(this);
        label = new JLabel(" ",JLabel.CENTER);
        
        fileChooser = new JFileChooser();
        
        contentPane.add(label,BorderLayout.NORTH);
        contentPane.add(scrollPane,BorderLayout.CENTER);
        contentPane.add(b,BorderLayout.SOUTH);
        
        f.pack();
        f.setVisible(true);
        
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
    
    public static void main(String[] args) {
        new PMInputStreamFixed();
    }
    
    Thread athread;
    
    public void actionPerformed(ActionEvent e)
    {
        athread = new Thread(this);
        athread.start();
    }
    
    public void run()
    {
        File file = null;
        int result = fileChooser.showOpenDialog(f);
        
        textarea.setText("");

        if (result == JFileChooser.APPROVE_OPTION)
        {
            file = fileChooser.getSelectedFile();
            label.setText("您选择的文件名称为:"+file.getName());
        }
        else if(result == JFileChooser.CANCEL_OPTION)
        {
            label.setText("您没有选择任何文件");
        }
        
        FileInputStream inputStream;
        
        if(file != null)
        {
            try{
                inputStream = new FileInputStream(file);
            }catch(FileNotFoundException fe){
                label.setText("File Not Found");
                return;
            }
            
            ProgressMonitorInputStream pmInputStream = new 
                ProgressMonitorInputStream(f,      //parant component
                                   "Get File Content.....", //message
                                   inputStream);       //input stream
            
            ProgressMonitor pMonitor = 
                pmInputStream.getProgressMonitor();
            pMonitor.setMillisToDecideToPopup(10);
            pMonitor.setMillisToPopup(0);
            int readbyte;
    
            try{
                while( (readbyte = pmInputStream.read()) != -1)
                {
                    textarea.append(String.valueOf((char)readbyte));

                    try{
                        Thread.sleep(10);
                    }catch(InterruptedException ie){}
                    
                    if(pMonitor.isCanceled())
                    {
                        textarea.append("\n\n读取文件中断!!");
                    }
                }
            }catch(IOException ioe){
                label.setText("读取文件错误");
            }
            finally{
                try{
                    if(pmInputStream != null)
                        pmInputStream.close();
                }catch(IOException ioe2){}
            }
        }
    }
}


⌨️ 快捷键说明

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