java - 将框架一分为三

标签 java swing user-interface layout-manager

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

  • 有一个由 BorderLayout 管理的主框架。
  • 它的西部是一个带有 3 x 2 按钮网格布局的面板。
  • 中心部分是一个面板。

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

enter image description here

最佳答案

In the center part is a 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);
    }
}

关于java - 将框架一分为三,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43700072/

相关文章:

java - 我的涉及 JFrame 的代码有什么问题

java - jProgressBar 带有用于数据库数据插入的线程

java - 如何在 JPanel 上添加选项卡

java - 灯笼终端输入不断重复

java - springrabbitmq和UI层或托管bean

java - Struts2 getters和setters,intellij说他们没有被使用

android - 如何开发一个好的触摸GUI?

Java GUI 功能 - 子类问题?

c++ - Qt:创建一个滚动条,后面有一个阴影,它包含最后一个值

Java/Android 使用一个 obj 指针来引用 2 个类