[英]How to print all the array elements in a JOptionPane?
I have an array which I want to print on a JOptionPane to get a result like this:我有一个数组,我想在 JOptionPane 上打印它以获得如下结果:
with the array being the book list.数组是书单。 I don't know how to
我不知道该怎么做
Following is the code for the array:以下是数组的代码:
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();
}
}
Format your text output using HTML.使用 HTML 格式化您的文本 output。
So your text string might look like:因此,您的文本字符串可能如下所示:
String text = "<html>There are two books of Sub-Genre Fantasy:<br>The Lost Hero<br>Another Book>";
To display in that format, you can use String.format() to create a string template and pass in the data.要以该格式显示,您可以使用 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);
To display multiple books, you can append the book names together and display it as one string.要显示多本书,您可以 append 将书名一起显示为一个字符串。
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);
The type of the second parameter, named message , in all the showMessageDialog methods of class javax.swing.JOptionPane
is Object
which means it can be any class, including Swing components like javax.swing.JList
. The type of the second parameter, named message , in all the showMessageDialog methods of class
javax.swing.JOptionPane
is Object
which means it can be any class, including Swing components like javax.swing.JList
.
Consider the following.考虑以下。
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);
}
}
Running the above code produces the following:运行上面的代码会产生以下结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.