javac找不到这个类:我假设的StreamedTextArea()是java.awt。*的一部分。如果我需要自己写,那么任何人都可以给我一些建议。网上唯一的参考文献来自〜1999年,这让我觉得它不是Sun课程,而是我必须自己编写的课程。此代码(来自教科书)也使用Frame而不是JFrame。这是不赞成的,现在应该如何编写这个应用程序?请我尝试在网上找到答案,但无济于事。
申请表:
//URLViewer.java
//This is a simple application that provides a window in which you can view the
//contents of a URL.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class URLViewer extends Frame implements WindowListener, ActionListener {
TextField theURL = new TextField();
Button loadbutton = new Button("load");
StreamedTextArea theDisplay = new StreamedTextArea();
public URLViewer() {
super ("URL Viewer");
}
public void init() {
this.add("North", theURL);
this.add("Center", theDisplay);
Panel south = new Panel();
south.add(loadbutton);
this.add("South", south);
theURL.addActionListener(this);
this.addWindowListener(this);
this.setLocation(50, 50);
this.pack();
this.show();
}
public void actionPerformed (ActionEvent evt) {
try {
URL u = new URL (theURL.getText());
InputStream in = u.openStream();
OutputStream out = theDisplay.getOutputStream();
StreamCopier.copy(in, out);
in.close();
out.close();
} catch (MalformedURLException ex) {theDisplay.setText("Invalid URL");}
catch (IOException ex) {theDisplay.setText("Invalid URL");}
}
public void windowClosing (WindowEvent e) {
this.setVisible(false);
this.dispose();
}
public void windowOpened(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public static void main (String[] args) {
URLViewer me = new URLViewer();
me.init();
}
}
和StreamCopier:
import java.io.*;
public class StreamCopier {
public static void main (String[] args) {
try {
copy (null, null);
} catch (IOException e) {System.err.println(e);}
}
public static void copy (InputStream in, OutputStream out) throws IOException {
//do not allow other threads to read from the input or
//write to the output while copying is taking place.
synchronized (in) {
synchronized (out) {
byte [] buffer = new byte [256];
while (true) {
int bytesRead = in.read(buffer);
if (bytesRead == -1) break;
out.write(buffer, 0, bytesRead);
}
}
}
} //end copy()
}