First of all create a new Java file called "HelloWorld.java", you can do this using WordPad or any other text editor if you don't have a proper programming complier. If you would like a complier but don't know which one to get then I recommend NetBeans, its free, easy to use and has loads of great features which makes writing code a breeze.
Now for the code, in the new file we created put the following code:
import java.applet.*;
import java.awt.*;
public class HelloWorld extends Applet
{
private Panel helloPanel;
private Label helloLabel;
public void init()
{
// Create a new panel to contain out Label object
helloPanel = new Panel();
helloPanel.setLayout(new BorderLayout());
// Create a new label saying "Hello World" and centre it
helloLabel = new Label("Hello World", Label.CENTER);
// Added the newly created label to the centre of the panel
helloPanel.add(helloLabel, BorderLayout.CENTER);
// Added the new panel to the applet
add(helloPanel);
}
}
And there you have your first applet. Obviously this is a very simplified version of an applet where we have only used a panel and a label but you can also add all sorts of other controls such as buttons, lists, text boxes, drop downs etc. In a future article I will talk about these as well as the different ways you can position your panels (you can have more than panel and position them within each other) and components.
Thats all I am going to write today because its my birthday and I have things to do, so you had better appreciate this tutorial lol.
1 comment:
thanks! I will have to test this later! :D
Web Head SEO
Post a Comment