使用 JFrame 中的循环使 JScrollPane 添加垂直对齐的 JPanels?

问题描述 投票:0回答:0

我正在创建一个搜索航班的应用程序,并使用带有航班信息的 JLabel 和一个预订航班的按钮生成 JPanel。我希望我的代码能够生成这个: enter image description here 我已经设法实现了预订功能,但我无法让我的代码垂直添加面板。我的代码改为像这样水平添加它们: enter image description here 这是我的 GUI 代码:

private void generateSearchList(ArrayList<JSONObject> searchResult) {

        searchList = new JPanel(new FlowLayout(FlowLayout.LEFT));

        for (int i = 0; i < searchResult.size(); i++) {
            // pane maker
            JLabel label = new JLabel();
            searchList.add(label);
            label.setText("Flight Number: " + searchResult.get(i).get("ident"));
            searchScroll.setViewportView(searchList);


            // This works....need to fix pane
            bookButton = new JButton();
            // Apply an identifier to the Button:
            bookButton.setName(new StringBuilder("bookButton").append(i).toString());
            bookButton.setText("Book");
            bookButton.addActionListener(clicked); // clicked is ButtonClicked implements ActionListener
            searchList.add(bookButton);
        }

    }

我已经设法让按钮生成器成功工作,但我不知道如何让我的代码垂直显示信息。不用担心 JSONObject 的键,我可以自己计算(您可以将文本显示为字符串)。我只需要帮助才能垂直对齐 JPanel、JLabel、Spacer 和 Button。

swing jframe jscrollpane gui-builder
© www.soinside.com 2019 - 2024. All rights reserved.