Java Reference
In-Depth Information
border.setHgap(20); // Set horizontal gap
This sets the horizontal gap between components to 20 pixels and leaves the vertical gap at the default of
zero. You can also retrieve the current values for the gaps with the getHgap() and getVgap() methods.
Using a Card Layout Manager
The card layout manager generates a stack of components, one on top of the other. The first component that
you add to the container is at the top of the stack, and therefore visible, and the last one is at the bottom. You
can create a CardLayout object with the default constructor, CardLayout() , or you can specify horizontal
and vertical gaps as arguments to the constructor. The gaps in this case are between the edge of the compon-
ent and the boundary of the container. You can see how this works in an applet.
TRY IT OUT: Dealing Components
Because of the way a card layout works, you need a way to interact with the applet to switch from one
component to the next. You will implement this by enabling mouse events to be processed, but I won't
explain the code that does this in detail here. I'm leaving that to the next chapter.
Try the following code:
import javax.swing.JButton;
import javax.swing.*;
import java.applet.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
// Class defining events
import java.awt.event.ActionListener;
// Interface for
receiving events
public class TryCardLayout extends JApplet implements ActionListener {
@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