fork download
  1. Code :
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.util.*;
  5. import java.net.*;
  6. import java.io.*;
  7. import javax.swing.*;
  8. import javax.swing.event.*;
  9.  
  10. public class WebBrowser
  11. {
  12. public static void main(String [] args)
  13. {
  14. JFrame frame = new EditorPaneFrame();
  15. frame.show();
  16. }
  17. }
  18. class EditorPaneFrame extends JFrame
  19. {
  20.  
  21. private JTextField url;
  22. private JCheckBox editable;
  23. private JButton loadButton;
  24. private JButton backButton;
  25. private JEditorPane editorPane;
  26. private Stack urlStack = new Stack();
  27.  
  28.  
  29. public EditorPaneFrame()
  30. {
  31. setTitle("Java Web Browser");
  32. setSize(600,400);
  33. addWindowListener(new WindowAdapter()
  34. {
  35. public void windowClosing(WindowEvent e)
  36. {
  37. System.exit(0);
  38. }
  39. } );
  40.  
  41. // set up text field and load button for typing in URL
  42.  
  43. url = new JTextField(30);
  44.  
  45. loadButton = new JButton("Load");
  46. loadButton.addActionListener(new ActionListener()
  47. {
  48. public void actionPerformed(ActionEvent event)
  49. {
  50. try
  51. {
  52. // remember URL for back button
  53. urlStack.push(url.getText());
  54. editorPane.setPage(url.getText());
  55. }
  56. catch(Exception e)
  57. {
  58. editorPane.setText("Error: " +e);
  59. }
  60. }
  61. });
  62.  
  63. // set up back button and button action
  64.  
  65. backButton = new JButton("Back");
  66. backButton.addActionListener(new ActionListener()
  67. {
  68. public void actionPerformed(ActionEvent event)
  69. {
  70. if(urlStack.size()<=1) return;
  71. try
  72. {
  73. urlStack.pop();
  74. String urlString = (String)urlStack.peek();
  75. url.setText(urlString);
  76. editorPane.setPage(urlString);
  77. }
  78. catch(IOException e)
  79. {
  80. editorPane.setText("Error : " +e);
  81. }
  82. }
  83. });
  84.  
  85. editorPane = new JEditorPane();
  86. editorPane.setEditable(false);
  87. editorPane.addHyperlinkListener(new HyperlinkListener()
  88. {
  89. public void hyperlinkUpdate(HyperlinkEvent event)
  90. {
  91. if(event.getEventType() ==
  92. HyperlinkEvent.EventType.ACTIVATED)
  93. {
  94. try
  95. {
  96. urlStack.push(event.getURL().toString());
  97. url.setText(event.getURL().toString());
  98.  
  99. editorPane.setPage(event.getURL());
  100. }
  101. catch(IOException e)
  102. {
  103. editorPane.setText("Error: " + e);
  104. }
  105. }
  106. }
  107. });
  108.  
  109. editable = new JCheckBox();
  110. editable.addActionListener(new ActionListener()
  111. {
  112. public void actionPerformed(ActionEvent event)
  113. {
  114. editorPane.setEditable(editable.isSelected());
  115. }
  116. });
  117.  
  118. Container contentPane = getContentPane();
  119. contentPane.add(new JScrollPane(editorPane), "Center");
  120.  
  121. JPanel panel = new JPanel();
  122. panel.add(new JLabel("URL"));
  123. panel.add(url);
  124. panel.add(loadButton);
  125. panel.add(backButton);
  126. panel.add(new JLabel("Editable"));
  127. panel.add(editable);
  128.  
  129. contentPane.add(panel,"South");
  130. }
  131.  
  132. }
Runtime error #stdin #stdout 0.02s 4980KB
stdin
Standard input is empty
stdout
Standard output is empty