📄 driver.java~
字号:
package designpatterns.decorator.java;import java.awt.event.*;import java.io.*;import javax.swing.*;import worker.SwingWorker;public class Driver extends JPanel { private JTextField name = new JTextField("Enter a File name"); private JButton readButton; private JLabel bytesReadLabel = new JLabel("Nothing read"); private int bytesRead; public Driver(){ readButton = buildReadButton(); add(name); add(readButton); add(bytesReadLabel); } private JButton buildReadButton() { JButton readButton = new JButton("Go!"); readButton.addActionListener(new ActionListener(){ public void actionPerformed(java.awt.event.ActionEvent ae){ readFile(); } }); return readButton; } protected void readFile() { //SwingWorker necessary so that a time-consuming file-read //can happen in its own thread and allow GUI updates to keep //happening. This can be automated in AspectJ //with the worker-object pattern. SwingWorker worker = new SwingWorker(){ public Object construct(){ actuallyReadFile(); return null; } }; worker.start(); } private void actuallyReadFile() { try { InputStream in = createInputStream(); byte[] b =new byte[1000]; while (in.read(b) != -1) { //do whatever here bytesRead+=1000; } bytesReadLabel.setText("Read " + (bytesRead/1000) + "k"); bytesRead = 0; in.close(); } catch (Exception e) { throw new RuntimeException(e); } } private InputStream createInputStream() throws FileNotFoundException { FileInputStream stream = new FileInputStream(name.getText());// ProgressMonitorInputStream pmStream = new ProgressMonitorInputStream(// this, "This is gonna take a while", stream); BufferedInputStream buff = new BufferedInputStream(stream); return buff; } public static void main(String[] args) { JFrame frame = new JFrame("Decorator Example"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); frame.getContentPane().add(new Driver()); frame.pack(); frame.setVisible(true); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -