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