【问题标题】:Creating an undefined amount of Jlabels in Java在 Java 中创建未定义数量的 Jlabel
【发布时间】:2015-12-03 01:39:56
【问题描述】:

我正在使用 Java swing 编写一个 Java 程序。我包括一个搜索功能,这个搜索的结果保存在一个 ArrayList 字符串中。我需要显示这些结果,但作为搜索,不可能知道数组中保存的项目数量。那么如何创建和添加未知数量的 Jlabel?

提前致谢。

【问题讨论】:

  • 与您存储要查找的字符串的方式相同吗?
  • 问题是当我已经有了搜索结果的时候。所以现在我试图在 jframe 中显示搜索返回的内容
  • 我建议使用JList
  • 我强烈支持@Jonah 关于您使用 JList 的建议。它不仅可以简化事情,而且还可以让您的程序使用更少的资源,因此可能会响应更快一些。如果您向我们展示相关代码,最好是您的minimal reproducible example(请阅读链接),我们可能会向您展示有用的代码。
  • @HovercraftFullOfEels,谢谢你的回答,这正是我需要的,非常感谢。

标签: java swing jlabel


【解决方案1】:

你也可以这样想,

// you already have this searched result's arraylist
ArralyList<String> allsearchresult = new ArrayList<String>();

JLabel ll = null;
for(String label : allsearchresult ){
   ll = new JLabel(label);
   frame.add(ll); // it's my idea, it can be change according you req.
   // set other necessary requirements...
}

【讨论】:

  • JFrame 的默认 LayoutManager 是 BorderLaqyout。将多个项目添加到框架最终只会显示一个项目。最好将 JPanel 添加到框架中,然后按照@Jonah 的建议在面板中使用 JList。
【解决方案2】:

如前所述,在 JList 中显示您的图像是为了方便和选择。

例如:

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

@SuppressWarnings("serial")
public class ImageList extends JPanel {
    private static final String BASE_PATH = "http://file.kelleybluebookimages.com/"
            + "kbb/images/content/editorial/";
    private static final String[] PATHS = {
        "2015-acura-tlx-guide-180.jpg",
        "13A420TFSI_01_hrgb-180.jpg",
        "CT_071713_BMW320i_0439-180.jpg",
        "2013-Cadillac-ATS-137-180.jpg",
        "EJ2V1342-180.jpg",
        "2014LexusIS005-180.jpg",
        "2014-volvo-s60-180.jpg",
        "2015-jeep-renegade-profile-180.jpg"
    };
    private DefaultListModel<Icon> listModel = new DefaultListModel<>();
    private JList<Icon> imageJList = new JList<>(listModel);

    public ImageList() throws IOException {
        for (String path : PATHS) {
            String imgPath = BASE_PATH + path;
            URL url = new URL(imgPath);
            BufferedImage img = ImageIO.read(url);
            listModel.addElement(new ImageIcon(img));
        }

        imageJList.setVisibleRowCount(4);
        JScrollPane scrollPane = new JScrollPane(imageJList);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        add(scrollPane);
    }    

    private static void createAndShowGui() {
        JFrame frame = new JFrame("ImageList");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        try {
            frame.getContentPane().add(new ImageList());
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        }
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-03
    相关资源
    最近更新 更多