如何在 Swing 中创建一个包含有组织的信息“列表”的面板

问题描述

我正在尝试创建一个包含信息的面板。

我已尝试使用 setBorderLayout 将带有信息的标签添加到面板的某些区域,但我知道这不起作用,因为它只会覆盖我之前已有的内容

这是我的代码中的一个示例:

        final JFrame fr = new JFrame("ATCGUI");
        fr.setLayout(new BorderLayout());

        JTabbedPane tabbedPane = new JTabbedPane();
        JPanel tab1 = new JPanel();
        tab1.setLayout(new BorderLayout());
        tabbedPane.addTab("Airport",tab1);

        JLabel labelAirportName = new JLabel(airportInfo[0]);
        tab1.add(labelAirportName,BorderLayout.norTH);

        JLabel labelAirportCodeStatic = new JLabel("Code:");
        JLabel labelAirportLocationStatic = new JLabel("Location:");
        JLabel labelAirportCoordinatesstatic = new JLabel("Coordinates:");
        JLabel labelAirportAltitudeStatic = new JLabel("Altitude:");
        JLabel labelAirportTimezonestatic = new JLabel("Timezone:");
        JLabel labelAirportICAOStatic = new JLabel("ICAO:");
        tab1.add(labelAirportCodeStatic,BorderLayout.WEST);
        tab1.add(labelAirportLocationStatic,BorderLayout.WEST);
        tab1.add(labelAirportCoordinatesstatic,BorderLayout.WEST);
        tab1.add(labelAirportAltitudeStatic,BorderLayout.WEST);
        tab1.add(labelAirportTimezonestatic,BorderLayout.WEST);
        tab1.add(labelAirportICAOStatic,BorderLayout.WEST);

这最终会创建这样的东西:

enter image description here

当我瞄准看起来更像这样的东西时:

enter image description here

我用 SwingUI 设计器创建了这个,但由于兼容性,我切换到只使用 Swing

解决方法

遵循由 maloomeister https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html 链接的 Oracle Swing 教程

我能够通过混合 BorderLayout 和 GridLayout 来创建我想要的布局。