Unit VII AWTControls&Swings

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

AWT Controls And 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()

Button button1 = new Button ();


button1.setLabel(“OK”);

Button button2 = new Button(“Cancel”);

Ex:
import java.awt.Frame;
import java.awt.Button;
import java.awt.FlowLayout;

class Test extends Frame{


public Test(String title){
super(title);

Button button1 = new Button ();


button1.setLabel("OK");

Button button2 = new Button("Cancel");


setLayout(new FlowLayout());

add(button1);
add(button2);
setSize(300,300);
setVisible(true);
}

public static void main(String args[]){


new Test(“Button Demo);
}
}

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);

Label label1 = new Label();


label1.setText("Label 1");

Label label2 = new Label("Label 2", Label.RIGHT);

Label label3 = new Label("Label3");

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 & TextArea:

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.*;

class TextComponentsDemo extends Frame implements ActionListener{


TextField tf1;
Button b1;
TextArea ta;

public TextComponentsDemo(String title){


super(title);

Label label1 = new Label("Enter ur name: ");


tf1 = new TextField(20);
b1 = new Button("OK");
ta = new TextArea(10,10);

b1.addActionListener(this);
tf1.addActionListener(this);

setLayout(new FlowLayout());

add(label1);
add(tf1);
add(b1);
add(ta);
setSize(300,250);
setVisible(true);
}

public void actionPerformed(ActionEvent ae){


if(ae.getSource()==b1 || ae.getSource()==tf1)
ta.setText(tf1.getText());
}

public static void main(String args[]){


new TextComponentsDemo ("Text components Demo");
}
}

Check box & CheckBox group:

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.*;

class CheckboxDemo extends Frame implements ItemListener{

Checkbox cb1,cb2,cb3;

Label l1;
Font f;
String text="This text is now ";

public Test(String title){


super(title);

cb1 = new Checkbox("Plain",true);


cb2 = new Checkbox("Bold",false);
cb3 = new Checkbox("Italic",false);

f = new Font("TimesNewRoman", Font.PLAIN,12);


this.setFont(f);
l1 = new Label(text);

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);
}

public void itemStateChanged(ItemEvent ie){


if(ie.getSource()==cb1 && cb1.isSelected())
l1.setFont(new Font("TimesNewRoman", l1.getStyle()
+Font.BOLD,12));
else if(ie.getSource()==cb2 && cb2.isSelected())
l1.setFont(new Font("TimesNewRoman", l1.getStyle()
+Font.BOLD,12));
else if(cb3.isSelected())
l1.setFont(new Font("TimesNewRoman", l1.getStyle()
+Font.ITALIC,12));
}

public static void main(String args[]){


new Test("Text components Demo");
}
}

You might also like