Java Reference
In-Depth Information
The initial size at which the application window is displayed is determined by the values you pass to the
setBounds() method for the JFrame object. If you want the window to assume a size that just accommod-
ates the components it contains, you can call the pack() method for the JFrame object. Add the following
line immediately before the call to setVisible() :
aWindow.pack();
If you recompile and run the example again, the application window should fit the components and ap-
pear as shown in Figure 17-17 .
FIGURE 17-17
As I've said, you add components to an applet created as a JApplet object in the same way as for a
JFrame application window. You can verify this by adding some buttons to an example of an applet. You
can try out a Font object and add a border to the buttons to brighten them up a bit at the same time.
TRY IT OUT: Adding Buttons to an Applet
You can define the class for an applet displaying buttons as follows:
import javax.swing.JButton;
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;
import java.awt.Font;
import java.awt.Container;
import java.awt.FlowLayout;
public class TryApplet extends JApplet {
@Override
public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createAppletGUI();
}
});
} catch (Exception e) {
System.err.println("Applet GUI creation failed.");
}
}
 
 
Search WWH ::




Custom Search