Professional Documents
Culture Documents
Programiranje Gui-Ja U Javi Objekt Jframe: Proširuje (Extends) Objekt Jframe, Što Znači Da Će Naša Aplikacija Acquire
Programiranje Gui-Ja U Javi Objekt Jframe: Proširuje (Extends) Objekt Jframe, Što Znači Da Će Naša Aplikacija Acquire
Programiranje Gui-Ja U Javi Objekt Jframe: Proširuje (Extends) Objekt Jframe, Što Znači Da Će Naša Aplikacija Acquire
Objekt JFrame
• Okvir je objekt u kome korisnik gradi interfejs. Svaka aplikacija koju gradimo
proširuje (extends) objekt JFrame, što znači da će naša aplikacija ****acquire
sve karakteristike okvira. To je centralna stvar u razvoju Java GUI aplikacija.
Okvir je objekt kontejner (container), pošto on „drži” druge kontrole. Jedno od
svojstava objekta kontejner je svojstvo vidlivosti (visible property) koje ako se
postavi na vrednost false, znači da će sve kontrole postati nevidljive.
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
exitForm(e);
}
});
public MyFrame()
{
// code to build the form
setTitle("My Frame");
setResizable(false);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
exitForm(e);
}
});
getContentPane().setLayout(new GridBagLayout());
// code to position controls follows
.
.
.
pack();
}
private void exitForm(WindowEvent e)
{
System.exit(0);
}
}
• Da li ste primetili da je u svim aplikacijama koje smo do sada gradili okvir počinjao
u gornjem levom uglu ekrana? Međutim, bilo bi lepo kada bi okvir bio centriran
kada se aplikacija pokrene. Sada će biti pokazano kako to može da se uradi.
Prvo, pogledajmo kako veličinu okvira postavlja menadžer GridBagLayout.
• There are other variables associated with GridBagConstraints that can be used
to adjust control size and, hence, associated column, row, and frame size. A
control can occupy more than one column or row. The number of columns
spanned by a control is set with the gridwidth variable; the number of rows
spanned is set with the gridheight variable. By default, a control fills one row and
one column. If we have a GridBagConstraints object named gridConstraints, a
control will occupy two rows and three columns, starting in the second column
(gridx = 1) and fourth row (gridy = 3), with this code:
gridConstraints.gridx = 1;
gridConstraints.gridy = 3;
gridConstraints.gridheight = 2;
gridConstraints.gridwidth = 3;
• A particular control may completely fill its region or may not. If the control is
smaller than its allocated region, its dimensions may be adjusted to fill the region
– use the fill variable. There are four values:
GridBagConstraints.NONE Control is not resized (default value)
GridBagConstraints.HORIZONTAL Control width fills display area.
GridBagConstraints.VERTICAL Control height fills display area.
GridBagConstraints.BOTH Control fills entire display area.
With our example gridConstraints object, a control will grow to fill the region
width using:
gridConstraints.fill = GridBagConstraints.HORIZONTAL;
Control
• Smaller changes in control size can be made using the ipadx and ipady
variables. These determine how much a control size is to be increased beyond
its minimum size (in each direction). To add five pixels to the width and height of
a control using our gridConstraints example:
gridConstraints.ipadx = 5;
gridConstraints.ipady = 5;
• If you choose not to expand a control to fill its area, its position within its allocated
area is set with the anchor variable. There are nine possible values:
gridConstraints.anchor = GridBagConstraints.CENTER;
Control
• If a control completely fills its allocated display area, a border region (free space)
can be established around the control using the Insets object. Four values are
used to define the top, left, bottom and right side margins from the side of the
display area. The default is Insets(0, 0, 0, 0). With our example, if we want 10
pixels of space at the top and bottom, 20 on the left and 30 on the right, we would
use:
This control would look something like this in its grid region:
Control
• Once the gridConstraints are established for a control, it is added to the frame’s
content pane using the add method. If the control is myControl, the code syntax
is:
getContentPane().add(myControl, gridConstraints);
• I think you are starting to see the flexibility available with the GridBagLayout
manager. Remember to establish all grid constraint values before adding a
control to the grid. We will start using some of these new concepts in building our
example applications. You, too, are encouraged to learn these ideas and use
them to “beautify” your GUI interfaces.
• Building an interface is an “art,” not a science. You will see the process involves
lots of trial and error and adjustments. And sometimes, you get results you would
never expect – components may not appear as you wish or may not appear at all!
The bottom line is – once all adjustments are made, your final frame size is
established and we can finally learn how to do the task we started out with –
centering the frame in the screen.
• First, to place a frame (width by height in size) at a horizontal position left and
vertical position top, we use the setBounds method:
All the dimensions are int types and measured in pixels. To center a frame in the
computer screen, we need to know find left and top.
• To find the centering position, we need two things: the dimensions of the frame
(use getWidth and getHeight methods) and the dimensions of the screen. The
dimensions of the screen are held in the frame’s ‘toolkit’. A Dimension object
holds the information we need. To retrieve this object, use:
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
With this, screenSize.width holds the screen width and screenSize.height holds
the screen height. So, the code to center the frame using setBounds is:
This code needs to be after the pack method in the code establishing the frame,
so that proper frame size is used. We’ll use this centering code in every
application built in the remainder of this course.