[英]Reading a file that has multiple lines and outputting that to a JLabel
我正在从一个包含成千上万行文本的文件中读取内容,我希望程序读取该文件中的所有行并将它们以相同格式输出到JLabel或任何可行的文件上。
public String readSoldTo() {
String file = "/Users/tylerbull/Documents/JUUL/Sold To/soldTo.txt";
String data;
try{
BufferedReader br = new BufferedReader(new FileReader(file));
while ((data = br.readLine()) != null) {
System.out.println(data);
return data;
}
br.close();
}catch(Exception e) {
}
return file;
}
构建JLabel来显示一行文本。 是的,您可以对它进行评审以显示更多内容,但这很麻烦,并且经常会在代码中引起将来的问题。 相反,我建议您在JTextArea中显示文本,并且可以通过使其背景颜色为空来删除其边框来使其看起来像JLabel。 如果将其添加到JScrollPane中,则会看到滚动条(如果适用)和滚动窗格的边框,这可能是一个问题。 我还将再次使JTextArea不可聚焦和不可编辑,以使其表现得更像标签,而不像接受用户交互的文本组件。
此外,像JTextFields一样,JTextComponents也具有允许您向其传递阅读器的机制,以便它可以参与文本文件的读取,并在需要时保留换行符。 有关更多信息,请参见其读取方法API条目 。 与往常一样,请注意遵守Swing线程规则,并在后台线程中执行文本I / O,并在Swing事件线程上进行所有Swing突变调用。
您的代码也让我有些担心:
例如:
import java.awt.BorderLayout;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
@SuppressWarnings("serial")
public class TextAreaAsLabel extends JPanel {
private static final int TA_ROWS = 30;
private static final int TA_COLS = 50;
private static final Font TA_FONT = new Font(Font.DIALOG, Font.BOLD, 12);
private JTextArea textArea = new JTextArea(TA_ROWS, TA_COLS);
public TextAreaAsLabel() {
// JButton and JPanel to open file chooser and get text
JPanel buttonPanel = new JPanel();
buttonPanel.add(new JButton(new ReadTextAction("Read Text")));
// change JTextArea's properties so it "looks" like a multi-lined JLabel
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setBackground(null);
textArea.setBorder(null);
textArea.setFocusable(false);
textArea.setEditable(false);
textArea.setFont(TA_FONT);
// add components to *this* jpanel
setLayout(new BorderLayout());
add(textArea, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.PAGE_END);
}
private class ReadTextAction extends AbstractAction {
public ReadTextAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
// create file chooser and limit it to selecting text files
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Text Files", "txt");
fileChooser.setFileFilter(filter);
fileChooser.setMultiSelectionEnabled(false);
// display it as a dialog
int choice = fileChooser.showOpenDialog(TextAreaAsLabel.this);
if (choice == JFileChooser.APPROVE_OPTION) {
// get file, check if it exists, if it's not a directory
File file = fileChooser.getSelectedFile();
if (file.exists() && !file.isDirectory()) {
// use a reader, pass into text area's read method
try (BufferedReader br = new BufferedReader(new FileReader(file))){
textArea.read(br, "Reading in text file");
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("Foo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new TextAreaAsLabel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.