// import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import java.net.URL; public class AppletMethodsDemo extends JApplet { public void init() { JPanel panel = new JPanel( new GridLayout(0,2,4,4) ); panel.setBorder( new EmptyBorder(5,5,5,5) ); final JTextField message = new JTextField("Hello World!", 15); message.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent ae) { showStatus(message.getText()); } } ); final JTextField url = new JTextField("http://java.sun.com", 15); final JTextField target = new JTextField("_blank", 15); url.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent ae) { showDoc(url.getText(), target.getText()); } } ); target.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent ae) { showDoc(url.getText(), target.getText()); } } ); panel.add( new JLabel("Show Status", SwingConstants.TRAILING) ); panel.add( message ); panel.add( new JLabel("Show URL", SwingConstants.TRAILING) ); panel.add( url ); panel.add( new JLabel("Target for URL (blank for none)", SwingConstants.TRAILING) ); panel.add( target ); setContentPane(panel); } public void showDoc(String urlString, String target) { try { URL url = new URL(getDocumentBase(),urlString); if (target.trim().length()>0) { getAppletContext().showDocument(url); } else { getAppletContext().showDocument(url, target); } } catch(Exception e) { JOptionPane.showMessageDialog(this, e, "Error!", JOptionPane.ERROR_MESSAGE); } } }