Java Reference
In-Depth Information
S
ELF
C
HECK
18.
How many Java source files are required by the investment viewer
application when we use inheritance to define the frame class?
19.
Why does the
InvestmentFrame
constructor call
setSize(FRAME_WIDTH, FRAME_HEIGHT)
, whereas the
main
method of the investment viewer class in
Chapter 9
called
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT)
?
A
DVANCED
T
OPIC
10.8: Adding the
main
Method to
the Frame Class
Have another look at the
InvestmentFrame
and
InvestmentViewer2
classes. Some programmers prefer to combine these two classes, by adding the
main
method to the frame class:
public class InvestmentFrame extends JFrame
{
public static void main(String[] args)
{
JFrame frame = new InvestmentFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE
frame.setVisible(true);
}
public InvestmentFrame()
{
account = new BankAccount(INITIAL_BALANCE);
//
Use instance fields for components
label = new JLabel(Ðbalance: Ñ +
account.getBalance());
//
Use helper methods
createButton();
createPanel();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
. . .
}
479
480