Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@craigmarvelley
Created March 5, 2014 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigmarvelley/9377139 to your computer and use it in GitHub Desktop.
Save craigmarvelley/9377139 to your computer and use it in GitHub Desktop.
Internet.java
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import javax.swing.event.*;
public class Internet extends JFrame
{
final JDesktopPane theDesktop;
JPanel browse;
JMenuBar bar;
JMenu openMenu, travelMenu, helpMenu;
JMenuItem browserFrame, favouritesFrame, filtersFrame,
aboutFrame, indexFrame, exitDialog, backItem, forwardItem,
stopItem, reloadItem;
JToolBar toolBar;
JOptionPane exit;
JButton back;
private JTextField enter;
private JEditorPane contents;
public Internet()
{
super( "An Internet Browser" );
theDesktop = new JDesktopPane();
getContentPane().add( theDesktop );
try
{
UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );
}
catch (Exception e) { }
bar = new JMenuBar();
openMenu = new JMenu( "Open" );
browserFrame = new JMenuItem( "New Web Browser" );
favouritesFrame = new JMenuItem( "Favourites" );
filtersFrame = new JMenuItem( "Filters" );
exitDialog = new JMenuItem( "Exit..." );
openMenu.add( browserFrame );
openMenu.add( favouritesFrame );
openMenu.add( filtersFrame );
openMenu.addSeparator();
openMenu.add( exitDialog );
browserFrame.addActionListener(new ActionListener() {
public void actionPerformed( ActionEvent e ){
JInternalFrame frame =
new JInternalFrame( "Browser", true, true, true, true );
Container c = frame.getContentPane();
JPanel b = browser();
c.add( b );
frame.setSize( 500, 400 );
frame.setOpaque( true );
theDesktop.add( frame );
}
}
);
//favouritesFrame.addActionListener(this);
//filtersFrame.addActionListener(this);
exitDialog.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e )
{
exit = new JOptionPane();
int result = exit.showConfirmDialog( theDesktop, "Exit the Program?",
"Exit", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null );
if( result == 0 )
System.exit( 0 );
}
}
);
travelMenu = new JMenu( "Go" );
backItem = new JMenuItem( "Back" );
forwardItem = new JMenuItem( "Forward" );
stopItem = new JMenuItem( "Stop" );
reloadItem = new JMenuItem( "Reload" );
travelMenu.add( backItem );
travelMenu.add( forwardItem );
travelMenu.add( stopItem );
travelMenu.add( reloadItem );
helpMenu = new JMenu( "Help" );
aboutFrame = new JMenuItem( "About..." );
indexFrame = new JMenuItem( "Index" );
helpMenu.add( aboutFrame );
helpMenu.add( indexFrame );
//aboutFrame.addActionListener(this);
//indexFrame.addActionListener(this);
bar.add( openMenu);
bar.add( travelMenu );
bar.add( helpMenu);
setJMenuBar( bar );
toolBar = new JToolBar();
back = new JButton( "Back" );
//toolBar.add( back );
enter = new JTextField( "Enter file URL here" );
enter.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
getThePage( e.getActionCommand () );
}
}
);
toolBar.add( enter );
toolBar.add(Box.createRigidArea(new Dimension(0, 24)));
toolBar.setFloatable( false );
getContentPane().add( toolBar, BorderLayout.NORTH );
setSize(800, 600);
show();
}
public static void main( String args[] )
{
Internet app = new Internet();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
public JPanel browser()
{
browse = new JPanel();
browse.setLayout( new BorderLayout() );
contents = new JEditorPane();
contents.setEditable( false );
contents.addHyperlinkListener(
new HyperlinkListener() {
public void hyperlinkUpdate( HyperlinkEvent e )
{
if( e.getEventType() == HyperlinkEvent.EventType.ACTIVATED )
getThePage( e.getURL().toString() );
}
}
);
browse.add( new JScrollPane( contents ), BorderLayout.CENTER );
return browse;
}
public void getThePage( String location )
{
setCursor( Cursor.getPredefinedCursor( Cursor.WAIT_CURSOR) );
try
{
contents.setPage( location );
enter.setText( location );
}
catch ( IOException io )
{
JOptionPane.showMessageDialog( this, "Error retrieving specified URL", "Bad URL", JOptionPane.ERROR_MESSAGE );
}
setCursor( Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment