Professional Documents
Culture Documents
Unit VII AWTControls&Swings
Unit VII AWTControls&Swings
Unit VII AWTControls&Swings
Buttons: Buttons can be created in java using the Buttons class in java.awt package.
Constructors:
Button()
Button(String label)
Methods:
public void setLabel(String label)
public void addActionListener(ActionListener object)
public void setActionCommand(String actionCommand)
public String getActionCommand()
Ex:
import java.awt.Frame;
import java.awt.Button;
import java.awt.FlowLayout;
add(button1);
add(button2);
setSize(300,300);
setVisible(true);
}
Ouput:
Label: A Label object is a component for placing text in a container. A label displays a
single line of read-only text.
Constructors:
Label()
Label(String text)
Label(String text, int alignment)
Constants:
public static final int CENTER
public static final int LEFT
public static final int RIGHT
Methods:
public void setText(String text)
public String getText()
public void setAlignment(int alignment)
public int getAlignment()
Ex:
class Test extends Frame{
public Test(String title){
super(title);
setLayout(new FlowLayout());
add(label1);
add(label2);
add(label3);
setSize(300,100);
setVisible(true);
}
public static void main(String args[]){
new Test("Label Demo");
}
}
Ouput:
TextField: A TextField object is a text component that allows for the editing of a single
line of text.
Constructors:
TextArea: A TextArea object is a text component that allows for the editing/displaying
of multiple lines of text. It can be set to allow editing or to be read-only.
Ex:
import java.awt.Frame;
import java.awt.Button;
import java.awt.Label;
import java.awt.TextField;
import java.awt.TextArea;
import java.awt.FlowLayout;
import java.awt.event.*;
b1.addActionListener(this);
tf1.addActionListener(this);
setLayout(new FlowLayout());
add(label1);
add(tf1);
add(b1);
add(ta);
setSize(300,250);
setVisible(true);
}
Check Box: A check box is a graphical component that can be in either an "on" (true) or
"off" (false) state. Clicking on a check box changes its state from "on" to "off," or from
"off" to "on."
Check Box group: The CheckboxGroup class is used to group together a set of Checkbox
buttons.
Ex:
import java.awt.Frame;
import java.awt.Font;
import java.awt.Label;
import java.awt.Checkbox;
import java.awt.FlowLayout;
import java.awt.event.*;
Checkbox cb1,cb2,cb3;
Label l1;
Font f;
String text="This text is now ";
cb1.addItemListener(this);
cb2.addItemListener(this);
cb3.addItemListener(this);
setLayout(new FlowLayout());
add(l1);
add(cb1);
add(cb2);
add(cb3);
setSize(300,250);
setVisible(true);
}