首页 > 解决方案 > 如何在 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);

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

在此处输入图像描述

当我的目标是看起来更像这样的东西时:

在此处输入图像描述

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

标签: javaswingjpaneljlabellayout-manager

解决方案


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

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


推荐阅读