swing佈局詳解(附示例圖)
- public static void addComponentsToPane(Container pane) {。。。}
- /**
- *createAndShowGUI()實例化一個JFrame,通過它的ContentPane加載佈局邏輯內容。
- */
- private static void createAndShowGUI() {
- // Create and set up the window.
- JFrame frame = new JFrame("FlowLayoutDemo");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- // Set up the content pane.
- addComponentsToPane(frame.getContentPane());
- // Display the window.
- frame.pack();
- frame.setVisible(true);
- }
- //main()程序入口,單獨起一個線程,實例化UI。
- public static void main(String[] args) {
- javax.swing.SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- createAndShowGUI();
- }
- });
- }
- public static void addComponentsToPane(Container pane) {
- pane.setLayout(new FlowLayout());
- pane.add(new JButton("Button 1"));
- pane.add(new JButton("Button 2"));
- pane.add(new JButton("Button 3"));
- pane.add(new JButton("Long-Named Button 4"));
- pane.add(new JButton("5"));
- }
- public static void addComponentsToPane(Container pane) {
- JButton button = new JButton("Button 1 (PAGE_START)");
- pane.add(button, BorderLayout.PAGE_START);
- button = new JButton("Button 2 (CENTER)");
- button.setPreferredSize(new Dimension(200, 100));
- pane.add(button, BorderLayout.CENTER);
- button = new JButton("Button 3 (LINE_START)");
- pane.add(button, BorderLayout.LINE_START);
- button = new JButton("Long-Named Button 4 (PAGE_END)");
- pane.add(button, BorderLayout.PAGE_END);
- button = new JButton("5 (LINE_END)");
- pane.add(button, BorderLayout.LINE_END);
- }
- public static void addComponentsToPane(Container pane) {
- JPanel xPanel = new JPanel();
- xPanel.setLayout(new BoxLayout(xPanel, BoxLayout.X_AXIS));
- addButtons(xPanel);
- JPanel yPanel = new JPanel();
- yPanel.setLayout(new BoxLayout(yPanel, BoxLayout.Y_AXIS));
- addButtons(yPanel);
- pane.add(yPanel, BorderLayout.PAGE_START);
- pane.add(xPanel, BorderLayout.PAGE_END);
- }
- private static void addAButton(String text, Container container) {
- JButton button = new JButton(text);
- button.setAlignmentX(Component.CENTER_ALIGNMENT);
- container.add(button);
- }
- private static void addButtons(Container container) {
- addAButton("Button 1", container);
- addAButton("Button 2", container);
- addAButton("Button 3", container);
- addAButton("Long-Named Button 4", container);
- addAButton("5", container);
- }
- public void addComponentToPane(Container pane) {
- final JPanel contentPanel = new JPanel();
- JPanel controlPanel = new JPanel();
- final CardLayout cardLayout=new CardLayout();;
- pane.setLayout(new BorderLayout());
- pane.add(contentPanel, BorderLayout.CENTER);
- pane.add(controlPanel, BorderLayout.PAGE_END);
- controlPanel.setLayout(new FlowLayout());
- JButton[] b = new JButton[10];
- for (int i = 0; i < 10; i++) {
- b[i] = new JButton("No." + i);
- contentPanel.add(b[i]);
- }
- contentPanel.setLayout(cardLayout);
- JButton nextButton = new JButton("next");
- nextButton.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- cardLayout.next(contentPanel);
- }});
- controlPanel.add(nextButton);
- }
- public static void addComponentsToPane(Container pane) {
- JButton[] buttons = new JButton[9];
- pane.setLayout(new GridLayout(3, 3));
- for (int i = 0; i < buttons.length; i++) {
- buttons[i] = new JButton(i + "");
- pane.add(buttons[i]);
- }
- }
內容面板代碼:
- public static void addComponentsToPane(Container pane) {
- JButton button;
- pane.setLayout(new GridBagLayout());
- GridBagConstraints c = new GridBagConstraints();
- button = new JButton("Button 1");
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridx = 0;
- c.gridy = 0;
- pane.add(button, c);
- button = new JButton("Button 2");
- c.fill = GridBagConstraints.HORIZONTAL;
- c.weightx = 0.5;
- c.gridx = 1;
- c.gridy = 0;
- pane.add(button, c);
- button = new JButton("Button 3");
- c.fill = GridBagConstraints.HORIZONTAL;
- c.weightx = 0.5;
- c.gridx = 2;
- c.gridy = 0;
- pane.add(button, c);
- button = new JButton("Long-Named Button 4");
- c.fill = GridBagConstraints.HORIZONTAL;
- c.ipady = 40; // make this component tall
- c.weightx = 0.0;
- c.gridwidth = 3;
- c.gridx = 0;
- c.gridy = 1;
- pane.add(button, c);
- button = new JButton("5");
- c.fill = GridBagConstraints.HORIZONTAL;
- c.ipady = 0; // reset to default
- c.weighty = 1.0; // request any extra vertical space
- c.anchor = GridBagConstraints.PAGE_END; // bottom of space
- c.insets = new Insets(10, 0, 0, 0); // top padding
- c.gridx = 1; // aligned with button 2
- c.gridwidth = 2; // 2 columns wide
- c.gridy = 2; // third row
- pane.add(button, c);
- }