Java Reference
In-Depth Information
Listing 8-3. A FlowLayout with Three Buttons Aligned to the Right
import java.awt.*;
import javax.swing.*;
public class MyFrame extends JFrame {
public static void main(String[] args) {
new MyFrame().setVisible(true);
}
public MyFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel thePanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
thePanel.add(new JButton("One"));
thePanel.add(new JButton("Two"));
thePanel.add(new JButton("Three"));
add(thePanel, BorderLayout.CENTER);
pack();
setSize(300, 70);
}
}
Figure 8-10. An example of FlowLayout with components aligned to the right
Tip The combination of the JFrame and JPanel containers is a powerful yet simple way to lay out
graphical components. There are certainly more complex ways to handle interface layout, but the combi-
nation of these two containers will probably be more than enough to lay out the client interface for the
SCJD exam.
Look and Feel
Swing supports a pluggable look and feel. This feature is a welcome side effect of Swing's
lightweight components that can be overwritten with programmer-defined design and func-
tionality. A prime example of this feature is the Ocean look and feel. This interface style can be
used on any platform that supports Java and graphical user interfaces, and it can be used as a
standard interface across all platforms. If an interface absolutely must look and behave the
same on every platform, then the Ocean look and feel is the right choice.
The look and feel of any Swing-based GUI can be changed on the fly programmatically
within the application.
Search WWH ::




Custom Search