Advertisement
Guest User

Untitled

a guest
Jun 16th, 2013
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.util.*;
  4. import java.net.*;
  5. import java.io.*;
  6. import javax.swing.*;
  7. import javax.swing.event.*;
  8. public class WebBrowser
  9. {
  10. public static void main(String [] args)
  11. {
  12. JFrame frame = new EditorPaneFrame();
  13. frame.show();
  14. }
  15. }
  16. class EditorPaneFrame extends JFrame
  17. {
  18. private JTextField url;
  19. private JCheckBox editable;
  20. private JButton Carica;
  21. private JButton Indietro;
  22. private JEditorPane editorPane;
  23. private Stack urlStack = new Stack();
  24. public EditorPaneFrame()
  25. {
  26. setTitle("Java Web Browser");
  27. setSize(600,400);
  28. setLocationRelativeTo(null);
  29. setVisible(true);
  30. // set up text field and load button for typing in URL
  31. String protocollo = new String ("http://");
  32. url = new JTextField(protocollo,30);
  33. Carica = new JButton("Load");
  34. Carica.addActionListener(new ActionListener()
  35. {
  36. public void actionPerformed(ActionEvent event)
  37. {
  38. try
  39. {
  40. // remember URL for back button
  41. urlStack.push(url.getText());
  42. editorPane.setPage(url.getText());
  43. }
  44. catch(Exception e)
  45. {
  46. editorPane.setText("Error: " +e);
  47. }
  48. }
  49. });
  50. // set up back button and button action
  51. Indietro = new JButton("Back");
  52. Indietro.addActionListener(new ActionListener()
  53. {
  54. public void actionPerformed(ActionEvent event)
  55. {
  56. if(urlStack.size()<=1) return;
  57. try
  58. {
  59. urlStack.pop();
  60. String urlString = (String)urlStack.peek();
  61. url.setText(urlString);
  62. editorPane.setPage(urlString);
  63. }
  64. catch(IOException e)
  65. {
  66. editorPane.setText("Error : " +e);
  67. }
  68. }
  69. });
  70. editorPane = new JEditorPane();
  71. editorPane.setEditable(false);
  72. editorPane.addHyperlinkListener(new HyperlinkListener()
  73. {
  74. public void hyperlinkUpdate(HyperlinkEvent event)
  75. {
  76. if(event.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
  77. {
  78. try
  79. {
  80. urlStack.push(event.getURL().toString());
  81. url.setText(event.getURL().toString());
  82. editorPane.setPage(event.getURL());
  83. }
  84. catch(IOException e)
  85. {
  86. editorPane.setText("Error: " + e);
  87. }
  88. }
  89. }
  90. });
  91. editable = new JCheckBox();
  92. editable.addActionListener(new ActionListener()
  93. {
  94. public void actionPerformed(ActionEvent event)
  95. {
  96. editorPane.setEditable(editable.isSelected());
  97. }
  98. });
  99. Container contentPane = getContentPane();
  100. contentPane.add(new JScrollPane(editorPane), "Center");
  101. JPanel panel = new JPanel();
  102. panel.add(Indietro);
  103. panel.add(new JLabel("URL"));
  104. panel.add(url);
  105. panel.add(Carica);
  106. contentPane.add(panel,"North");
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement