Skip to content

Instantly share code, notes, and snippets.

@jaronoff97
Last active May 29, 2016 17:06
Show Gist options
  • Save jaronoff97/e588354aabb7a730933c3eb8bbf73886 to your computer and use it in GitHub Desktop.
Save jaronoff97/e588354aabb7a730933c3eb8bbf73886 to your computer and use it in GitHub Desktop.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class FrameLayoutURLReader extends JFrame implements ActionListener {
public JTextField urlTextField = new JTextField("Enter a valid URL");//Text field for a URL
public JTextArea urlTextArea = new JTextArea(400, 300);//Text area for our HTML Code
public JScrollPane urlScrollTextArea = new JScrollPane(urlTextArea);//Make it so our text area can scroll
public JButton go = new JButton("Go");//Make a button
public static void main(String args[]) {
FrameLayoutURLReader application = new FrameLayoutURLReader("Get URL"); // construct a get
application.resize(600, 600); // resize the frame
application.show(); // show the frame
}
public FrameLayoutURLReader( String title) {
super(title);
JPanel centerPanel = new JPanel();//Construct center panel
JPanel northPanel = new JPanel();//Construct northern panel
//START CENTER SETUP -------------
centerPanel.setLayout(new GridLayout(1, 1));//Set layout of center pane to grid layout
centerPanel.add(urlScrollTextArea);//add our scrollable panel to the center
//END CENTER SETUP -------------
//START NORTH SETUP -------------
northPanel.setLayout(new GridLayout(1, 2));//Set layout for one column, two rows
go.addActionListener(this);//add the action listener
northPanel.add(urlTextField);//We want the text field to be on the left so we add it first
northPanel.add(go);//add the go button
//END NORTH SETUP -------------
//Finaly put all the panels onto the Applet panel.
this.setLayout(new BorderLayout());//We want it in a border layout
this.add("Center", centerPanel);//Set centerpanel as the center
this.add("North", northPanel);//Set northpanel as the north
}
public void actionPerformed (ActionEvent e) {
readURL();//This will be called when we press the go button
} // end of Action Performed
public void readURL() {
System.out.println("READURL");
URL destinationURL = null;//Declare a new URL for our program to go
InputStreamReader dis = null;//Declare a DataInputSteram which connects our program to the internet
BufferedReader reader = null;//Make a reader which will read the HTML line by line
try {
destinationURL = new URL("http://" + urlTextField.getText());//Set the URL's location
dis = new InputStreamReader(destinationURL.openStream());// open a stream to our URL and put the data in dis
reader = new BufferedReader(dis);//Construct the reader to read from the input stream
String line = "";//Make a blank string
while (line != null) { //Read until line is null
urlTextArea.append(line + "\n");//Add the line to our text area
line = reader.readLine();//Read the next line
}
} catch (Exception e) {
e.printStackTrace();//If any errors happen, print them out
}
}
} // End of Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment