Docsity

Java GUI Components: Choices, Containers, Panels, Frames, and Layout Managers, Slides of Java Programming

Partial preview of the text

Download Java GUI Components: Choices, Containers, Panels, Frames, and Layout Managers and more Slides Java Programming in PDF only on Docsity!

Choices

  • Used to group choice
  • Constructors

 Choice()

  • Two step components

 Create an object  Populate it with items

  • Important methods

 addItem(String item)  remove(int position)  removeAll()  select (int position)  Select(String item)  String getSelectedItem()  int getSelectedIndex()  String getItem(int position)

Choices

  • Used to group choice
  • Constructors

 Choice()

  • Two step components

 Create an object  Populate it with items

  • Important methods

 addItem(String item)  remove(int position)  removeAll()  select (int position)  Select(String item)  String getSelectedItem()  int getSelectedIndex()  String getItem(int position)

choices cont..

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

Containers

  • Containers

 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

  • Important methods of container (Common to all containers)

 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( )

Panels

  • A panel usually have no GUI representation  Can have different back ground color
  • It is used to organized components
  • Used on other containers so that more than one layouts can be used
  • Constructors

 Panel()  Panel( LayoutManager initialLayout )

  • Important Methods

 Refer to slide 9 Containers

  • Example: Multipanel Applet

Using panels

  • MultiPanel Applet  Flow layout and grid layout

Frames

  • Well defined GUI
  • Enable you to create separate windows

 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

  • Constructors  public Frame()  public Frame(String frameTitle)
  • Important methods  void resize(int height, int width)  void show()  void hide()  void dispose()  void setTitle(), String getTitle()  setCursot( )

Frames

  • Cursors

 Frame.DEFULT_CURSOR  Frame.CROSSHAIR_CURSOR  Frame.TEXT_CURSOR  Frame.HAND_CURSOR  Frame.BUSY_CURSOR

  • Few Steps to Create a frame

 Create an object  Frame f=new Frame(“First Frame”)  Set Size  f.resize(200,300)  Show  f.show()  Dispose  f.dispose()

Frame example

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

More Layout Managers

  • Grid Layout  Divides container into grid of equally sized cells  Each component is placed in a cell starting from left  Useful when using components of equal size
  • Constructors  GridLayout(int rows,int col)  GridLayout(int rows,int col, int vg, in hg)
  • Automatically adjusts rows or columns if necessary
  • Doesn’t display unnecessary cells
  • Important Methods  int getVgap(), int getHgap()  setVgap(int value), setHgap(int value)

grid layout

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

Border Layout

  • Divides the container into following five areas

 North  South  East  Wet  Center

  • Areas are arranged like points on a compass
  • If one of the area is not used it is assigned to the component
  • A special add method is used to add components

 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

  • Constructors

 BorderLayout()  BorderLayout(int hgap,int vgap)

West

North

East

South

Null Layout

  • It is recommended to use a layout
  • If layouts are not necessary or required pass null to the setLayout

method

  • For a null layout the position and size of each component need to

be defined

  • A useful method when using null layout

 setBounds(int x,int y,int height,int width)

  • Note

 If size and location is not defined when using null layout the components will not display correctly

null layout..