GUI Layout Managers in Java

undefined
Introduction to GUI
Introduction to GUI
1
Graphical User
Interface
3
2
 
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
.
Container 
contentPane
 = 
getContentPane();
contentPane
 
methods:
setBackground(Color.BLUE);
add(Object_name);
setLayout( layout_type);
Placing GUI Objects on a
Frame
There are two ways to put GUI objects on the content pane of a
frame:
Use a 
layout manager (
using 
setLayout()
method
)
Use 
absolute positioning
null layout manager
3
Layout Managers
The layout manager determines how the GUI components are
added to the container
the common ones are
FlowLayout ()
GridLayout (rows, columns)
BorderLayout
4
FlowLayout
In using this layout, GUI components are placed in the top-to-
bottom, left-to right order
As a default, components on each line are centered, but you
can change it to left or right alignment.
When the frame containing the component is resized, the
placement of components is adjusted accordingly.
5
FlowLayout Sample
 
This shows the placement of five buttons by using FlowLayout.
6
 
7
 
 
8
GridLayout
This layout manager places GUI components on equal-size N by M
grids.
Components are placed in top-to-bottom, left-to-right order.
The number of rows and columns remains the same after the frame
is resized, but the width and height of each region will change.
9
 
 
10
11
BorderLayout
This layout manager divides the container into five regions: center,
north, south, east, and west.
If the window is enlarged, the center area gets as much of the
available space as possible.
The other areas expand only as much as necessary to fill all available
space.
Not all regions have to be occupied.
12
 
 
13
14
Absolute Positioning
Here we do not use any layout manager.
We place the GUI objects on the frame’s content pane by
explicitly specifying their position and size.
This is called 
absolute positioning
15
 
