三个分裂框架

时间:2017-04-29 20:13:26

标签: java swing user-interface layout-manager

我编写了一个如下所示的GUI。它编码如下:

  • 使用BorderLayout管理主框架。
  • 在它的西部是一个面板,网格布局为3 x 2个按钮。
  • 中间部分是一个面板。

我想添加第三个面板,如图所示。我怎样才能做到这一点?

enter image description here

1 个答案:

答案 0 :(得分:5)

  

中间部分是Panel

该面板可能还有一个BorderLayout,将两个组合框放在PAGE_START的面板中,以及CENTER中的第三个面板。

enter image description here

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.*;

public class ThreePanelLayout {

    private JComponent ui = null;
    private String[] buttonNames = {"Time", "Price", "Route", "Sort", "Admin", "End"};
    private String[][] comboFirstNames = {{"Departing Stop"}, {"Final Stop"}};

    ThreePanelLayout() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        // I always use this 'ui' panel as a content pane that contains
        // everything else..
        ui = new JPanel(new BorderLayout(4,4));
        ui.setBorder(new EmptyBorder(4,4,4,4));

        // now to create the 3 panels of the '3 panel layout'. 
        JPanel panel1 = new JPanel(new BorderLayout());
        panel1.setBackground(Color.RED);
        panel1.setBorder(new TitledBorder("Choose Option"));

        JPanel panel2 = new JPanel(new BorderLayout());
        panel2.setBackground(Color.GREEN);
        panel2.setBorder(new TitledBorder("Choose Two Stops"));

        JPanel panel3 = new JPanel(new BorderLayout());
        panel3.setBackground(Color.ORANGE);
        panel3.setBorder(new TitledBorder("Third Panel Here"));

        // add the buttons to 1st panel
        panel1.add(addButtonsToPanel(buttonNames), BorderLayout.LINE_START);
        // add the combos to the top of 2nd panel 
        panel2.add(addCombosToPanel(comboFirstNames), BorderLayout.PAGE_START);
        // give the 3rd panel some size
        panel3.add(new JLabel(new ImageIcon(new BufferedImage(400,200,BufferedImage.TYPE_INT_ARGB))));

        // now assemble them all together
        panel2.add(panel3, BorderLayout.CENTER);
        panel1.add(panel2, BorderLayout.CENTER);
        ui.add(panel1, BorderLayout.CENTER);
    }

    private JPanel addButtonsToPanel(String[] ids) {
        JPanel p = new JPanel(new GridLayout(0, 2));
        for (String id : ids) {
            p.add(new JButton(id));
        }
        return p;
    }

    private JPanel addCombosToPanel(String[][] ids) {
        JPanel p = new JPanel(new FlowLayout());
        for (String[] id : ids) {
            p.add(new JComboBox<String>(id));
        }
        return p;
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                ThreePanelLayout o = new ThreePanelLayout();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}