37
Introduction to GUI in Java 1

Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

Embed Size (px)

Citation preview

Page 1: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

1

Introduction to GUI in Java

Page 2: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

2

Graphical User Interface

• Java is equipped with many powerful ,easy to use GUI component such as input and output dialog boxes that you can use them to make your program attractive and user friendly

• GUI-based programs are implemented by using classes from the javax.swing and java.awt packages.

Page 3: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

3Creating GUI Objects

// Create a button with text OK JButton jbtOK = new JButton("OK");  // Create a label with text "Enter your name: "JLabel jlblName = new JLabel("Enter your name: ");  

// Create a text field with text "Type Name Here"JTextField jtfName = new JTextField("Type Name Here");  // Create a check box with text boldJCheckBox jchkBold = new JCheckBox("Bold");  // Create a radio button with text redJRadioButton jrbRed = new JRadioButton("Red");  // Create a combo box with choices red, green, and blueJComboBox jcboColor = new JComboBox(new String[]{"Red", "Green", "Blue"});

Label Text field

Check Box

Radio Button

Combo Box

Page 4: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

4

Frames

Frame is a window that is not contained inside another window. Frame is the basis to contain other user interface components in Java GUI applications.

The JFrame class can be used to create windows.

For Swing GUI programs, use JFrame class to create widows.

Page 5: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

5

Example

Page 6: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

6Creating Frames

import javax.swing.*;

public class MyFrame { public static void main(String[] args) { JFrame frame = new JFrame("Test Frame"); frame.setSize(400, 300); frame.setVisible(true);}

}

Page 7: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

7JFrame Class

javax.swing.JFrame

+JFrame()

+JFrame(title: String)

+getSize(width: int, height: int): void

+setLocation(x: int, y: int): void

+setVisible(visible: boolean): void

+setDefaultCloseOperation(mode: int): void

+setLocationRelativeTo (c: Component): void

Creates a default frame with no title.

Creates a frame with the specified title.

Specifies the size of the frame.

Specifies the upper-left corner location of the frame.

Sets true to display the frame.

Specifies the operation when the frame is closed.

Sets the location of the frame relative to the specified component. If the component is null, the frame is centered on the screen.

Page 8: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

8Creating a JFrame

a JFrame class have the following component:

setResizable(boolean var); setDefaultCloseOperation(JFrame. *********); EXIT_ON_CLOSE

DO_NOTHING_ON_CLOSE

Page 9: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

9Example

Page 10: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

10

Example

After Pressing OK or closing the “How are you?” dialog, the “Good Bye” dialog appears

Page 11: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

11

Using Dialog Boxes for Input/Output

• JOptionPane this class is contained in the package javax.swing

• We will use two methods of this class :

• showInputDialog

• showMessageDialog

Page 12: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

12ShowInputDialog

General syntaxStr= JOptionPane.showInputDialog(ParentComponent,string expression, box title , message type);

This method returns the input as a String value

