If you want to use some Java source code sometimes as an application and sometimes as an applet, it would be better not to have duplicate copies of the code as is the case with the main method of the GetDateProg class and the init method of the GetDateApplet class. It is usually easy to rewrite the code of the program and applet classes so as to avoid the duplication.
0390: // // GetDateBoth.java 0391: // An applet containing the button to get the date and time. 0392: // Barry Cornelius, 22nd November 1999 0393: import java.awt. BorderLayout; 0394: import java.awt. Container; 0395: import javax.swing. JApplet; 0396: import javax.swing. JButton; 0397: import javax.swing. JFrame; 0398: import javax.swing. JTextField; 0399: import javax.swing. RootPaneContainer; 0400: public class GetDateBoth extends JApplet 0401: { 0402: public static void main(final String[] pArgs) 0403: { 0404: final JFrame tJFrame = new JFrame("GetDateBoth"); 0405: iSetUpContentPane(tJFrame); 0406: tJFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 0407: tJFrame.pack(); 0408: tJFrame.setVisible(true); 0409: } 0410: public void init() 0411: { 0412: iSetUpContentPane(this); 0413: } 0414: private static void iSetUpContentPane(final RootPaneContainer pWindow) 0415: { 0416: final JTextField tJTextField = new JTextField("hello", 35); 0417: final JButton tJButton = new JButton("Get Date"); 0418: final JButtonListener tJButtonListener = 0419: new JButtonListener(tJTextField); 0420: tJButton.addActionListener(tJButtonListener); 0421: final Container tContentPane = pWindow.getContentPane(); 0422: tContentPane.add(tJTextField, BorderLayout.NORTH); 0423: tContentPane.add(tJButton, BorderLayout.SOUTH); 0424: } 0425: }
Here is a link to a WWW page containing HTML instructions to run the above code (as an applet): http://www.dur.ac.uk/barry.cornelius/papers/advanced+/code2/Applets/GetDateBoth/GetDateBoth.html.