(希望)简单的问题,我已经编写了一些代码来显示 JOptionPane 中文本文件中的信息,它可以工作,但它为每一行创建一个新框。
我如何让它在单个 JOptionPane 中显示所有读取的文本,而不是逐框、逐行显示。如果可能的话,不必重写整个事情。
Scanner inFile = null;
try {
inFile = new Scanner(new FileReader( curDir + "/scripts/sysinfo.txt"));
} catch (FileNotFoundException ex) {
Logger.getLogger(DropDownTest.class.getName()).log(Level.SEVERE, null, ex);
}
while(inFile.hasNextLine()){
String line = inFile.nextLine();
JOptionPane.showMessageDialog(null, line, "System Information", JOptionPane.INFORMATION_MESSAGE);
}
我四处寻找类似的问题,但找不到任何适合这个问题的内容,因此决定只问一下,但如果我以某种方式错过了某些内容并且最终导致重复,我会道歉。
感谢您的帮助。
最佳答案
您可以尝试使用 StringBuilder 创建单个大字符串,然后像这样显示此 StringBuilder:
Scanner inFile = null;
StringBuilder builder = new StringBuilder();
try {
inFile = new Scanner(new FileReader(curDir + "/scripts/sysinfo.txt"));
} catch (FileNotFoundException ex) {
Logger.getLogger(DropDownTest.class.getName()).log(Level.SEVERE, null, ex);
}
while(inFile.hasNextLine()){
String line = inFile.nextLine();
builder.append(line);
builder.append("\n"); // add this for newlines
}
JOptionPane.showMessageDialog(null, builder, "System Information", JOptionPane.INFORMATION_MESSAGE);
关于java - 在一个 JOptionPane 中显示所有文本,而不是逐行显示多个文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36588363/