Example:str=JOptionPane.showInputDialog(FrameOne,"Enter your name“, “Input” ,QUESTION_MESSAGE);

Page 13: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

13showMessageDialog

General syntaxJOptionPane.showMessageDialog(ParentComponent, message string, box title , message type);

Page 14: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

14Adding pane to a Frame

Page 15: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

15

Page 16: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

16Example 2

Page 17: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

17

Page 18: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

18Message Type

JOptionPane.showMessageDialog(Jframe/null,"Hello","Title", JOptionPane.*********);

INFORMATION_MESSAGE ERROR_MESSAGE WARNING_MESSAGE

QUESTION_MESSAGE

PLAIN_MESSAGE

Page 19: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

19Example for adding two numbers

Page 20: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

20

Page 21: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

21Subclassing JFrame

To create a customized frame window, we define a subclass of the JFrame class.

class TESTGUI extends JFrame {

Public TESTGUI ()

{ // constructor

// necessary code

//set the frame default properties} }

write a code to do the following : An instance of TESTGUI will have the following default

characteristics:The title is set to My First Subclass.The program terminates when the close box is clicked.The size of the frame is 300 pixels wide by 200 pixels high.The frame is positioned at screen coordinate (150, 250).

Page 22: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

22Jframe Subclass

Page 23: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

23

This gray area is the content pane of this frame.

This gray area is the content pane of this frame.

The content pane is where we put GUI objects such as buttons, labels, scroll bars, and others. We access the content pane by calling the frame’s getContentPane method in class Container.

Page 24: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

24Accessing the content pane of a JFrame

To retrieve the content pane of a JFrame:

frame.getContentPane(); To add a component to the content pane:

frame.getContentPane().add(yellowLabel); To remove a component from the content pane:

frame.getContentPane().remove(yellowLabel);

Page 25: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

25Using Panels as Sub-Containers

Panels act as sub-containers for grouping user interface components.

It is recommended that you place the user interface components in panels and place the panels in a frame. You can also place panels in a panel.

To add a component to JFrame, you actually add it to the content pane of JFrame. To add a component to a panel, you add it directly to the panel using the add method.

Page 26: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

26Creating a JPanel

You can use new JPanel() to create a panel with a default FlowLayout manager

or new JPanel(LayoutManager) to create a panel with the specified layout manager.

Use the add(Component) method to add a component to the panel. For example,

JPanel p = new JPanel();

p.add(new JButton("OK"));

Page 27: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

27

Layouts

Page 28: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

28

Layout Managers

Java’s layout managers provide a level of abstraction to automatically map your user interface on all window systems.

The UI components are placed in containers. Each container has a layout manager to arrange the UI components within the container.

Layout managers are set in containers using the setLayout(LayoutManager) method in a container.

Page 29: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

29Kinds of Layout Managers

FlowLayout

GridLayout

BorderLayout

Page 30: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

30FlowLayout Example

FlowLayout is the default layout manager for every JPanel. It simply lays out components in a single row, starting a new row if its container is not sufficiently wide.

If container is more than wide enough, components are centered Can change alignment using FlowLayout.setAlignment()

Page 31: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

31The FlowLayout Class

java.awt.FlowLayout

-alignment: int

-hgap: int

-vgap: int

+FlowLayout()

+FlowLayout(alignment: int)

+FlowLayout(alignment: int, hgap: int, vgap: int)

The alignment of this layout manager (default: CENTER).

The horizontal gap of this layout manager (default: 5 pixels).

The vertical gap of this layout manager (default: 5 pixels).

Creates a default FlowLayout manager.

Creates a FlowLayout manager with a specified alignment.

Creates a FlowLayout manager with a specified alignment, horizontal gap, and vertical gap.

The get and set methods for these data fields are provided in the class, but omitted in the UML diagram for brevity.

Page 32: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

32GridLayout

GridLayout simply makes a bunch of components equal in size and displays them in the requested number of rows and columns.

Page 33: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

33GridLayout

java.awt.GridLayout

-rows: int

-columns: int

-hgap: int

-vgap: int

+GridLayout()

+GridLayout(rows: int, columns: int)

+GridLayout(rows: int, columns: int, hgap: int, vgap: int)

The number of rows in this layout manager (default: 1).

The number of columns in this layout manager (default: 1).

The horizontal gap of this layout manager (default: 0).

The vertical gap of this layout manager (default: 0).

Creates a default GridLayout manager.

Creates a GridLayout with a specified number of rows and columns.

Creates a GridLayout manager with a specified number of rows and columns, horizontal gap, and vertical gap.

The get and set methods for these data fields are provided in the class, but omitted in the UML diagram for brevity.

Page 34: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

34BorderLayout

Every content pane is initialized to use a BorderLayout.the content pane is the main container in all frames, applets, and dialogs.) ABorderLayout places components in up to five areas: top, bottom, left, right, and center.

Page 35: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

35BorderLayout

java.awt.BorderLayout

-hgap: int

-vgap: int

+BorderLayout()

+BorderLayout(hgap: int, vgap: int)

The horizontal gap of this layout manager (default: 0).

The vertical gap of this layout manager (default: 0).

Creates a default BorderLayout manager.

Creates a BorderLayout manager with a specified number of horizontal gap, and vertical gap.

The get and set methods for these data fields are provided in the class, but omitted in the UML diagram for brevity.

Page 36: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

36

1. Create it Instantiate object:

JButton b = new JButton();

2. Configure it Methods:

b.setText(“Press me”);

3. Add it panel.add(b);

4. Listen to it Events: Listeners

Press me

Adding components to Frame

Page 37: Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog

37

import javax.swing.*;

class Hello {

public static void main(String[] args){

JFrame f = new JFrame(“Hello World”);

JPanel p = new JPanel();

JButton b = new JButton(“press me”);

p.add(b); // add button to panel

f.getContentPane().add(p); // add panel to frame

}

}

press me