Вы находитесь на странице: 1из 7

Computer

Programming 2

Panels
JPanel objects are the simplest kind of container you can use in a Swing interface.

The purpose of JPanel objects is to subdivide a display area into different groups of components.

When the display is divided into sections, you can use different rules for how each section is
organized.

You can create a JPanel object and add it to a container with the following statements:

JPanel topPanel = new JPanel();

FlowLayout flo = new FlowLayout();


setLayout(flo);

add(topPanel);

Panels are often used when arranging the components in an interface.

Panels also can be used when you need an area in an interface to draw something, such as an image
from a graphics file.

Another convenient use of JPanel is to create your own components that can be added to other
classes.

Using Layout Managers


In Java, the placement of components within a container depends on the size of other components
and the height and width of the container.

The layout of buttons, text fields, and other components can be affected by the following things:
• The size of the container
• The size of other components and containers
• The layout manager that is being used

There are several layout managers you can use to affect how components are shown.
The default manager for panels is the FlowLayout class in the java.awt package.

Under FlowLayout, components are dropped onto an area in the same way words are organized on a
printed page in English—from left to right, then on to the next line when there's no more space.

FlowLayout LayoutKo = new FlowLayout();


setLayout(LayoutKo);

You also can set up a layout manager to work within a specific container, such as a JPanel object. You
can do this by using the setLayout() method of that container object.
The following statements create a JPanel object called panelTop and set it up to use FlowLayout
as its layout manager:

JPanel panelTop = new JPanel();


FlowLayout LayoutKo = new FlowLayout();

panelTop.setLayout(LayoutKo);

Page 1 of 7
Computer
Programming 2
The GridLayout Manager

The GridLayout class in the java.awt package organizes all components in a container into a
specific number of rows and columns. All components are allocated the same amount of size in the
display area, so if you specify a grid that is three columns wide and three rows tall, the container will
be divided into nine areas of equal size.

The following statements create a container and set it to use a grid layout that is two rows wide and
three columns tall:

GridLayout grid = new GridLayout(2, 3);


setLayout(grid);

Example: the Login frame using GridLayout


import javax.swing.*;
import java.awt.*;
public class LoginKo extends JFrame {

public LoginKo() {
setSize(350,150);

JLabel lblUName = new JLabel("UserName:");


JLabel lblPwd = new JLabel("Password:");

JTextField txtUname = new JTextField(20);


JPasswordField txtPwd = new JPasswordField(20);

JButton btnOK = new JButton("Ok");


JButton btnCancel = new JButton("Cancel");

// create a GridLayout with 3 rows, 2 columns


GridLayout grid = new GridLayout(3,2);
setLayout(grid);

//add controls to Layout


add(lblUName);
add(txtUname);
add(lblPwd);
add(txtPwd);
add(btnOK);
add(btnCancel);

setVisible(true);
}

public static void main(String[] args) {


LoginKo log = new LoginKo();
}
}

Page 2 of 7
Computer
Programming 2

The BorderLayout Manager

The BorderLayout manager arranges components into


five areas: four denoted by compass directions (NORTH, SOUTH, EAST, WEST) and one for the CENTER
area.

BorderLayout BLayout = new BorderLayout();


setLayout(BLayout);

add(lblReg, BorderLayout.NORTH);
add(lblName, BorderLayout.WEST);
add(txtName, BorderLayout.CENTER);
add(btnReg, BorderLayout.EAST);
add(btnCancel, BorderLayout.SOUTH);

Example: the Registration frame using BorderLayout

import javax.swing.*;
import java.awt.*;
public class RegistrationKo extends JFrame {

public RegistrationKo () {
setSize(350,150);

JLabel lblReg = new JLabel("Please enter you whole name to register:");


JLabel lblName = new JLabel(" Name : ");

JTextField txtName = new JTextField(20);

JButton btnReg = new JButton("Register");


JButton btnCancel = new JButton("Cancel");

BorderLayout BLayout = new BorderLayout();


setLayout(BLayout);

add(lblReg, BorderLayout.NORTH);
add(lblName, BorderLayout.WEST);
add(txtName, BorderLayout.CENTER);
add(btnReg, BorderLayout.EAST);
add(btnCancel, BorderLayout.SOUTH);

setVisible(true);
}

public static void main(String[] args) {


RegistrationKo RegKo = new RegistrationKo ();
}
}

Page 3 of 7
Computer
Programming 2

The BoxLayout Manager

BoxLayout makes it possible to stack components in a single row horizontally or vertically.

Unlike the other layout managers, the BoxLayout class is in the javax.swing package.

When components are lined up in FlowLayout, they hit the right margin and wrap around to the next
line, just like the words in this paragraph.

For the times when you want components on a single left-to-right or top-to-bottom line, they can be
placed in a panel arranged with BoxLayout.

Create the panel, then create a layout manager with two arguments:
• The component to organize in box layout
• The value BoxLayout.Y_AXIS for vertical alignment and BoxLayout.X_AXIS for horizontal
alignment

JPanel panelKo = new JPanel();


BoxLayout BxLayout = new BoxLayout(panelKo, BoxLayout.Y_AXIS);

panelKo.setLayout(BxLayout);
panelKo.add(lblUName);
panelKo.add(txtUname);
panelKo.add(lblPwd);
panelKo.add(txtPwd);
panelKo.add(btnOK);
panelKo.add(btnCancel);

add(panelKo);

Page 4 of 7
Computer
Programming 2

Example: the Login frame using BoxLayout

import javax.swing.*;
import java.awt.*;
public class LoginKo2 extends JFrame {

public LoginKo2() {
setSize(300,170);

JLabel lblUName = new JLabel("UserName:");


JLabel lblPwd = new JLabel("Password:");

JTextField txtUname = new JTextField(20);


JPasswordField txtPwd = new JPasswordField(20);

JButton btnOK = new JButton("OK");


JButton btnCancel = new JButton("Cancel");

JPanel panelKo = new JPanel();


BoxLayout BxLayout = new BoxLayout(panelKo, BoxLayout.Y_AXIS);

panelKo.setLayout(BxLayout);
panelKo.add(lblUName);
panelKo.add(txtUname);
panelKo.add(lblPwd);
panelKo.add(txtPwd);
panelKo.add(btnOK);
panelKo.add(btnCancel);

add(panelKo);
setVisible(true);
}

public static void main(String[] args) {


LoginKo2 log2 = new LoginKo2();
} Page 5 of 7
}
Computer
Programming 2

Separating Components with Insets

Insets - an object that represents the border area of a container.

Page 6 of 7
Computer
Programming 2
It moves components away from the edges of the container.

The Insets class, which is part of the java.awt package, has a constructor method that takes
four arguments: the space to leave at the top, left, bottom, and right on the container.

Each argument is specified using pixels, the same unit of measure employed when defining the
size of a frame.

The following statement creates an Insets object:

Insets around = new Insets(10, 6, 10, 3);

The around object represents a container border that is 10 pixels inside the top edge, 6 pixels
inside the left, 10 pixels inside the bottom, and 3 pixels inside the right.

To make use of an Insets object in a container, you must override the container's getInsets()
method.

This method has no arguments and returns an Insets object, as in the following example:

public Insets getInsets() {

Insets layo = new Insets(50, 25, 25, 25);

return squeeze;
}

The Login frame using GridLayout with insets

The container shown has an empty border that's 50 pixels from the top edge, 25 pixels from the
left edge, 25 pixels from the bottom edge and 25 pixels from the right edge.

Page 7 of 7

Вам также может понравиться