Swing Java TXT文件写入Jtextarea
有没有办法在文件末尾消失?我决定在while循环之前附加第一行,以免受\ n的影响,但在这样做时出现了无效...(没有第一个附加,它只是跳过第一行,然后开始在第二行上打印)
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.io.*;
public class notepadMinus implements ActionListener{
//Properties
JFrame theFrame;
JPanel thePanel;
JMenuBar theBar;
JMenu theMenu;
JMenuItem openItem;
JMenuItem saveItem;
JTextArea theArea;
JScrollPane theScroll;
JFileChooser openChoose;
String strLine;
//Methods
public void actionPerformed(ActionEvent evt){
if(evt.getSource() == openItem){
System.out.println("Open File");
int intResult = openChoose.showOpenDialog(openItem);
if(intResult == JFileChooser.APPROVE_OPTION){
System.out.println("File selected");
File selectedFile = openChoose.getSelectedFile();
theArea.selectAll();
theArea.replaceSelection("");
try{
BufferedReader txtfile = new BufferedReader(new FileReader(openChoose.getSelectedFile()));
strLine = txtfile.readLine();
theArea.append(strLine);
while(strLine != null){
strLine = txtfile.readLine();
theArea.append( "\n" + strLine);
}
txtfile.close();
}catch(FileNotFoundException e){
System.out.println("File Not Found");
}catch(IOException e){
System.out.println("Reading Error");
}
//read lines
//put those lines in theArea - append
//System.out.println(openChoose.getSelectedFile());
}else{
System.out.println("Cancelled?");
}
}
}
//Constructor
notepadMinus(){
theFrame = new JFrame("Notepad Minus");
theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thePanel = new JPanel();
thePanel.setLayout(null);
thePanel.setPreferredSize(new Dimension(800, 600));
theBar = new JMenuBar();
//theBar.setSize(600,50);
//theBar.setLocation(100, 50);
//thePanel.add(theBar);
theFrame.setJMenuBar(theBar);
theMenu = new JMenu("File");
theBar.add(theMenu);
openItem = new JMenuItem("Open");
openItem.addActionListener(this);
theMenu.add(openItem);
saveItem = new JMenuItem("Save");
saveItem.addActionListener(this);
theMenu.add(saveItem);
theArea = new JTextArea();
theScroll = new JScrollPane(theArea);
theScroll.setPreferredSize(new Dimension(800, 600));
//theFrame.setContentPane(thePanel);
theFrame.setContentPane(theScroll);
theFrame.pack();
theFrame.setVisible(true);
openChoose = new JFileChooser();
}
//Main Method
public static void main(String[] args){
new notepadMinus();
}
}
在JTextarea中的外观
hello world
hello world
hello world
hello world
null
Is there a way to make the null go away at the end of the file? I decided to append the first line before the while loop so it wouldn't be affected by the \n but in doing so the null appears... (without the first append it just skips the first line and starts printing on the second line)
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.io.*;
public class notepadMinus implements ActionListener{
//Properties
JFrame theFrame;
JPanel thePanel;
JMenuBar theBar;
JMenu theMenu;
JMenuItem openItem;
JMenuItem saveItem;
JTextArea theArea;
JScrollPane theScroll;
JFileChooser openChoose;
String strLine;
//Methods
public void actionPerformed(ActionEvent evt){
if(evt.getSource() == openItem){
System.out.println("Open File");
int intResult = openChoose.showOpenDialog(openItem);
if(intResult == JFileChooser.APPROVE_OPTION){
System.out.println("File selected");
File selectedFile = openChoose.getSelectedFile();
theArea.selectAll();
theArea.replaceSelection("");
try{
BufferedReader txtfile = new BufferedReader(new FileReader(openChoose.getSelectedFile()));
strLine = txtfile.readLine();
theArea.append(strLine);
while(strLine != null){
strLine = txtfile.readLine();
theArea.append( "\n" + strLine);
}
txtfile.close();
}catch(FileNotFoundException e){
System.out.println("File Not Found");
}catch(IOException e){
System.out.println("Reading Error");
}
//read lines
//put those lines in theArea - append
//System.out.println(openChoose.getSelectedFile());
}else{
System.out.println("Cancelled?");
}
}
}
//Constructor
notepadMinus(){
theFrame = new JFrame("Notepad Minus");
theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thePanel = new JPanel();
thePanel.setLayout(null);
thePanel.setPreferredSize(new Dimension(800, 600));
theBar = new JMenuBar();
//theBar.setSize(600,50);
//theBar.setLocation(100, 50);
//thePanel.add(theBar);
theFrame.setJMenuBar(theBar);
theMenu = new JMenu("File");
theBar.add(theMenu);
openItem = new JMenuItem("Open");
openItem.addActionListener(this);
theMenu.add(openItem);
saveItem = new JMenuItem("Save");
saveItem.addActionListener(this);
theMenu.add(saveItem);
theArea = new JTextArea();
theScroll = new JScrollPane(theArea);
theScroll.setPreferredSize(new Dimension(800, 600));
//theFrame.setContentPane(thePanel);
theFrame.setContentPane(theScroll);
theFrame.pack();
theFrame.setVisible(true);
openChoose = new JFileChooser();
}
//Main Method
public static void main(String[] args){
new notepadMinus();
}
}
What it looks like in the JTextArea
hello world
hello world
hello world
hello world
null
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
固定的!
\ n
应在strline
之后:Fixed! the
\n
should be afterstrLine
:或使它变得容易得多
Or make it a lot easier with
解决方案
通过在打印前阅读而更改
为
错误
,您会跳过NULL检查。
您已经在循环之前从
txtfile
中读取一行。您应该在阅读另一张之前先打印它。这个错误还有另一个结果 - 第一行是跳过的。
如果您的文件
作为内容,输出将是
The solution
Change
to
The mistake
By reading just before printing, you are skipping the null check.
You are already reading a line from
txtfile
before the loop. You should print it first before you read another.There is another consequence of this mistake - the first line is skipped.
If your file had
as its contents, the output would be