/* * $Id: LayoutDemo.java,v 1.14 2005/10/31 13:44:37 zfq Exp $ * * Copyright (C) 2001-2003 Extreme Component, Inc. All rights reserved. * Use is subject to license terms. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.event.*; import java.net.URL; import com.zfqjava.swing.CellLayout; import com.zfqjava.swing.BoxLayout2; import com.zfqjava.swing.JGridPane; import com.zfqjava.swing.JCardPane; import com.zfqjava.layout.*; import com.zfqjava.swing.AnchorLayout; import com.zfqjava.swing.JBean; /** * CellLayout Demo * * @author $Author: zfq $ * @version $Revision: 1.14 $ $Date: 2005/10/31 13:44:37 $ */ public class LayoutDemo extends JPanel { private CellLayout cellLayout; public LayoutDemo() { setLayout(new BorderLayout()); add(createLayoutPanel(), BorderLayout.CENTER); add(createEastPanel(), BorderLayout.EAST); } private JTabbedPane createLayoutPanel() { JTabbedPane pane = new JTabbedPane(); pane.addTab("CellLayout", createCellLayoutPanel()); pane.addTab("BoxLayout2 Demo", createBoxLayoutPanel()); pane.addTab("GridLayout2 Demo", createGridLayoutPanel()); pane.addTab("CardLayout2 Demo", createCardLayoutPanel()); pane.addTab("AnchorLayout Demo", createLayoutLayoutPanel()); return pane; } private JPanel createCellLayoutPanel() { JPanel p = new JPanel(new BorderLayout(5, 0)); final JCardPane pane = new JCardPane(); java.util.Vector l = new java.util.Vector(); l.add("simple layout"); pane.add(createPanel0()); l.add("compose mail layout"); pane.add(createPanel1()); l.add("chat room layout"); pane.add(createPanel2()); l.add("option layout"); pane.add(createPanel3()); final JList list = new JList(l); list.setSelectedIndex(0); pane.setSelectedIndex(0); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { if(!(e.getValueIsAdjusting())) { pane.setSelectedIndex(list.getSelectedIndex()); } } }); p.add(new JScrollPane(list), BorderLayout.WEST); p.add(pane, BorderLayout.CENTER); return p; } private JPanel createPanel0() { CellLayout cellLayout = new CellLayout(7, 5, 6, 5); cellLayout.setColumnWeight(1, 1.0); JPanel panel = new JPanel(cellLayout); for(int i=0; i<6; i++) { JLabel label = new JLabel("hello"+i); panel.add(label, new CellLayout.Constraints(0, i)); } for(int i=0; i<6; i++) { JTextField field = new JTextField("world"+i, 9); field.setAlignmentX(0.0f); panel.add(field, new CellLayout.Constraints(1, i)); } JLabel label = new JLabel("hello6"); label.setAlignmentY(0.0f); panel.add(label, new CellLayout.Constraints(0, 6)); JScrollPane scrollPane = new JScrollPane(new JList()); scrollPane.setAlignmentX(0.0f); panel.add(scrollPane, new CellLayout.Constraints(1, 6)); return panel; } private JPanel createPanel1() { CellLayout cellLayout = new CellLayout(4, 2, 6, 5); cellLayout.setColumnWeight(1, 1.0); cellLayout.setRowWeight(3, 1.0); JPanel panel = new JPanel(cellLayout); panel.add(new JLabel("To:"), new CellLayout.Constraints(0, 0)); panel.add(new JTextField(20), new CellLayout.Constraints(1, 0, FillMode.WIDTH)); panel.add(new JLabel("Cc:"), new CellLayout.Constraints(0, 1)); panel.add(new JTextField(20), new CellLayout.Constraints(1, 1, FillMode.WIDTH)); panel.add(new JLabel("Subject:"), new CellLayout.Constraints(0, 2)); panel.add(new JTextField(20), new CellLayout.Constraints(1, 2, FillMode.WIDTH)); panel.add(new JScrollPane(new JTextArea(15, 20)), new CellLayout.Constraints(0, 3, 2, 1, FillMode.BOTH)); return panel; } private JPanel createPanel2() { CellLayout cellLayout = new CellLayout(3, 2, 5, 5); cellLayout.setColumnWeight(0, 1.0); cellLayout.setRowWeight(1, 1.0); JPanel panel = new JPanel(cellLayout); panel.add(new JLabel("Chat Room: xyz"), new CellLayout.Constraints(0, 0)); panel.add(new JScrollPane(new JTextArea(15, 20)), new CellLayout.Constraints(0, 1, 2, 1, FillMode.BOTH)); panel.add(new JTextField(20), new CellLayout.Constraints(0, 2, FillMode.BOTH)); panel.add(new JButton("Send"), new CellLayout.Constraints(1, 2)); return panel; } private JPanel createPanel3() { CellLayout cellLayout = new CellLayout(4, 1, 6, 6); cellLayout.setColumnWeight(0, 1.0); JPanel panel = new JPanel(cellLayout); JPanel option1 = new JPanel(); option1.setBorder(BorderFactory.createTitledBorder("option1")); option1.add(new JLabel("hello")); panel.add(option1, new CellLayout.Constraints(0, 0, FillMode.WIDTH)); JPanel option2 = new JPanel(); option2.setBorder(BorderFactory.createTitledBorder("option2")); option2.add(new JLabel("hello")); panel.add(option2, new CellLayout.Constraints(0, 1, FillMode.WIDTH)); JPanel option3 = new JPanel(); option3.setBorder(BorderFactory.createTitledBorder("option3")); option3.add(new JLabel("hello")); panel.add(option3, new CellLayout.Constraints(0, 2, FillMode.WIDTH)); JPanel option4 = new JPanel(); option4.setBorder(BorderFactory.createTitledBorder("option4")); option4.add(new JLabel("hello")); panel.add(option4, new CellLayout.Constraints(0, 3, FillMode.WIDTH)); return panel; } private static int H_STRUT = 5; private static int V_STRUT = 5; private JPanel createBoxLayoutPanel() { JPanel panel = new JPanel(); panel.setLayout(new BoxLayout2(panel, BoxLayout.Y_AXIS, true, false)); JPanel boxPanel; boxPanel = new JPanel(); boxPanel.setLayout(new BoxLayout2(boxPanel, BoxLayout.X_AXIS)); boxPanel.setBorder(BorderFactory.createTitledBorder("X_AXIS left align")); boxPanel.add(new JButton("ok")); boxPanel.add(Box.createHorizontalStrut(H_STRUT)); boxPanel.add(new JButton("apply")); boxPanel.add(Box.createHorizontalStrut(H_STRUT)); boxPanel.add(new JButton("cancel")); boxPanel.add(Box.createGlue()); panel.add(boxPanel); boxPanel = new JPanel(); boxPanel.setLayout(new BoxLayout2(boxPanel, BoxLayout.X_AXIS)); boxPanel.setBorder(BorderFactory.createTitledBorder("X_AXIS center align")); boxPanel.add(Box.createGlue()); boxPanel.add(new JButton("ok")); boxPanel.add(Box.createHorizontalStrut(H_STRUT)); boxPanel.add(new JButton("apply")); boxPanel.add(Box.createHorizontalStrut(H_STRUT)); boxPanel.add(new JButton("cancel")); boxPanel.add(Box.createGlue()); panel.add(boxPanel); boxPanel = new JPanel(); boxPanel.setLayout(new BoxLayout2(boxPanel, BoxLayout.X_AXIS)); boxPanel.setBorder(BorderFactory.createTitledBorder("X_AXIS right align")); boxPanel.add(Box.createGlue()); boxPanel.add(new JButton("ok")); boxPanel.add(Box.createHorizontalStrut(H_STRUT)); boxPanel.add(new JButton("apply")); boxPanel.add(Box.createHorizontalStrut(H_STRUT)); boxPanel.add(new JButton("cancel")); panel.add(boxPanel); boxPanel = new JPanel(); boxPanel.setLayout(new BoxLayout2(boxPanel, BoxLayout.X_AXIS)); boxPanel.setBorder(BorderFactory.createTitledBorder("X_AXIS separate")); boxPanel.add(new JButton("help")); boxPanel.add(Box.createGlue()); boxPanel.add(new JButton("ok")); boxPanel.add(Box.createHorizontalStrut(H_STRUT)); boxPanel.add(new JButton("apply")); boxPanel.add(Box.createHorizontalStrut(H_STRUT)); boxPanel.add(new JButton("cancel")); panel.add(boxPanel); JPanel p = new JPanel(); p.setLayout(new BorderLayout()); boxPanel = new JPanel(); boxPanel.setLayout(new BoxLayout2(boxPanel, BoxLayout.Y_AXIS)); boxPanel.setBorder(BorderFactory.createTitledBorder("Y_AXIS top align")); boxPanel.add(new JButton("ok")); boxPanel.add(Box.createVerticalStrut(V_STRUT)); boxPanel.add(new JButton("apply")); boxPanel.add(Box.createVerticalStrut(V_STRUT)); boxPanel.add(new JButton("cancel")); boxPanel.add(Box.createVerticalStrut(30)); p.add(boxPanel, BorderLayout.WEST); boxPanel = new JPanel(); boxPanel.setLayout(new BoxLayout2(boxPanel, BoxLayout.Y_AXIS)); boxPanel.setBorder(BorderFactory.createTitledBorder("Y_AXIS center align")); boxPanel.add(Box.createGlue()); boxPanel.add(new JButton("ok")); boxPanel.add(Box.createVerticalStrut(V_STRUT)); boxPanel.add(new JButton("apply")); boxPanel.add(Box.createVerticalStrut(V_STRUT)); boxPanel.add(new JButton("cancel")); boxPanel.add(Box.createGlue()); for(int i=0; i