我想创建一个程序,允许用户输入文件名,然后它显示在JLabel文件中写入的所有内容,我设法找到/创建代码,允许用户输入文件的名称,然后显示控制台中文件的内容,但我找不到从JLabel中的文本文件中显示所有内容的方法。
有没有办法做到这一点?有些人告诉我,不可能这样做。
这是我目前使用的代码。
public class OpenFile {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter username: ");
String userName = myObj.nextLine(); // Read user input
BufferedReader br = null;
FileReader fr = null;
try {
//br = new BufferedReader(new FileReader(FILENAME));
fr = new FileReader(userName + ".txt");
br = new BufferedReader(fr);
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
是的......这是可能的,但正如已经提到的那样,使用JTextArea或类似的组件会好得多,而且很可能会让自己感到悲伤。
尽管JLabel基本上是针对单个字符串的文本行设计的,但它允许将该文本包装在HTML标记内,因此允许基本的HTML / CSS格式化相同的文本。然后诀窍是将所需文本文件的每一行读取为单个字符串变量格式化该字符串,当您追加每行读取时,通过格式化,我的意思是添加:
JLabel不承认你已经熟悉的常见换行符,如"\n"
或"\r\n"
甚至System.lineSeparator();
。然而,它将处理<br>
的HTML Line Break标记,但前提是应用于JLabel的文本包含在HTML中。以下是两行JLabel文本的示例:
String txt = "<html>This is line one.<br>This is line two.</html>";
jLabel1.setText(txt);
最终你的JLabel看起来像这样:
请注意,在上面的代码行中,String文本以<html>
开头,以</html>
结尾。这两个标记之间的任何文本都被视为包装在HTML中。您还会注意到字符串中的<br>
标记会强制换行以创建两行。
JLabel的功能非常有限,没有HTML,它无法真正执行上面列出的任何子弹,并在JLabel中显示文本文件,如下所示:
您当然会注意到上图中的滚动条。 JLabel的另一个问题是,如果需要,它不会显示Scrollbars。您需要将JLabel放入JScrollPane才能拥有此功能,因为很可能会有超出JLabel边界的文件,因此您还需要考虑这一点。很简单,不是世界末日。
下面提供的方法将读入提供的文本文件,并在提供的JLabel中显示它。它会自动将所有内容包装成HTML,提供标题,左边所有文本按10像素填充,换行文本,处理换行符,并处理基本缩进:
public void displayFileInJLabel(JLabel label, String filePath) {
try {
// Try With Resources (will auto close the reader).
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
/* We use StringBuilder to build our HTML Wrapped
string to display within the JLabel. */
StringBuilder sb = new StringBuilder();
// Get the width of our supplied JLabel
int w = label.getWidth();
/* Calculations for determininfg Line Wrap.
The (w / 4) is a modifiable offset. */
String width = String.valueOf((w - (w / 4)));
/* Deal with Line Wrap (JLabels don't do this) and
set up Left Padding. */
sb.append("<html><body style='width: ").append(width).append("px; padding:10px;'>");
/* Apply the Title Center of JLabel, Blue Color Text,
and Bold Font Style.The size of the text is determined
by the <h1> and </h1> tags. */
sb.append("<center><h1><font color=blue><b>").append(filePath).append("</b></font></h1></center><br>");
// Read in File Lines one at a time.
String line;
while((line = reader.readLine()) != null) {
/* Deal with multiple whitespaces (basic indenting etc) since HTML
doesn't deal well with more than a single whitespace. */
line = line.replaceAll("\\s{4}", " ");
/* Deal with line breaks. JLabels won't deal with them since
it is designed for a single line of text. We therefore
apply the HTML Line Break tag (<br>)at the end of each
text file line to take care of this business. */
line+= "<br>";
sb.append(line);
}
// Apply the closing tags to finish our HTML Wrapping.
sb.append("</body></html>");
/* Set the formated HTML text to the JLabel */
label.setText(sb.toString());
}
}
catch (IOException ex) {
Logger.getLogger("displayFileInJLabel() Method").log(Level.SEVERE, null, ex);
}
}
如果删除所有评论,那么它真的不是那么多代码,但还有更多工作要做。构建上面显示的表单示例
displayFileInJLabel(jLabel1, "C:\\MyFiles\\LoremIpsum.txt");
最好让一个类成员变量保存文件路径以查看而不是硬编码,然后在Form的类构造函数中填充此成员变量,该变量将具有String Type参数。这都是你真正想要做的事情。使用JTextArea仍然是一个更好的主意。