我正在尝试用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!!!
}
}
答案 0 :(得分:1)
一种解决方法是更改窗口关闭的默认行为,并添加一个捕获窗口关闭事件的WindowListener并在那里保存。
可以在DisplayPanel类中添加一个简单示例(在创建jFrame对象之后):
theFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
theFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
String[] lines = theText.getText().split("\\n");
try (BufferedWriter writer = new BufferedWriter(new FileWriter("newfile.txt"))) {
for (String line : lines)
writer.write(line + "\n");
} catch (IOException i) {
System.out.println("Cannot write file...");
}
System.out.println("File saved!");
System.exit(0);
}
});
以上代码将在关闭窗口时将更改后的文本保存到文件newfile.txt
中。
在上面的示例中,可能不需要拆分成几行;您可能只通过执行writer.write(theText.getText());
就可以得到正确的输出。主要的收获应该是使用WindowAdapter。
一些相关文档:
答案 1 :(得分:1)
以下是使用JButton
来触发事件保存文本文件的示例。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.io.*;
public class DisplayPanel {
public static String textFilePath = // adjust path as needed
"C:\\Users\\Andrew\\Documents\\junk.txt";
private JComponent ui = null;
private JFrame frame;
private JTextArea theText;
private JButton saveButton;
private ActionListener actionListener;
File file;
DisplayPanel(File file) {
this.file = file;
try {
initUI();
} catch (IOException ex) {
ex.printStackTrace();
}
}
private void saveText() {
Writer writer = null;
try {
writer = new FileWriter(file);
theText.write(writer);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public final void initUI() throws FileNotFoundException, IOException {
if (ui != null) {
return;
}
ui = new JPanel(new BorderLayout(4, 4));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));
theText = new JTextArea(20, 120); //120 monospaced chrs
theText.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));
theText.setLineWrap(true);
theText.setEditable(true);
JScrollPane scroll = new JScrollPane(theText);
scroll.setVerticalScrollBarPolicy(
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
ui.add(scroll);
saveButton = new JButton("Save");
ui.add(saveButton, BorderLayout.PAGE_START);
actionListener = (ActionEvent e) -> {
saveText();
};
saveButton.addActionListener(actionListener);
Reader reader = new FileReader(file);
theText.read(reader, file);
}
public void createAndShowGUI() {
frame = new JFrame(this.getClass().getSimpleName());
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setContentPane(getUI());
frame.pack();
frame.setMinimumSize(frame.getSize());
frame.setVisible(true);
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = () -> {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
File file = new File(textFilePath);
DisplayPanel o = new DisplayPanel(file);
o.createAndShowGUI();
};
SwingUtilities.invokeLater(r);
}
}