Awt Frame
Awt Frame
Frame
It is a predefined class used to create a container with the title bar and body part.
Example
Frame f=new Frame();
Mostly used methods
setTitle()
It is used to display user defined message on title bar.
Example
Frame f=new Frame(); f.setTitle("myframe");
setBackground()
It is used to set background or image of frame.
Example
Frame f=new Frame(); f.setBackground(Color.red);
Example
Frame f=new Frame(); f.setBackground("c:\image\myimage.png");
setForground()
It is used to set the foreground text color.
Example
Frame f=new Frame(); f.setForground(Color.red);
setSize()
It is used to set the width and height for frame.
Example
Frame f=new Frame(); f.setSize(400,300);
setVisible()
It is used to make the frame as visible to end user.
Example
Frame f=new Frame(); f.setVisible(true);
Note: You can write setVisible(true) or setVisible(false), if it is true then it visible otherwise not visible.
setLayout()
It is used to set any layout to the frame. You can also set null layout it means no any layout apply on frame.
Example
Frame f=new Frame(); f.setLayout(new FlowLayout());
Note: Layout is a logical container used to arrange the gui components in a specific order
add()
It is used to add non-container components (Button, List) to the frame.
Example
Frame f=new Frame(); Button b=new Button("Click"); f.add(b);
Explanation: In above code we add button on frame using f.add(b), here b is the object of Button class..
Example of Frame
import java.awt.*; class FrameDemo { public static void main(String[] args) { Frame f=new Frame(); f.setTitle("myframe"); f.setBackground(Color.cyan); f.setForeground(Color.red); f.setLayout(new FlowLayout()); Button b1=new Button("Submit"); Button b2=new Button("Cancel"); f.add(b1); f.add(b2); f.setSize(500,300); f.setVisible(true); } }
Download code Click
Example of Frame
import java.awt.*; class AddEmployee extends Frame { Label user_id,fn,ln,ftn,DOB,sex,country,mobile,address; TextField ID,f_name, l_name, Father1, Father2, dob, add1, mobile_no; Choice c; Button b1,b2,b3,b4; CheckboxGroup cg1; Checkbox male,female; public AddEmployee() { setTitle("Employee Detail"); setBackground(Color.cyan); setLayout(null); fn=new Label("Emp Name"); ftn=new Label("Father Name:"); DOB=new Label("Date of birth:"); sex=new Label("Sex:"); address=new Label("Address:"); country=new Label("Country:"); mobile=new Label("Mobile no:"); Font f=new Font("Arial", Font.BOLD, 15); fn.setFont(f); ftn.setFont(f); DOB.setFont(f); sex.setFont(f); address.setFont(f); country.setFont(f); mobile.setFont(f); fn.setBounds(20,50,100,40); ftn.setBounds(20,100,100,40); DOB.setBounds(20,150,100,40); sex.setBounds(20,200,100,40); address.setBounds(20,250,100,50); country.setBounds(20,300,100,50); mobile.setBounds(20,350,100,40); add(fn); add(ftn); add(DOB); add(sex); add(address); add(country); add(mobile); f_name=new TextField("",20); Father1=new TextField("",20); dob=new TextField("Day/Month/Year",20); add1=new TextField("",40); mobile_no=new TextField("",20); f_name.setBounds(200,60,200,20); Father1.setBounds(200,110,200,20); dob.setBounds(200,160,140,20); add1.setBounds(200,260,200,20); mobile_no.setBounds(200,360,130,20); add(f_name); add(Father1); add(dob); cg1=new CheckboxGroup(); male=new Checkbox("Male", cg1, true); female=new Checkbox("Female", cg1, false); add(male); add(female); male.setBounds(200,200,50,50); female.setBounds(300,200,60,50); add(add1); c=new Choice(); c.addItem("City"); c.addItem("New Delhi"); c.addItem("Raipur"); c.addItem("Chandigarh"); c.addItem("Dehradun"); c.addItem("Patna"); c.addItem("Dispur"); c.addItem("Other"); c.setBounds(200,310,130,20); add(c); add(c); add(c); add(c); add(c); add(c); add(c); add(c); add(c); add(c); add(c); add(c); add(c); add(mobile_no); Font fa=new Font("Arial", Font.BOLD, 15); b1=new Button("Submit"); b2=new Button("Exit"); b3=new Button("AddNew"); b1.setBounds(170,430,80,30); b2.setBounds(270,430,80,30); b3.setBounds(370,430,80,30); b1.setBackground(Color.pink); b2.setBackground(Color.pink); b3.setBackground(Color.pink); add(b1); add(b2); add(b3); b1.setFont(fa); b2.setFont(fa); b3.setFont(fa); setSize(600,500); setVisible(true); } //cons } // class class Employee { public static void main(String s[]) { AddEmployee obj=new AddEmployee(); } }
Download code Click