Java 如何将搜索结果全部放在一个框中?代替(JOptionPane单独的盒子)

Java 如何将搜索结果全部放在一个框中?代替(JOptionPane单独的盒子),java,swing,joptionpane,Java,Swing,Joptionpane,你好,我有一个ArrayList,里面有5部手机,其中2部来自“三星制造” 我如何将它们全部显示在一起?而不是单独的盒子 String conta1 = "Samsung"; for(int i=0;i<PhoneList.size();i++){ if(conta1.equalsIgnoreCase(PhoneList.get(i).getMfg())){ JOptionPane.showMessageDialog(null,PhoneList.get(i));

你好,我有一个
ArrayList
,里面有5部手机,其中2部来自“三星制造” 我如何将它们全部显示在一起?而不是单独的盒子

String conta1 = "Samsung";
for(int i=0;i<PhoneList.size();i++){
    if(conta1.equalsIgnoreCase(PhoneList.get(i).getMfg())){
        JOptionPane.showMessageDialog(null,PhoneList.get(i));
    }
String conta1=“三星”;

对于(int i=0;i您可以使用
JLabels
并将它们添加到
JPanel
,使用您认为合适的任何布局。然后将
JPanel
添加到
JOptionPane
。类似于

JPanel panel = new JPanel(new GridLayout(0, 1);
for(int i=0; i<PhoneList.size(); i++){
if(conta1.equalsIgnoreCase(PhoneList.get(i).getMfg())){
    panel.add(new JLabel(PhoneList.get(i)); // not sure what .get(i) returns
}                                           // but it must be a string passed to the label
JOptionPane.showMessageDialog(null, panel);

请看我答案的最后一部分。我需要int和string混合,谢谢,虽然我创建了一个result arraylist,但现在的问题是它在一行中输出所有内容…如何使它们输出到两行或更多行?电话列表的类/对象是什么?get(I)
返回的?作为
toString()
类中表示字符串表示的方法。调用
PhoneList.get(i).toString()
“如何使它们输出为两行或更多行?”-请参阅我发布的链接。搜索,您将找到答案。要更快地获得更好的帮助,请发布一个(最简单的完整且可验证的示例)。
import java.awt.Color;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.MatteBorder;

public class TwoLineLabel {


    public static void main(String[] args) {
        List<Phone> phones = new ArrayList<>();
        phones.add(new Phone("Galaxy", 12345));
        phones.add(new Phone("iPhone", 12345));

        JPanel panel = new JPanel(new GridLayout(0, 1));
        for (Phone phone: phones) {
            String html = "<html><body style='width:100px'>Phone: " + phone.getName() 
                    + "<br/>Model: " + phone.getModel() + "</body></html>";
            JLabel label = new JLabel(html);
            label.setBorder(new MatteBorder(0, 0, 1, 0, Color.BLACK));
            panel.add(label);

        }
        JOptionPane.showMessageDialog(null, panel, "Phone List", JOptionPane.PLAIN_MESSAGE);
    }

}

class Phone {
    private String name;
    private int model;

    public Phone(String name, int model) {
        this.name = name;
        this.model = model;
    }

    public String getName() {
        return name;
    }

    public int getModel() {
        return model;
    }
}