

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of various java gui components, including choices, containers, panels, frames, and layout managers. It covers their constructors, methods, and usage. The document also includes examples of using these components in java applets and frames.
Typology: Slides
1 / 25
This page cannot be seen from the preview
Don't miss anything!


















Choice()
Create an object Populate it with items
addItem(String item) remove(int position) removeAll() select (int position) Select(String item) String getSelectedItem() int getSelectedIndex() String getItem(int position)
Choice()
Create an object Populate it with items
addItem(String item) remove(int position) removeAll() select (int position) Select(String item) String getSelectedItem() int getSelectedIndex() String getItem(int position)
import java.awt.; import java.applet.;
public class TestApplet2 extends Applet{
Choice countries=new Choice();
public void init(){ countries.addItem(“Pakistan”); countries.addItem(“Iran”); countries.addItem(“China”); add(countries) } }
Any component that can contain other components is a container Used to contain other components All container classes are inherited from the class Container Containers can be thought of a vector
add( Component comp ) to add components to a container void remove(Component comp) to remove a single component from the container void removeAll() to remove all components in the container Component getComponent(int index) method to get to get a certain component Component[] getComponents() tp get all components in a container int countComponents( )
Panel() Panel( LayoutManager initialLayout )
Refer to slide 9 Containers
Can develop stand alone applications (no need of browser) Allow development of may window applications Can be displayed or hide dynamically (can use button to open a new frame) Provides a rich set of cursors e.g. busy,resize,crosshair etc
Frame.DEFULT_CURSOR Frame.CROSSHAIR_CURSOR Frame.TEXT_CURSOR Frame.HAND_CURSOR Frame.BUSY_CURSOR
Create an object Frame f=new Frame(“First Frame”) Set Size f.resize(200,300) Show f.show() Dispose f.dispose()
Import java.wt.; import java.applet.;
public class FrameApplet extends Applet {
Frame f=new Frame("First Frame"); Button showFrame=new Button("Show Frame"); Button hideFrame=new Button("Hide Frame"); Button closeFrame=new Button("Close Frame"); public Applet2(){ }
public void init(){
f.setSize(300,300); add(showFrame); add(hideFrame); add(closeFrame); } //end of init method
public boolean action(Event e,Object obj){ String s;
//getting the label of the button clicked s=(String)obj;
if(s=="Show Frame") f.show(); if(s=="Hide Frame") f.hide();
if(s=="Close Frame") f.dispose(); return true;
} //end of action method
} // end of class
import java.awt.l; import java.applet.;
public class Applet2 extends Applet {
public void init(){ GridLayout grid=new GridLayout(2,2); setLayout(grid);
for(int a=1;a<5;a++) add(new Button("Button"+a));
grid.setVgap(5); grid.setHgap(5);
} //end of init method
} //end of class
North South East Wet Center
Add(“AreaName”,component) No component is displayed if regular add method is used If two components are added to same location the last one will be displayed
BorderLayout() BorderLayout(int hgap,int vgap)
West
North
East
South
setBounds(int x,int y,int height,int width)
If size and location is not defined when using null layout the components will not display correctly