Java Reference
In-Depth Information
It consists of three parts, one for the account name, one for the total value
of the account, and one for the remaining value. Each part will be represented
by its own label, using a JLabel object. (We could have done the entire line
in one label, but this gives us a few more objects to manipulate.) Since we want
to group the labels together, we create a JPanel , which is a Swing container,
to hold all these objects. We'll also add the JButton object (the variable named
upton ).
A JLabel is a simple Swing object. You can construct an empty one with
new JLabel(); but you can also construct a label with a String as its initial
value, which is more useful. You can later change a label's value with a call to
its setText() method, as you see here from line 117:
117 tot.setText("Total: $"+current.getTotal());
16.7.2.2
The JLabel s are added to their JPanel , but with no position argument, unlike
the JFrame and BorderLayout used in main() . JPanel has a different default
layout manager: It uses FlowLayout . With it, added objects are placed side by
side according to the window size. If the window is narrowed, they will simply
flow onto the next line. (You won't see this behavior if you narrow the Budget-
Pro window, but that's because the JPanel has been added to the JFrame 's
NORTH region, which means it's no longer just a FlowLayout that determines
sizes.) FlowLayout is a layout that's easy to use, but doesn't give you much
control; it was just fine for our purposes here.
FlowLayout
16.7.2.3
Another simple layout mechanism is the BoxLayout . It allows you to place the
objects like stacking boxes—though they can be stacked horizontally as well as
vertically. Look at line 224:
BoxLayout
224 retval.setLayout(new BoxLayout(retval, BoxLayout.X_AXIS));
Here we are creating a BoxLayout object and associating it with our
JFrame to manage its objects. When we create a BoxLayout we can tell it that
we want to stack our objects horizontally (using either X_AXIS or LINE_AXIS )
or vertically (using either Y_AXIS or PAGE_AXIS ). Note that the BoxLayout
Search WWH ::




Custom Search