Академический Документы
Профессиональный Документы
Культура Документы
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:
add(topPanel);
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.
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.
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:
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:
public LoginKo() {
setSize(350,150);
setVisible(true);
}
Page 2 of 7
Computer
Programming 2
add(lblReg, BorderLayout.NORTH);
add(lblName, BorderLayout.WEST);
add(txtName, BorderLayout.CENTER);
add(btnReg, BorderLayout.EAST);
add(btnCancel, BorderLayout.SOUTH);
import javax.swing.*;
import java.awt.*;
public class RegistrationKo extends JFrame {
public RegistrationKo () {
setSize(350,150);
add(lblReg, BorderLayout.NORTH);
add(lblName, BorderLayout.WEST);
add(txtName, BorderLayout.CENTER);
add(btnReg, BorderLayout.EAST);
add(btnCancel, BorderLayout.SOUTH);
setVisible(true);
}
Page 3 of 7
Computer
Programming 2
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
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
import javax.swing.*;
import java.awt.*;
public class LoginKo2 extends JFrame {
public LoginKo2() {
setSize(300,170);
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);
}
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 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:
return squeeze;
}
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