我正在尝试创建一个语音文本编辑器,但是我只能从JTextArea中读取一次。最后我将读取文本,然后将其保存为文本文档,但是现在我只想使用System.out.print()读取和显示当前文本。这意味着每次我点击工具栏上的打开按钮时,它应该打开一个终端并显示JTextArea中的文本,但它只会执行一次。当我启动它时,我可以键入内容,然后单击“打开”按钮,文本将通过终端显示。但是,如果我退出终端并输入其他内容并再次单击“打开”按钮,则终端不会显示任何文本!任何人帮我解决问题?我昨天工作了6个小时,什么都没有!我已经包含了我编写的CreateTextEdit和TextEditorMain类。
import javax.swing.JEditorPane;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.event.DocumentListener;
import java.io.*;
import javax.swing.JOptionPane;
public class CreateTextEdit extends JFrame implements ActionListener
{
JButton newButton = new JButton("New");
JButton openButton = new JButton("Open");
JButton saveButton = new JButton("Save");
JButton exitButton = new JButton("Exit");
JTextArea textField = new JTextArea();
JFrame frame = new JFrame();
DocumentListener docList;
public void initializeTextEdit()
{
frame.setSize(700, 500);
JPanel pane = new JPanel();
pane.setLayout(new BorderLayout());
pane.setBorder(BorderFactory.createMatteBorder(1,10,1,10, Color.gray));
JToolBar toolBar = new JToolBar();
toolBar.add(newButton);
toolBar.add(openButton);
toolBar.add(saveButton);
toolBar.add(exitButton);
pane.add(toolBar, BorderLayout.PAGE_START);
textField.setLineWrap(true);
textField.getDocument().addDocumentListener(docList);
JScrollPane scrollPane = new JScrollPane(textField);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
pane.add(scrollPane);
frame.add(pane);
frame.setTitle("TextEdit+");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
//CreatePanel panelObj = new CreatePanel();
//frame.add(pane);
//panelObj.add(pane);
//panelObj.add(getPanel());
//panelObj.setTitle("TextEdit+");
//panelObj.setDefaultCloseOperation(EXIT_ON_CLOSE);
//panelObj.setVisible(true);
//panelObj.setSize(700, 500);
//scrollPane.add(textField);
//scrollPane.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
//pane.add(scrollPane, BorderLayout.CENTER);
//pane.add(textField, BorderLayout.CENTER);
}
public void buttonFunctions()
{
newButton.addActionListener(this);
openButton.addActionListener(this);
saveButton.addActionListener(this);
exitButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Object src = e.getSource();
String fileName = "";
String newText = "";
String holder = "";
if (src == newButton)
{
String newTxt = JOptionPane.showInputDialog("Name of new text file: ");
JFileChooser save = new JFileChooser();
save.showSaveDialog(this);
try
{
File textFile = new File("fileName.txt");
textFile.createNewFile();
}
catch (IOException i)
{
i.printStackTrace();
}
frame.setTitle(newTxt);
textField.setText(null);
}
else if (src == openButton)
{
holder = textField.getText();
System.out.print(holder);
String userhome = System.getProperty("user.home");
JFileChooser chooser = new JFileChooser(userhome);
chooser.showOpenDialog(null);
File file = chooser.getSelectedFile();
fileName = file.getName();
System.out.print(file);
if (fileName == null)
{
return;
}
String line = null;
try {
FileReader input = new FileReader(fileName);
BufferedReader bufRead = new BufferedReader(input);
while((line = bufRead.readLine()) != null)
{
newText = newText + line;
}
bufRead.close();
}
catch (ArrayIndexOutOfBoundsException f){
System.out.println("Usage: java ReadFile filename\n");
}
catch (IOException g){
// If another exception is generated, print a stack trace
System.out.println("not found");
g.printStackTrace();
}
textField.setText(null);
textField.setText(newText);
frame.setTitle(fileName);
newText = "";
}
else if (src == saveButton)
{
try {
FileWriter output = new FileWriter(fileName);
BufferedWriter out = new BufferedWriter(output);
out.write(textField.getText());
out.close();
}
catch (ArrayIndexOutOfBoundsException f){
System.out.println("Usage: java ReadFile filename\n");
}
catch (IOException g){
// If another exception is generated, print a stack trace
System.out.println("couldnt write");
g.printStackTrace();
}
}
else if (src == exitButton)
{
int n = JOptionPane.showConfirmDialog(null, "Have you saved your work?","Saving",JOptionPane.YES_NO_OPTION);
if (n == 0)
{
System.exit(0);
}
}
}
void readFile(String fileName)
{
}
}
import javax.swing.JEditorPane;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TextEditorMain extends CreateTextEdit
{
public static void main(String[] args)
{
TextEditorMain TE = new TextEditorMain();
TE.initializeTextEdit();
TE.buttonFunctions();
}
}