It was always a big challenge of portability of UI program from one machine to other machine and one OS to other OS. Problem got solved partially with standard like POSIX and X in all kind of flavor of Unix. Problem did not solve of porting from windows to Linux and Linux to Mac. Java AWT solved this issue. All AWT UI program can run in any platform. AWT uses look and feel & components from OS and use them in java awt library. AWT stands for Abstract Window Toolkit. AWT have all basic components which is required for UI building.
Components in AWT :
Please click on image to get clear view of the diagram.
Some of basic components are given below along with its syntax :
1. Label (Static Text ): It is label and it can be used to write some information on UI. It is single line text on screen.
Label my_label = new Label (“This is my label text.”);
We can align text of lebel.
Label my_label = new Label (“This is my label text.”, Label.RIGHT);
Alignment can be left, right and center.
2. TextFiled (user input box ) : Here user can enter limited text data with single line. We can set number of char in textfield by its constuructor as given below
TextField tf = new TextField(10); // 10 number of char will be accepted
3. TextArea : This is also limited text but multiple line of text data input for the user. User can enter multiple line of data.
TextArea myTxtArea = new TextArea();
4. Checkbox : Checkbox provides options to select multiple options.
CheckBox checkbox_name = new Checkbox (“Optional check box 1″, false);
False means that chekcbox will be checked when it is created.
5. Radio Buttons : This is special kind of checkboxes which have same checkbox group name so only one can be selected in all members of checkbox.
CheckboxGroup chkgrp = new CheckboxGroup();
add (new Checkbox (“One”, chkgrp, false);
add (new Checkbox (“Two”, chkgrp, false);
add (new Checkbox (“Three”,chkgrp, false);
add (new Checkbox (“One”, chkgrp, false);
add (new Checkbox (“Two”, chkgrp, false);
add (new Checkbox (“Three”,chkgrp, false);
All will be unchecked but only one can be selected at a time.
6. Buttons : We required buttons to be clicked for all UI application and do many task based on click of button. Button Label ( text on button) can be change by methods.
Button button_name = new Button (“This label will appear on button.”);
7. Frame, Window or panel : These are container and required to put all components in these container.
Frame fr = new Frame(“title of frame”);
here is a good and simple example of all basic components of AWT :
import java.awt.*; // import all awt containers and components import java.awt.event.*; // importing awt events // Frame is top level container //Either frame or window can be used public class SampleExample extends Frame implements ActionListener { private TextField tf; private Button btn; private Button btnClose; public SampleExample () { // Framelayout by default is border layout setLayout(new FlowLayout()); tf = new TextField(50); tf.setEditable(false); add(tf); btn = new Button("Press"); add(btn); btnClose = new Button("Close"); add(btnClose); btn.addActionListener(this); // providing events for this button btnClose.addActionListener(this); // providing events for this button setTitle("AWT Simple Example"); // title of frame setSize(500, 100); // dmension of frame setVisible(true); // show frame } /** The entry main() method */ public static void main(String[] args) { // Invoke the constructor to setup the GUI, by allocating an instance SampleExample app = new SampleExample(); } public void actionPerformed(ActionEvent evt) { if ( evt.getActionCommand() .equals("Press")) tf.setText("button is pressed on "+new java.util.Date().toString()); else System.exit(0); } }
Output of the program :
0 comments:
Post a Comment