Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.72 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.event.*;
  5.  
  6. public class ReadFile extends JFrame {
  7.  
  8.     private JTextField addressBar;
  9.     private JEditorPane display;
  10.  
  11.     // constructor
  12.     public ReadFile() {
  13.         super("Mike's Browser");
  14.  
  15.         addressBar = new JTextField("Hey dude, enter a URL!");
  16.         addressBar.addActionListener(new ActionListener() {
  17.             public void actionPerformed(ActionEvent event) {
  18.                 // when enter is hit, an event occurs. This passes the string
  19.                 // from the JTextField into the loadCrap method
  20.                 loadCrap(event.getActionCommand());
  21.  
  22.             }
  23.         });
  24.         add(addressBar, BorderLayout.NORTH);
  25.  
  26.         display = new JEditorPane();
  27.         // stops the user edit the contents of the pane. This would be set to
  28.         // true if for eg, you're making a text editer
  29.         display.setEditable(false);
  30.         // listens to hyperlinks on the page waiting for a click
  31.         display.addHyperlinkListener(new HyperlinkListener() {
  32.             public void hyperlinkUpdate(HyperlinkEvent event) {
  33.                 // this method is called whenever an event occurs on the
  34.                 // hyperlink. It listens for clicks, not just enter / exit
  35.                 // events
  36.                 if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
  37.                     loadCrap(event.getURL().toString());
  38.                 }
  39.             }
  40.         });
  41.         add(new JScrollPane(display), BorderLayout.CENTER);
  42.         setSize(500, 300);
  43.         setVisible(true);
  44.     }
  45.  
  46.     // load crap to display on the screen
  47.     private void loadCrap(String userText) {
  48.         try {
  49.             // takes URL as a string and displays it on the screen
  50.             display.setPage(userText);
  51.             // keep url in addressbar
  52.             addressBar.setText(userText);
  53.         } catch (Exception e) {
  54.             System.out.println("Well, that's crap! Check your typing, moron!");
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement