1 12 13 package demo; 14 15 import javax.swing.*; 16 import javax.swing.event.*; 17 import java.awt.*; 18 import java.awt.event.*; 19 import java.net.*; 20 import java.io.*; 21 22 26 27 public class SwingBrowser extends JFrame implements HyperlinkListener, 28 ActionListener { 29 public static void main(String [] args) { 30 if (args.length == 0) 31 new SwingBrowser(); 32 else 33 new SwingBrowser(args[0]); 34 } 35 36 private JButton homeButton; 37 private JTextField urlField; 38 private JEditorPane htmlPane; 39 private String initialURL; 40 41 public SwingBrowser() { this(""); } 42 43 public SwingBrowser(String initialURL) { 44 super("Simple Swing Browser"); 45 this.initialURL = initialURL; 46 47 JPanel topPanel = new JPanel(); 48 topPanel.setLayout(new BorderLayout()); 49 JPanel buttonPanel = new JPanel(); 50 buttonPanel.setLayout(new FlowLayout()); 51 topPanel.add(buttonPanel, BorderLayout.WEST); 52 53 homeButton = new JButton(); 54 homeButton.setIcon(new ImageIcon(getClass().getResource("/demo/Homepage.png"))); 55 homeButton.addActionListener(this); 56 urlField = new JTextField(100); 57 urlField.setText(initialURL); 58 topPanel.add(urlField, BorderLayout.CENTER); 59 60 urlField.addKeyListener(new KeyListener() { 61 public void keyPressed(KeyEvent k) { 62 if (k.getKeyCode() == KeyEvent.VK_ENTER) { 63 String url = urlField.getText(); 64 try { 65 htmlPane.setPage(new URL(url)); 66 urlField.setText(url); 67 } catch(IOException ioe) { 68 warnUser("Can't follow link to " + url + ": " + ioe); 69 } 70 } 71 } 72 public void keyReleased(KeyEvent k) {} 73 public void keyTyped(KeyEvent k) { 74 } 75 }); 76 77 buttonPanel.add(homeButton); 78 79 getContentPane().add(topPanel, BorderLayout.NORTH); 80 81 String sample = "<html><body><h2>Test HTML</h2><p>This is a simple browser test. Type in a URL at the top to go to a new location, or hit the home button to go back to the URL you supplied.</p></body></html>"; 82 83 try { 84 if (initialURL.equals("")) 85 htmlPane = new JEditorPane("text/html", sample); 86 else 87 htmlPane = new JEditorPane(initialURL); 88 htmlPane.setEditable(false); 89 htmlPane.addHyperlinkListener(this); 90 JScrollPane scrollPane = new JScrollPane(htmlPane); 91 getContentPane().add(scrollPane, BorderLayout.CENTER); 92 } catch(IOException ioe) { 93 warnUser("Can't build HTML pane for " + initialURL 94 + ": " + ioe); 95 } 96 97 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 98 int width = screenSize.width * 8 / 10; 99 int height = screenSize.height * 8 / 10; 100 setBounds(width/8, height/8, width, height); 101 setVisible(true); 102 } 103 104 public void actionPerformed(ActionEvent event) { 105 String url = initialURL; 106 if (url.equals("")) { 107 warnUser("You didn't supply an initial home URL!"); 108 return; 109 } 110 try { 111 htmlPane.setPage(new URL(url)); 112 urlField.setText(url); 113 } catch(IOException ioe) { 114 warnUser("Can't follow link to " + url + ": " + ioe); 115 } 116 } 117 118 public void hyperlinkUpdate(HyperlinkEvent event) { 119 if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { 120 try { 121 htmlPane.setPage(event.getURL()); 122 urlField.setText(event.getURL().toExternalForm()); 123 } catch(IOException ioe) { 124 warnUser("Can't follow link to " 125 + event.getURL().toExternalForm() + ": " + ioe); 126 } 127 } 128 } 129 130 private void warnUser(String message) { 131 JOptionPane.showMessageDialog(this, message, "Error", 132 JOptionPane.ERROR_MESSAGE); 133 } 134 } 135 136 | Popular Tags |