Docstoc

Basics of GUI in java

Document Sample
Basics of GUI in java Powered By Docstoc
					GUI in Java
      Graphical User Interface
• Graphical User Interface (GUI)
  – Gives program distinctive “look” and “feel”
  – Provides users with basic level of familiarity
     • User interacts with GUI component via mouse,
       keyboard, etc.
               GUI in Java
• Java.awt
  – Abstract Window ToolKit
  – Was used with java1.1
• Java.Swing
  – Uses some of the classes of java.awt package
  – Part of Java Foundation Classes (JFC)
          Creating a Window
• Window class in java.awt
  – Provides no boarder and title bar.
• JFrame class in java.swing
  – Provides title, boarder, and other facilities for a
    window.
Creating Window:Example
    Components and Container
• A graphical entity
  – A component is any object of a class that is a
    subclass of Component.
  – JFram Window is a component ( Subclass of
    Component class)
Subclasses of Component Class
             Window Pane
• window panes are container objects that
  represent an area of a window.
• They come in several different types.
• Defined in javax.swing package
               Component Class
• Attributes
   – Position ( x and y with respect to the container
     component)
   – Name
   – Size (width and height)
   – Foreground color, background color
   – Font
   – Cursor
   – Enabled or not ( normal or grayed out)
   – Visible or not
   – Valid or not
• Methods
  – setName(String)
  – isVisible(), isEnabled(), isValid() (Boolean)
  – setVisible(), setEnabled()
  Size and Position of a Component
• Position is represented by Point Object of
  java.awt.Point
  – point representing a location in (x, y)
    coordinate space, specified in integer precision.
• Size by java.awt.Dimension
  – It has width and Height public members.
• Size and position together by
  java.awt.Rectangle
   Methods for Size and Position

• With Dimension Class
  – Dimension getSize()
  – Dimension getSize(Dimension dim)
• With Point Class
  – Point getLocation()
  – Point getLocation(Point p)
• With Rectangle Class
  – Rectangle getBounds()
  – Rectangle getBounds(Rectangle rect)
   Changing Size and Position of a
            Component
• Void setBounds( int x, int y, int width, int
  Height)
• Void setBounds( Rectangle rect)
• Void setSize(Dimension d)
• setLocation( int x, int y)
• setLocation( Point p)
                ToolKit()
• Method of Component class
• getToolKit() returns an object of type
  ToolKit.
• It gives information about the environment
  in which application is running.
• E.g screen size
Example:NewWindow
         Visual Characteristics of
              Components
•   void setBackground(Color aColor)
•   Color getBackground()
•   void setForeground(Color bColor)
•   Color getForeground()
•   void setCursor(Cursor aCursor)
•   void setFont(Font aFont)
•   Font getFont()
Example: ColorWindow
               System Colors
• In package java.awt
• Sub class of class Color
• The class contains definitions for 24 public final
   static variables of type SystemColor that specify
   the standard system colors used by the operating
   system
• if(colorA.getRGB()=SystemColor.window.getRG
   B()) {
 // colorA is the window background color...
 }
           Creating Cursors
• An object of the java.awt.Cursor class
  encapsulates a bitmap representation of the
  mouse cursor.
• Cursor myCursor = new
  Cursor(Cursor.TEXT_CURSOR);
Creating Cursors
Example:CursorWindow
           Selecting Fonts
• Font myFont = new Font(“Serif”,
  Font.ITALIC, 12);
• Font myFont = new Font(“Serif”,
  Font.ITALIC + Font.BOLD, 12);
• GraphicsEnvironment e =
  GraphicsEnvironment.getLocalGraphicsEnv
  ironment();
   Font[] fonts = e.getAllFonts();
Example: FontInfo
SwingComponents:Buttons
      Creating a Swing Button
Container content = aWindow.getContentPane();
JButton myButton=new JButton(“MyButton”);

content.add(myButton);
       Creating JRadioButton
JRadioButton myButton=new
  RadioButton(“my radio button”);
Content.add(myButton);
Example:Adding Radio Buttons
Menus
Adding Menu:Example
Text Components
                  JPanel
• The JPanel class defines something like a
  physical panel that you can use as a
  container to group a set of components.
• JList Component
• JTable Component
                Containers
• An Abstract Class
• Adding Components to Container
  – add(Component c)
  – add(Component c, int index)
          Lay Out Managers
• A layout manager determines the way that
  components are arranged in a container.
• Layout manager for a container determines
  the position and size of all the components
  in the container.
• The classes that define layout managers all
  implement the LayoutManager interface
              Flow Layout
• The flow layout manager places
  components in a row, and when the row is
  full, it automatically spills components onto
  the next row.
• Default position is centered.
Example:FlowLayout
 Assignment : Project Deliverable 2

• Make Graphical User Interface of You
  Project using swing package in java.
• Add different swing components such as
  Buttons, Menu, Menubar, Lists, Tables etc.
  as per requirement of the project.
• Group Assignment

				
DOCUMENT INFO
Shared By:
Categories:
Tags: Basics, java
Stats:
views:2
posted:8/5/2012
language:
pages:39