- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好的,我会尽力让这一点尽可能容易理解。
我创建了一个简单的 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();
}
因为input
是null
(嗯,显然)。这是因为您在 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
方法,该方法采用File
。 OutputPanel
将实现此接口(interface)
,而ButtonPanel
将需要对此接口(interface)
实例的引用。这可以解耦代码并防止 ButtonPanel
对 OutputPanel
进行不必要的更改 - 因为这实际上不是它的责任 - 这是 OutputPanel
的责任负责加载文件并显示它
关于java - 在 TextArea 中显示文件内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49910463/
我知道如何在简单的html文本框中删除文本,但是html textareas似乎要复杂得多。而不是value属性,而是将文本放置在之间: . 这就是为什么我在制作onFocus和onBlur
这个问题在这里已经有了答案: text from one textarea should get copy to another textarea and original textarea sh
我实现了一个 在 Elm 中,选项卡缩进和取消缩进,而不是将焦点转移到另一个 HTML 元素。效果很好,除了取消缩进有时会导致选择消失!如果我选择第 5 个字符到第 12 个字符,我按 shift-t
是否可以更改 TextArea 的字体?我需要一个固定宽度的字体来显示一些原始数据,这些数据被格式化为在控制台的列中显示。 最佳答案 您可以使用 CSS 来实现,只需为您的应用程序创建一个主题并添加以
换行符或 pharagraph 在 textarea 输出中不起作用?例如,我在 textarea 中为 pharagraph 使用 enter 但在输出中不起作用?我怎样才能做到这一点? $("#s
这个问题在这里已经有了答案: How to calculate the pixel width of a String in JavaFX? (4 个回答) 6年前关闭。 如何计算 TextArea
我正在尝试使用两个不同的 textarea s 显示相同的代码,但有一个母版和另一个副本。在副本textarea ,我想强调更改,类似于 GitHub 在我们进行一些更改时提供的内容。 我想知道这是否
我需要允许用户突出显示文本(用鼠标选择一个范围),然后我想让他们能够通过下拉右键菜单将某些设置应用于该文本。 我知道后半部分。但是如何获取从 JavafX 的文本区域中选择的文本字符串? 另外,我可以
我使用 TinyMCE。 如果我在我的 textarea(编辑器)中插入另一个 texarea,tinymce 认为这个的结束标记是相关的,所以他关闭了编辑器。所有代码都可以在编辑器之外找到... 你
假设我有两个文本区域... 文本区域 1 文本区域 2 Hi There. 我希望能够将我在文本区域一中输入的文本添加到文本区域二中的文本之后。例如:如果我写“我的名字是乔”。在文本区域一中,它将同
我通过点击一个按钮来实现这个 Bootstrap Modal。但是在点击按钮时,模态出现在屏幕上,除了文本区域字段之外的每个字段都正确显示,按钮代码和文件上传代码在它下面,正在进入文本区域,为什么会这
现在我在获取值时遇到了问题 ~ 我正在执行一个功能,当我单击一个按钮时,它会显示一个模式,并且在其中有一个文本区域。将所有内容写入其中后,单击“确定”按钮,它将使用您编写的文本创建一个新的文本区域。你
我有一个呈现文本区域字段的 HTML 模板和 CSS,但是当单击该字段时,光标从文本区域的中间开始,而不是我期望的从左上角开始。 这不会出现在 IE 中,但会出现在 Chrome 和 FF 中。我还得
我有一个在页面上运行的 TinyMCE 实例,它使我能够编辑现有帖子。我查询数据库,并填充变量 $content与存储的文本。然后我有以下 HTML: Content: " > TinyMCE 显示为
每当有人在 textarea 中按 Enter 键时,我都会调用一个函数.现在我想禁用 new line或 break按下回车键时。 所以new line按下 shift+enter 时应该可以工作。
我有一个带有一些 TextArea 元素的 GUI 来显示信息。由于 GUI 将对 keyevents 使用react,因此我在场景元素中添加了一个 EventHandler。因为我不想为每个文本区域
我想用 jquery 在文本区域中插入图像。 (我知道 img 标签无法插入到文本区域中)。我正在使用tinymce jquery: tinyMCE.init({ mod
为什么这行不通?这里的input类型是text: var name = $("input[name='Event[name]']").serializeArray(); name = name[0].
我正在研究一种解决方案,当您在 TextArea 中键入文本时,它应该通知用户还剩下多少字符需要输入。比如你只能输入200之类的。这很容易制作,但我希望计数器显示在 TextArea 的背景中而不是单
“文本区域类型属性”有什么用? W3foos says this: The type property returns which type of form element a text area i
我是一名优秀的程序员,十分优秀!