import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class ReadFiles extends Frame implements ActionListener{
static GridBagLayout grid = new GridBagLayout();
static GridBagConstraints cons = new GridBagConstraints();
Panel fn = new Panel();
static TextField f = new TextField("");
static Button get = new Button("Get");
static TextArea file = new TextArea("");
static TextArea stat = new TextArea("Ready\n");
static URL addr;
public ReadFiles() {
get.addActionListener(this);
stat.setEditable(false);
setLayout(grid);
fn.add(f);
fn.add(get);
fn.setLayout(new GridLayout(2,1));
// fileName
buildConstraints(cons,0,0,1,1,0,1,true);
grid.setConstraints(fn,cons);
add(fn);
// stat
buildConstraints(cons,0,1,1,1,1,1,false);
grid.setConstraints(stat,cons);
add(stat);
// file
buildConstraints(cons,1,0,2,3,0,200,true);
grid.setConstraints(file,cons);
add(file);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
}
public static void main(String args[]) {
System.out.println("Starting ReadFiles...");
ReadFiles mainFrame = new ReadFiles();
mainFrame.setSize(600, 300);
mainFrame.setTitle("ReadFiles");
mainFrame.setVisible(true);
}
public void actionPerformed(ActionEvent ev)
{
stat.setText("");
URLConnection conn = null;
DataInputStream in = null;
StringBuffer buf = new StringBuffer();
String t = new String("http://"+f.getText());
//file.setText(t);
try {addr = new URL(t); }
catch( MalformedURLException x) { stat.setText(stat.getText()+"Incorrect URL\n");}
try
{
stat.setText(stat.getText()+"Connecting...\n");
conn = addr.openConnection();
conn.connect();
stat.setText(stat.getText()+"Connected...\n");
in = new DataInputStream(new BufferedInputStream(conn.getInputStream()));
stat.setText(stat.getText()+"Reading...\n");
while((t=in.readLine()) != null)
{
buf.append(t+"\n");
}
stat.setText(stat.getText()+"Displaying File...\n");
file.setText(buf.toString());
stat.setText(stat.getText()+"Done\n");
}
catch(IOException e) { stat.setText(stat.getText()+"Error Connecting\n"); }
}
public void buildConstraints(GridBagConstraints gbc, int gx,int gy, int gw, int gh, int wx, int wy,boolean f)
{
gbc.gridx=gx;
gbc.gridy=gy;
gbc.gridwidth=gw;
gbc.gridheight=gh;
gbc.weightx=wx;
gbc.weighty=wy;
if(f)
gbc.fill=GridBagConstraints.BOTH;
else
{
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.NORTH;
}
}
}
No comments:
Post a Comment