16
Other Common GUI Components
JCheckBox
Useful in representing a collection of binary (yes/no, true/false)
options.
A JCheckBox object generates 
action events 
(like a JButton), but
also generates 
item events
, when the state of a check-box button
changes (selected or deselected).
17
JCheckBox
JCheckBox box1= new JCheckBox(“BOX1");
box1.setSelected(true); 
//for defualt selection
Handling the 
CheckBox
 events:
1.
implements
 ActionListener , 
ItemListener
2.
Redefine the function 
itemStateChanged
public void 
itemStateChanged(ItemEvent e) {
            Object source = e.getSource();
            if (source == box1) {//do something  }
 else if (source == ......) {}
18
JRadioButton
The radio button is similar to the check-box, but only one button
can be selected from the group.
 
When you select a new item, the currently selected radio button
will get deselected.
Thus, the JRadioButton must be added to a group.
A JRadioButton generates both 
action events 
and 
item events
.
JRadioButton radio1= new JRadioButton(“
r1
");
19
Other Common GUI
Components
JComboBox (drop-down list)
Similar to JRadioButton,
 but the choices are presented to the user
 in a form of a drop-down list
Jlist
Used to display a list of items
You can highlight one or more selection
20
Menus
The javax.swing package contains three menu-related classes:
JMenuBar
, 
JMenu
, and 
JMenuItem
.
JMenuBar
 is a bar where the menus are placed. There is one menu bar
per frame.
JMenu
 (such as File or Edit) is a group of menu choices. JMenuBar may
include many JMenu objects.
JMenuItem
 (such as Copy, Cut, or Paste) is an individual menu choice in
a JMenu object.
Only the 
JMenuItem
 objects generate events.
21
 
 
22
Sequence for Creating Menus
1.
Create a 
JMenuBar
 object and attach it to a frame.
2.
Create a 
JMenu
 object.
3.
Create 
JMenuItem
 objects and add them to the 
JMenu
 object.
4.
Attach the 
JMenu
 object to the 
JMenuBar
 object.
23
Example
public class gui implements ActionListener
{
  private JFrame frame;
  private JMenuBar menuBar;
  private JMenu fileMenu;
  private JMenu editMenu;
  private JMenuItem openMenuItem;
  private JMenuItem cutMenuItem;
  private JMenuItem copyMenuItem;
  private JMenuItem pasteMenuItem;
24
25
26
    // add menus to menubar
    menuBar.add(fileMenu);
    menuBar.add(editMenu);
    // put the menubar on the frame
    frame.setJMenuBar(menuBar);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setPreferredSize(new Dimension(400, 300));
    frame.setVisible(true);
  }
Slide Note
Embed
Share

Exploring the concepts of GUI layout managers in Java, including how to place GUI objects on a frame using different layout managers such as FlowLayout, GridLayout, and BorderLayout. This guide covers the functionality and characteristics of each layout manager to help you effectively design graphical user interfaces in Java programming.

  • Java GUI
  • Layout Managers
  • FlowLayout
  • GridLayout
  • BorderLayout

Uploaded on Sep 13, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. 1 Introduction to GUI Graphical User Interface 3

  2. This gray area is the content pane of this frame. 2 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. Container contentPane = getContentPane(); contentPane methods: setBackground(Color.BLUE); add(Object_name); setLayout( layout_type);

  3. 3 Placing GUI Objects on a Frame There are two ways to put GUI objects on the content pane of a frame: Use a layout manager (using setLayout()method) Use absolute positioning null layout manager

  4. 4 Layout Managers The layout manager determines how the GUI components are added to the container the common ones are FlowLayout () GridLayout (rows, columns) BorderLayout

  5. 5 FlowLayout In using this layout, GUI components are placed in the top-to- bottom, left-to right order As a default, components on each line are centered, but you can change it to left or right alignment. When the frame containing the component is resized, the placement of components is adjusted accordingly.

  6. 6 FlowLayout Sample This shows the placement of five buttons by using FlowLayout.

  7. 7

  8. 8

  9. 9 GridLayout This layout manager places GUI components on equal-size N by M grids. Components are placed in top-to-bottom, left-to-right order. The number of rows and columns remains the same after the frame is resized, but the width and height of each region will change.

  10. 10

  11. 11

  12. 12 BorderLayout This layout manager divides the container into five regions: center, north, south, east, and west. If the window is enlarged, the center area gets as much of the available space as possible. The other areas expand only as much as necessary to fill all available space. Not all regions have to be occupied.

  13. 13

  14. 14

  15. 15 Absolute Positioning Here we do not use any layout manager. We place the GUI objects on the frame s content pane by explicitly specifying their position and size. This is called absolute positioning

  16. 16

  17. 17 Other Common GUI Components JCheckBox Useful in representing a collection of binary (yes/no, true/false) options. A JCheckBox object generates action events (like a JButton), but also generates item events, when the state of a check-box button changes (selected or deselected).

  18. 18 JCheckBox JCheckBox box1= new JCheckBox( BOX1"); box1.setSelected(true); //for defualt selection Handling the CheckBox events: implements ActionListener , ItemListener Redefine the function itemStateChanged 1. 2. public void itemStateChanged(ItemEvent e) { Object source = e.getSource(); if (source == box1) {//do something } else if (source == ......) {}

  19. 19 JRadioButton The radio button is similar to the check-box, but only one button can be selected from the group. When you select a new item, the currently selected radio button will get deselected. Thus, the JRadioButton must be added to a group. A JRadioButton generates both action events and item events. JRadioButton radio1= new JRadioButton( r1");

  20. 20 Other Common GUI Components JComboBox (drop-down list) Similar to JRadioButton, but the choices are presented to the user in a form of a drop-down list Jlist Used to display a list of items You can highlight one or more selection

  21. 21 Menus The javax.swing package contains three menu-related classes: JMenuBar, JMenu, and JMenuItem. JMenuBar is a bar where the menus are placed. There is one menu bar per frame. JMenu (such as File or Edit) is a group of menu choices. JMenuBar may include many JMenu objects. JMenuItem (such as Copy, Cut, or Paste) is an individual menu choice in a JMenu object. Only the JMenuItem objects generate events.

  22. 22

  23. 23 Sequence for Creating Menus Create a JMenuBar object and attach it to a frame. 1. Create a JMenu object. 2. Create JMenuItem objects and add them to the JMenu object. 3. Attach the JMenu object to the JMenuBar object. 4.

  24. 24 Example public class gui implements ActionListener { private JFrame frame; private JMenuBar menuBar; private JMenu fileMenu; private JMenu editMenu; private JMenuItem openMenuItem; private JMenuItem cutMenuItem; private JMenuItem copyMenuItem; private JMenuItem pasteMenuItem;

  25. frame = new JFrame("Java Menubar Example"); 25 menuBar = new JMenuBar(); // build the File menu fileMenu = new JMenu("File"); openMenuItem = new JMenuItem("Open"); openMenuItem.addActionListener(this); fileMenu.add(openMenuItem); // build the Edit menu editMenu = new JMenu("Edit"); cutMenuItem = new JMenuItem("Cut"); copyMenuItem = new JMenuItem("Copy"); pasteMenuItem = new JMenuItem("Paste"); editMenu.add(cutMenuItem); editMenu.add(copyMenuItem); editMenu.add(pasteMenuItem);

  26. 26 // add menus to menubar menuBar.add(fileMenu); menuBar.add(editMenu); // put the menubar on the frame frame.setJMenuBar(menuBar); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setPreferredSize(new Dimension(400, 300)); frame.setVisible(true); }

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#