Java Application or Applet

There is a neat way that an application/applet can be written such that it can run as a GUI in a web page or as a GUI application. Create the Test.java code as:
[cc lang="java"]
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;

public class Test extends JFrame {

public Test() {
}

public static void main(String args[])
{
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TestApplet app = new TestApplet();
app.init();
jf.getContentPane().add(“Center”, app);
jf.setSize(600,600);
jf.setVisible(true);
jf.repaint();

}
}
[/cc]
and create the TestApplet.java code as:
[cc lang="java"]
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.Font;

public class TestApplet extends JApplet implements ActionListener, DocumentListener
{
JPanel main_panel = null;

public TestApplet()
{
createTestApplet();

}

public void init()
{
}

private void createTestApplet()
{
// Create the two panels, one for input and the other for gradient
// table output
main_panel = new JPanel(new GridLayout(1,2));
add( main_panel );

}

public void insertUpdate(DocumentEvent e )
{ changedUpdate(e); }

public void removeUpdate(DocumentEvent e )
{ changedUpdate(e); }

public void changedUpdate(DocumentEvent e)
{
}

public void actionPerformed(ActionEvent e)
{

}
}
[/cc]
And then to compile it, just do:
[cc lang="bash"]
$ javac Test.java
[/cc]

And now to run it

You can either run it from the command line:
[cc lang="bash"]
$ java Test
[/cc]
or create a web page:
[cc lang="html"]





[/cc]

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>