Learning Center
Plans & pricing Sign in
Sign Out

Basics of GUI in java


									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
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
   – 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
  – 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
   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
• Void setBounds( int x, int y, int width, int
• Void setBounds( Rectangle rect)
• Void setSize(Dimension d)
• setLocation( int x, int y)
• setLocation( Point p)
• Method of Component class
• getToolKit() returns an object of type
• It gives information about the environment
  in which application is running.
• E.g screen size
         Visual Characteristics of
•   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
• 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
Creating Cursors
           Selecting Fonts
• Font myFont = new Font(“Serif”,
  Font.ITALIC, 12);
• Font myFont = new Font(“Serif”,
  Font.ITALIC + Font.BOLD, 12);
• GraphicsEnvironment e =
   Font[] fonts = e.getAllFonts();
Example: FontInfo
      Creating a Swing Button
Container content = aWindow.getContentPane();
JButton myButton=new JButton(“MyButton”);

       Creating JRadioButton
JRadioButton myButton=new
  RadioButton(“my radio button”);
Example:Adding Radio Buttons
Adding Menu:Example
Text Components
• 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
• 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.
 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

To top