例8—6 GridLayout示例:
import java.awt.*;
import javax.swing.*;
public class GridLayoutWindow extends JFrame {
public GridLayoutWindow() {
setLayout(new GridLayout(0,2));
add(new JButton("Button 1"));
add(new JButton("2"));
add(new JButton("Button 3"));
add(new JButton("Long-Named Button 4"));
add(new JButton("Button 5"));
add(new JButton("6"));
}
public static void main(String args[]) {
GridLayoutWindow window = new GridLayoutWindow();
window.setTitle("GridWindow Application");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.pack();
window.setVisible(true);
}
}
例8—7 CardLayout示例:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutWindow extends JFrame implements ActionListener {
JPanel cards;
CardLayout CLayout=new CardLayout();
public CardLayoutWindow() {
setLayout(new BorderLayout()); //设置Frame为BorderLayout。
//创建摆放"卡片切换"按钮的panel,并添加到Frame中。
JPanel cp = new JPanel();