Java Reference
In-Depth Information
FIGURE 9.1
Displaying a
frame.
The SimpleFrame application isn't much to look at—the graphical user interface contains
no components, aside from the standard Minimize, Maximize, and Close (X) buttons on
the title bar shown in Figure 9.1. You will add components later today.
In the application, a SimpleFrame object is created in the main() method in lines 11-13.
If you had not displayed the frame when it was constructed, you could call
sf.setVisible(true) in the main() method to display the frame.
The work involved in creating the frame's user interface takes place in the
SimpleFrame() constructor. Components could be created and added to the frame within
this constructor.
Creating a window using JWindow is similar to working with frames in Swing, but you
can't provide a title or close a window.
Listing 9.2 contains an application that creates and opens a window, changing its size
from 0
×
0 to 400
×
400 before your eyes.
LISTING 9.2
The Full Text of SimpleWindow.java
1: import javax.swing.JWindow;
2:
3: public class SimpleWindow extends JWindow {
4: public SimpleWindow() {
5: super();
6: setBounds(400, 300, 10, 10);
7: setVisible(true);
8: }
9:
10: public static void main(String[] arguments) {
11: SimpleWindow sw = new SimpleWindow();
12: for (int i = 10; i < 400; i++) {
13: sw.setBounds(400 - (i/2), 300 - (i/2), i, i);
14: }
15: }
16: }
In the application, the call to setBounds(400, 300, 10, 10) in line 6 of Listing 9.2
sets the window to be 10
×
10 in size and displayed with its upper-left corner at the (x,y)
position 400, 300.
 
Search WWH ::




Custom Search