gpt4 book ai didi

java - 在 TextArea 中显示文件内容

转载 作者:行者123 更新时间:2023-12-02 11:21:13 28 4
gpt4 key购买 nike

好的,我会尽力让这一点尽可能容易理解。

我创建了一个简单的 GUI,其中包含一个“打开”按钮和一个文本区域(以及其他内容,但现在这并不重要)。基本上,我正在创建一种方法,当我单击“打开”按钮时,它会在文本区域中显示文件的内容。

我相信我编写的方法 99% 都是正确的,但我不断收到 NullPointerException,但我不太明白为什么。希望我的代码能够消除对我所问问题的任何困惑,我将对出现异常的代码行进行评论。这是我的代码:

应用程序类(我的 GUI 的基本设置):

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

public class Application extends JFrame {

public OutputPanel outPanel = new OutputPanel();
public BarcodePanel barPanel = new BarcodePanel();
public ButtonPanel btnPanel = new ButtonPanel();
public ReferencePanel refPanel = new ReferencePanel();

public Application() {
super("Book Processor");
this.setLayout(new BorderLayout());

btnPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
outPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
refPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

add(btnPanel, BorderLayout.NORTH);
add(outPanel, BorderLayout.WEST);
add(refPanel,BorderLayout.SOUTH);
}//end constructor

public static void main(String[] args) {
Application frame = new Application();
frame.setSize(1000,500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}//end main

}//end class

下一组代码是我的 ButtonPanel 类(这是我遇到问题的地方)所以我有一个 openFile 方法,当我单击按钮时,它应该显示文件的内容在文本区域中。我再次收到 NullPointerException,但我不明白为什么。这是该类的代码:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Closeable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class ButtonPanel extends JPanel{


JButton btnOpen = new JButton("Open");
OutputPanel outPanel = new OutputPanel();
Scanner input;

public ButtonPanel() {
ButtonHandler handler = new ButtonHandler();

add(btnOpen);
btnOpen.addActionListener(handler);

}//end constructor


/**
* Method for displaying the file onto the textArea.
*
*/
private void openFile() {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);


int result = chooser.showOpenDialog(this);

String fileName;

fileName = chooser.getSelectedFile().getAbsolutePath();

try {
Scanner input = new Scanner(Paths.get(fileName));
StringBuilder sb = new StringBuilder();

while(input.hasNextLine()) {

sb.append(input.nextLine()+ "\n");

}

outPanel.txtOutput.setText(sb.toString());//my issue is with this line right here

} catch (IOException e) {

e.printStackTrace();
}
finally {
input.close();
}

}//end readFile



private class ButtonHandler implements ActionListener{

public void actionPerformed(ActionEvent e) {

if(e.getSource()== btnOpen) {

openFile();

}

}//end actionPerformed


}//end actionlistener


}//end class

这是我的最后一个类,OutputPanel,该类包含 JTextArea:

import java.awt.BorderLayout;

import javax.swing.JPanel;
import javax.swing.JTextArea;

public class OutputPanel extends JPanel{

public JTextArea txtOutput = new JTextArea(20,50);

public OutputPanel() {

add(txtOutput);

}//end constructor

}//end class

如何让textArea显示文件内容?更重要的是,为什么我会遇到此异常以及我可以采取什么措施来解决它?希望这尽可能有意义,我真的很感谢你们提供的所有意见。

最佳答案

您的NullPointerException实际上是由

引起的
} finally {
input.close();
}

因为inputnull(嗯,显然)。这是因为您在 try-catch block 中隐藏了变量

try {
Scanner input = new Scanner(Paths.get(fileName));
//...
} finally {
// This is null because the instance field has not been initialised
input.close();
}

更好的解决方案是使用 try-with-resources statement

try (Scanner input = new Scanner(Paths.get(fileName))) {
StringBuilder sb = new StringBuilder();

while (input.hasNextLine()) {

sb.append(input.nextLine() + "\n");

}

outPanel.txtOutput.setText(sb.toString());//my issue is with this line right here

} catch (IOException e) {
e.printStackTrace();
}

更好的解决方案是利用 JTextArea 读取 Reader 内容的能力,例如...

try (FileReader reader = new FileReader(chooser.getSelectedFile())) {
outPanel.txtOutput.read(reader, fileName);
} catch (IOException e) {
e.printStackTrace();
}

现在,在您问为什么文本区域中没有内容之前,原因是您在 ButtonPanel 类中创建了 OutputPanel 的新实例,该实例没有与您在应用程序中创建并添加到屏幕的实例的关系。

您需要将 OutputPanel 的实例传递给 ButtonPanel(通过构造函数),以便引用匹配。

就个人而言,更好的解决方案是定义一个接口(interface),其中有一个read方法,该方法采用FileOutputPanel 将实现此接口(interface),而ButtonPanel 将需要对此接口(interface) 实例的引用。这可以解耦代码并防止 ButtonPanelOutputPanel 进行不必要的更改 - 因为这实际上不是它的责任 - 这是 OutputPanel 的责任负责加载文件并显示它

关于java - 在 TextArea 中显示文件内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49910463/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com