【问题标题】:How to print all the array elements in a JOptionPane?如何打印 JOptionPane 中的所有数组元素?
【发布时间】:2021-04-07 21:16:33
【问题描述】:

我有一个数组,我想在 JOptionPane 上打印它以获得如下结果:

数组是书单。我不知道该怎么做

以下是数组的代码:

String[] booksOfSubGenre = new String[bookInfo.size()];
        for (int i = 0; i < bookInfo.size(); i++) {
            int j = 0;
            if (bookInfo.get(i).getSubGenre().equals(selectedSubGenreInCombobox)) {
                subGenreCount++;
                System.out.println(subGenreCount);
                booksOfSubGenre[j] = bookInfo.get(i).getBookName();
            }
        }

【问题讨论】:

    标签: java arrays swing joptionpane


    【解决方案1】:

    要以该格式显示,您可以使用 String.format() 创建字符串模板并传入数据。

    String text = String.format("There are %o books of Sub-Genre Fantasy: %s", subGenreCount , bookInfo.get(i).getBookName());
    JOptionPane.showMessageDialog(null, text, "Query Result", JOptionPane.INFORMATION_MESSAGE);
    

    要显示多本书,您可以将书名附加在一起并显示为一个字符串。

        String appendedText = "";
        int subGenreCount = 0;
        String[] booksOfSubGenre = new String[bookInfo.size()];
        for (int i = 0; i < bookInfo.size(); i++) {
            int j = 0;
            if (bookInfo.get(i).getSubGenre().equals(selectedSubGenreInCombobox)) {
                subGenreCount++;
                System.out.println(subGenreCount);
                booksOfSubGenre[j] = bookInfo.get(i).getBookName();
                appendedText += bookInfo.get(i).getBookName() + "\n";
            }
        }
        String text = String.format("There are %o books of Sub-Genre Fantasy: %s", subGenreCount , appendedText);
        JOptionPane.showMessageDialog(null, text, "Query Result", JOptionPane.INFORMATION_MESSAGE);
    

    【讨论】:

    • 此解决方案是一次只显示一本书,还是如消息所示设计为显示两本书?我尝试了代码,但无法获得识别数组并在单个选项窗格中显示数组中的所有值的格式。
    • 此代码用于一次显示一本书。如果要显示多本书,则需要附加文本。当我有空时,我会为您提供显示多本书的答案。
    • 我将为您提供显示多本书的答案 - 我知道如何显示多本书,正如我在答案中所展示的那样。我不明白为什么 OP 想要一次显示一本书的解决方案。作为一个用户,如果有 10 本书要显示,我会因为必须点击 Ok 按钮 10 次而感到恼火。不是一个非常友好的界面。
    【解决方案2】:

    使用 HTML 格式化您的文本输出。

    所以您的文本字符串可能如下所示:

    String text = "<html>There are two books of Sub-Genre Fantasy:<br>The Lost Hero<br>Another Book>";
    

    【讨论】:

      【解决方案3】:

      第二个参数的类型,名为message,在javax.swing.JOptionPane类的所有showMessageDialog方法中都是Object,这意味着它可以是任何类,包括 Swing 组件,例如 javax.swing.JList

      考虑以下内容。

      import java.awt.BorderLayout;
      
      import javax.swing.JLabel;
      import javax.swing.JList;
      import javax.swing.JOptionPane;
      import javax.swing.JPanel;
      
      public class OptsList {
      
          public static void main(String[] args) {
              String[] booksOfSubGenre = new String[]{"The Lost Hero",
                                                      "The Hobbit",
                                                      "A Game of Thrones"};
              int count = booksOfSubGenre.length;
              String subgenre = "Fantasy";
              JPanel panel = new JPanel(new BorderLayout(10, 10));
              String text = String.format("There are %d books of Sub-Genre %s", count, subgenre);
              JLabel label = new JLabel(text);
              panel.add(label, BorderLayout.PAGE_START);
              JList<String> list = new JList<>(booksOfSubGenre);
              panel.add(list, BorderLayout.CENTER);
              JOptionPane.showMessageDialog(null, panel, "Query Result", JOptionPane.INFORMATION_MESSAGE);
          }
      }
      

      运行上面的代码会产生以下结果:

      【讨论】:

        猜你喜欢
        • 2015-01-31
        • 1970-01-01
        • 2013-05-06
        • 2021-06-06
        • 1970-01-01
        • 2017-08-22
        • 2019-04-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多