【发布时间】:2018-07-13 10:28:32
【问题描述】:
我正在尝试用 Java 编写一个文本编辑器应用程序。下面的程序读入一个文本文件,然后通过BufferedReader 方法显示它。但是,在这一点上,我完全被卡住了。可以在JFrame 窗口中编辑显示的文本。但编辑后,我不知道如何关闭并保存编辑后的文件(即如何合并事件处理程序,然后保存编辑后的文本)。
我已经尝试了很多事情,但非常感谢有关如何从这一点取得进展的帮助。我对 Java 很陌生,所以我的程序的整个结构可能是错误的 - 非常感谢任何帮助。主程序在这里,然后是它调用的显示面板创建器。程序应该会弹出一个窗口,其中包含您放置在目录中的任何名为 text.txt 的文本文件。
主要:
import java.io.*;
import java.util.ArrayList;
import static java.lang.System.out;
public class testApp4 {
public static void main(String args[]) {
ArrayList<String> listToSend = new ArrayList<String>();
String file = "text.txt";
try (BufferedReader br = new BufferedReader(new FileReader(file)))
{
String line;
while ((line = br.readLine()) != null) {
listToSend.add(line);
}
br.close();
}
catch(FileNotFoundException e)
{
out.println("Cannot find the specified file...");
}
catch(IOException i)
{
out.println("Cannot read file...");
}
new DisplayPanel(listToSend);
}
}
显示面板创建者:
import java.awt.Font;
import java.util.ArrayList;
import javax.swing.JFrame;// javax.swing.*;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
public class DisplayPanel {
public DisplayPanel(ArrayList<String> list) //constructor of the DisplayGuiHelp object that has the list passed to it on creation
{
JTextArea theText = new JTextArea(46,120); //120 monospaced chrs
theText.setFont(new Font("monospaced", Font.PLAIN, 14));
theText.setLineWrap(true);
theText.setEditable(true);
for(String text : list)
{
theText.append(text + "\n"); //append the contents of the array list to the text area
}
JScrollPane scroll = new JScrollPane(theText);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
JPanel mainPanel = new JPanel();
mainPanel.add(scroll);
final JFrame theFrame = new JFrame();
theFrame.setTitle("textTastico");
theFrame.setSize(1100, 1000);
theFrame.setLocation(550, 25);
theFrame.add(mainPanel); //add the panel to the frame
theFrame.setVisible(true);
System.out.print(theText.getText()); //double check output!!!
}
}
【问题讨论】:
-
1)
JTextArea theText = new JTextArea(46,120);见JTextComponent.read(Reader ..)&write(Writer)。 2)“如何合并事件处理程序,然后保存编辑的文本”你指的是什么“事件”?用户点击按钮?文字变了?窗户被关了?爆米花做好后微波炉会“叮”的一声? .. 3) .. -
您也可以使用保存按钮将文本从编辑器保存(写入)到文件中;按钮的事件处理程序具有要保存的代码。此外,读取文件并填充到列表集合的代码可以简洁地编写为(使用 Java 7 或更高版本):
ArrayList<String> listToSend = Files.readAllLines(Paths.get("text.txt")); -
..
new Font("monospaced", Font.PLAIN, 14)最好是new Font(Font.MONOSPACED, Font.PLAIN, 14)用于编译时检查。如果有一个常数,为什么不使用它? 4)theFrame.setSize(1100, 1000); .. theFrame.add(mainPanel);更多猜测。将其更改为theFrame.add(mainPanel); .. theFrame.pack()以在任何平台上获得正确的大小。 5) 但是您可能会注意到,对于文本区域而言,120 个字符变得非常 宽。 ;) -
@prasad_ 似乎
Files.readAllLines(Path)假定为 UTF-8。我在第一条评论中概述的方法正确地解释了文本文件实际使用的任何编码。 -
“用户点击了一个按钮?文本被改变了?窗口被关闭了?” 那么..是什么?我已经编写了涵盖所有这三个的代码,但是它们在一起并没有多大意义(文档侦听器使动作侦听器和窗口侦听器变得多余)。
标签: java swing file-io jframe text-files