Java Programming/Applets/User Interface

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

The main difference between an applet and a regular command-line executed program is that applets allow for extensible Graphical User Interfaces (GUI). Applets are what most people think of when they hear the words "Java programming" because applets are very widely used throughout the Internet.

Since applets provide for the ability to create complex GUI, it is important for developers to know how to create such programs.

Contents

[edit] Applying styles and adding content

In Java applets, graphical portions are initialized and added in two different areas. While objects are initialized in the main class, they are added to the layout of the applet in the init() method. This is done using the syntax of add(<object>). A typical init() method looks something like this:

public void init() 
{
 setFont("Times New Roman", Font.PLAIN, 24);
 setColor(Color.white);
 setBackGroundColor(Color.black);
 setLayout(new GridLayout);
 
 add(label);
 add(button);
}

The different aspects of this method will be covered below.

[edit] Font

Using stylish fonts in your Java applets may be necessary to help keep your Java applets attractive. The setFont() allows for either the font used throughout the applet to be defined or for one element's font to be set at a time.

The syntax for setting a font is <element>.setFont(<fontName>,<fontStyle>,<fontSize>). However, the <element> field is not mandatory.

To make every font in the applet plain, size 24 Times New Roman, the following code should be used:

Font f = new Font("Times New Roman", Font.PLAIN, 24);
setFont(f);

or

setFont("Times New Roman", Font.PLAIN, 24);

As you can see, it is not necessary to initialize the font and set the font through two different lines of code.

However, to make the font of element a plain, size 24 Times New Roman, and element b italicized, size 28 Times New Roman, the following code should be used:

a.setFont("Times New Roman", Font.PLAIN, 24);
b.setFont("Times New Roman", Font.ITALIC, 28);

To set the color of the fonts used in an applet, the setColor(<color>) method is used. This method already includes some predefined colors which can be used by calling, for example, setColor(Color.white). Here are all of the predefined colors:

  • Color.black
  • Color.blue
  • Color.cyan
  • Color.darkGray
  • Color.gray
  • Color.green
  • Color.red
  • Color.white
  • Color.yellow

To create a custom color, the RGB values of the color can be passed in as the color parameter. For example, if red were not a predefined color, one could use setColor(255,0,0) to define red.

Just as font styles, font colors can be applied to separate elements. The syntax follows the same pattern: a.setColor(Color.white).

[edit] Button

Almost every applet not focused only on painting, drawing, or some other media uses a button. There are only a few ways to have contact between the applet and the user, and the use of buttons is one of those ways. Buttons are created the same way as most other Java applet objects: Button b = new Button(<String>). When initializing a button, it is necessary to define what text will appear on that button in the given parameter. For example, a button with the word "Submit" printed on it would be initialized like so:

Button b = new Button("Submit");

Adding the button to the actual layout is done in the init() method, as described above.

public void init() 
{
 add(b);
}

Allowing buttons to carry out tasks or utilize a user's input is a bit more complicated. These functions require an ActionListener, and will be discussed later in this book.

[edit] Label

Labels are areas in applets that contain text which can not be edited by the user. This is usually ideal for descriptions (i.e. "Insert name:"). Labels are initialized and added to applet layouts in the same way as buttons. Also, like buttons, the text inside labels must be identified at initialization. If, however, the label will receive its text as the cause of a later function and should start off blank, no text should be placed between the quotation marks.

Label l = new Label("Name: ");
...
public void init() {
 add(l);
}

[edit] TextField

TextFields are areas in applets that allow users to insert text. The two parameters, which are optional, for TextFields can set predefined text in the field or set the the number of columns allowed in the TextField. Here are a few examples:

TextField t1 = new TextField();                //blank
TextField t2 = new TextField(5);               //blank in 5 columns
TextField t3 = new TextField("Input here");    //predefined text
TextField t4 = new TextField("Input here", 5); //predefined text in 5 columns
...
public void init() {
 add(t1);
 ...
}

[edit] Layout

Layouts are what make applets visible. Without a layout, nothing would display. There are five different types of layouts to choose from - some are very simple while others are complex.

[edit] Flow Layout

The Flow Layout is the default layout for applets and, therefore, does not need to be set. However, for clarity, one can specify the applet layout as a Flow Layout by placing this line of code at the top of the init() method:

setLayout(new FlowLayout());

The added components to the layout that follow will be placed on screen in order of which they are added.

public void init() 
{
 setLayout(new FlowLayout());
 add(l);
 add(t1);
 add(b);
}

Assuming that these variables are defined the same as above, these lines of code will create the layout of an applet that is composed of a label, a text field, and a button. They will all appear on one line if the window permits. By changing the width of window, the Flow Layout will contract and expand the components accordingly.

[edit] Grid Layout

Container a=getContentPane(); a.setLayout(new GridLayout(4,4,2,2) ); JButton b1=new JButton("one"); a.add(b1); //duyant raut...........

[edit] Border Layout

import java.awt.*;
Container b=getcontentPane();
b.setLayout(new BorderLayout() );
Jbutton b2=new JButton("two");
b.add(b2,BorderLayout.EAST);

[edit] Card Layout

[edit] Panel