Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.event.*;
- public class ReadFile extends JFrame {
- private JTextField addressBar;
- private JEditorPane display;
- // constructor
- public ReadFile() {
- super("Mike's Browser");
- addressBar = new JTextField("Hey dude, enter a URL!");
- addressBar.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent event) {
- // when enter is hit, an event occurs. This passes the string
- // from the JTextField into the loadCrap method
- loadCrap(event.getActionCommand());
- }
- });
- add(addressBar, BorderLayout.NORTH);
- display = new JEditorPane();
- // stops the user edit the contents of the pane. This would be set to
- // true if for eg, you're making a text editer
- display.setEditable(false);
- // listens to hyperlinks on the page waiting for a click
- display.addHyperlinkListener(new HyperlinkListener() {
- public void hyperlinkUpdate(HyperlinkEvent event) {
- // this method is called whenever an event occurs on the
- // hyperlink. It listens for clicks, not just enter / exit
- // events
- if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
- loadCrap(event.getURL().toString());
- }
- }
- });
- add(new JScrollPane(display), BorderLayout.CENTER);
- setSize(500, 300);
- setVisible(true);
- }
- // load crap to display on the screen
- private void loadCrap(String userText) {
- try {
- // takes URL as a string and displays it on the screen
- display.setPage(userText);
- // keep url in addressbar
- addressBar.setText(userText);
- } catch (Exception e) {
- System.out.println("Well, that's crap! Check your typing, moron!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement