+92 332 4229 857 99ProjectIdeas@Gmail.com

GUI In Java


GUI (Graphical User Interface)

Supported Libraries in Java:

Awt (Abstract Window Toolkit)

e.g., import java.awt.*;
 Components in awt are:

Button.

Frame.

TextField.

Label.

List.

And many more..
Basically these components are called as “Heavy weight components” because they pick the component (button, label etc.) from an underlying operating system environment.

Swing (Starts with ‘J’ Letter)

e.g., import javax.swing.*;

Components in swing are:

JButton.

JFrame.

JTextField.

JLabel.

JList.

And many more..
These components are called as “Light Weight components” because they are java’s own.



GUI Creation Steps:

Imports required packages.
import java.awt.*;
import javax.swing.*;

Set up a top level container in which you create components like buttons, labels etc.
            JFrame myFrame = new JFrame();

Get the components area of top level container.
Container con = null;
con = myFrame.getContentPane();

Apply Layout to that area.
 con.setLayout(new FlowLayout());//Later we discuss about Layout Managers 

Create & Add components.
            JButton helloButton = new JButton("Hello");
con.add(helloButton);

Set size of frame and show it.
myFrame.setSize(250,100);
       myFrame.setVisible(true);

GUI Creation Approaches:

Two approaches generally adopt by the programmer/developer.

Approach One through Composition (Through creating JFrame object):

public class GUIComposition {
JFrame myFrame = null;
              Container con = null;
                    
                  public GUIComposition() {
                        myFrame = new JFrame();
con = myFrame.getContentPane();

……….
           
myFrame.setSize(250,100);

              myFrame.setVisible(true);
}
  }

Approach Second through Inheritance (extending from JFrame):

public class GUIInheritance extends JFrame {
Container con = null;
                    
                   public GUIInheritance() {
con = getContentPane();
                         
 ……….
setSize(250,100);

              setVisible(true);
          }
}

Layout Managers:
The layout managers automatically place components on the screen, you don’t have to explicitly define the x-axis and y-axis coordinates of components to place it on container.
Java gives us many layout managers, following are some commonly used:

FlowLayout()

GridLayout()

BorderLayout() 
FlowLayout(): 
  Places components in a line as long as they fit, then start the next line. 
  Uses “best judgment” in spacing components.  Centers by default. 
  Let’s each component assume its natural (preferred) size.

  Often used for placing buttons on panels.
             
             JFrame myFrame = new JFrame();         
             con = myFrame.getContentPane();
             con.setLayout (new FlowLayout() );
             JButton b1 = new JButton("Next Slide");
             JButton b2 = new JButton("Previous Slide");
             JButton b3 = new JButton("Back to Start");
             JButton b4 = new JButton("Last Slide");
             JButton b5 = new JButton("Exit");
             con.add(b1);
             con.add(b2);
             con.add(b3);
             con.add(b4);
             con.add(b5);

GridLayout():
Splits the panel into a grid with given number of rows and columns.
Places components into the grid cells.
Forces the size of each component to occupy the whole cell.
Allows additional spacing between cells.
   
      JFrame myFrame = new JFrame();
             con = myFrame.getContentPane(); 
con.setLayout (new GridLayout(3 , 2) ); //three rows, two columns
             JButton b1 = new JButton("Next Slide");
             JButton b2 = new JButton("Previous Slide");
             JButton b3 = new JButton("Back to Start");
             JButton b4 = new JButton("Last Slide");
             JButton b5 = new JButton("Exit");
             con.add(b1);
             con.add(b2);
             con.add(b3);
             con.add(b4);
             con.add(b5);

BorderLayout():
Divides the area into five regions.
Adds a component to the specified region 
Forces the size of each component to occupy the whole region. 

             JFrame myFrame = new JFrame();

             con = myFrame.getContentPane();
             con.setLayout (new BorderLayout() );
             JButton b1 = new JButton("Next Slide");
             JButton b2 = new JButton("Previous Slide");
             JButton b3 = new JButton("Back to Start");
             JButton b4 = new JButton("Last Slide");
             JButton b5 = new JButton("Exit");

             con.add(b1, BorderLayout.NORTH);
             con.add(b2, BorderLayout.SOUTH);
             con.add(b3, BorderLayout.EAST);
             con.add(b4, BorderLayout.WEST);
             con.add(b5, BorderLayout.CENTER);

0 comments: