import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;import javax.swing.JApplet;
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 ViewRemoteFile extends JApplet{
private JButton jbtView = new JButton("Enter");
private JTextField jtfURL = new JTextField();
private JTextArea jtaFile = new JTextArea();
private JLabel jlblStatus = new JLabel();

public void init(){
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
p1.add(new JLabel("URL:"),BorderLayout.WEST);
p1.add(jtfURL,BorderLayout.CENTER);
p1.add(jbtView,BorderLayout.EAST);

setLayout(new BorderLayout());
add(new JScrollPane(jtaFile),BorderLayout.CENTER);
add(jlblStatus,BorderLayout.SOUTH);

jbtView.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
showFile();
}
});
}

private void showFile() {
BufferedReader in = null;
URL url = null;

try {
url = new URL(jtfURL.getText().trim());

InputStream is = url.openStream();
in = new BufferedReader(new InputStreamReader(is));

String inLine;
while((inLine = in.readLine()) != null){
jtaFile.append(inLine + "\n");
}

jlblStatus.setText("File load successfully");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());

ViewRemoteFile applet = new ViewRemoteFile();
frame.add(applet,BorderLayout.CENTER);

applet.init();
applet.start();

frame.pack();
frame.setVisible(true);
}

}
run  application后只能出来

解决方案 »

  1.   

    可爱的帮忙者,如果你们看不到我的图,我在这描述下,就是一个小窗口,里面什么都没有
    图片我有放在我的java相册里,叫swing目前也就一张
    附上地址:
    http://hi.csdn.net/space-6063491-do-album-picid-645920.html
      

  2.   

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;import javax.swing.JApplet;
    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 ViewRemoteFile extends JApplet{
        private JButton jbtView = new JButton("Enter");
        private JTextField jtfURL = new JTextField(40);
        private JTextArea jtaFile = new JTextArea(40,20);
        private JLabel jlblStatus = new JLabel();    public void init(){
    JPanel p1 = new JPanel();
    p1.setLayout(new BorderLayout());
    p1.add(new JLabel("URL:"),BorderLayout.WEST);
    p1.add(jtfURL,BorderLayout.CENTER);
    p1.add(jbtView,BorderLayout.EAST); setLayout(new BorderLayout());
    add(p1,BorderLayout.NORTH); // +++++++++++
    add(new JScrollPane(jtaFile),BorderLayout.CENTER);
    add(jlblStatus,BorderLayout.SOUTH);
    jbtView.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
        showFile();
    }
        });
        }    private void showFile() {
    BufferedReader in = null;
    URL url = null; try {
        url = new URL(jtfURL.getText().trim());     InputStream is = url.openStream();
        in = new BufferedReader(new InputStreamReader(is));     String inLine;
        while((inLine = in.readLine()) != null){
    jtaFile.append(inLine + "\n");
        }     jlblStatus.setText("File load successfully");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally{
        if(in != null){
    try {
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
        }
    }
        }    public static void main(String[] args) {
    JFrame frame = new JFrame(); ViewRemoteFile applet = new ViewRemoteFile();
    frame.add(applet.getContentPane(),BorderLayout.CENTER); applet.init();
    applet.start();
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
        }}
      

  3.   

    in = new BufferedReader(new InputStreamReader(is,"UTF-8"));//解决中文乱码