Java Reference
In-Depth Information
JComponent has many subclasses and two or more commonly used ones are JLabel
and JButtons, the usage of which are given in the Java API.
As an example, suppose viewManager is an instantiated object of ViewManager, a Can-
vas3D object has been added, and an createControlPanel method has been invoked. Then,
the code segment in Figure 8 will add a JLabel title that stretches over three columns, a
JButton minus, a JLabel value, and a JButton plus. Specifically, ViewManager will position
the JLabel title in the first row and have it stretch over three columns, and place the other
controls in the second row, in the order of minus, value, and plus.
GettInG optImum WIdth and heIGht
The optimum width and height of the applet, or the width and height without stretching,
can be obtained by using the methods in Figure 9.
These methods are particularly useful when it is used together with the MainFrame
utility class that allows applets to be run as applications. An example code segment is
provided in Figure 10. The lines are from the main method of ViewModelApp, which uses
the MainFrame utility class to enable it to be run as an application. Here, ViewManager
is called using viewModel.viewManager because only the ViewManager object that has
been used to manage the application layout is able to return the correct width and height.
A new instance of ViewManager created in the main method will not be able to return the
correct width and height.
Figure 9. Methods for getting applet size
int viewManager.getOptimumFrameWidth();
int viewManager.getOptimumFrameHeight();
Figure 10. ViewManager and Mainframe
ViewModelApp viewModel = new ViewModelApp();
Frame frame = new MainFrame( viewModel,
viewModel.viewManager.getOptimumFrameHeight() );
Search WWH ::

Custom Search