我一直在学习如何用Java创建文本编辑器(Java新手)和我想要包含一个按钮,让用户可以单独创建一个新的文本文件窗口,就像你可以在记事本或TextEdit中创建一个新的文本文件一样。但我不确定这是怎么做到的?
到目前为止,我已经为"新文件"创建了一个动作。选项和我希望此选项只需单击第一个文本编辑器中的按钮即可打开另一个文本编辑器窗口。这就是我所坚持的。我读到使用ProcessBuilder是可行的方法,但我认为我没有正确使用它(请参阅Action中的代码块" New")。
更一般地说,我想从另一个已经运行的程序中调用一个程序,只是在这种情况下它们都属于同一个类。
编辑:我不是指使用IO流保存文件。我只是坚持如何从另一个已经运行的程序调用程序,我想通过点击GUI上的按钮来调用另一个程序。
import javax.swing.*;
import javax.swing.text.DefaultEditorKit;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class textEditor {
private JFrame window = new JFrame("Text editor");
private JTextArea area = new JTextArea();
private JFileChooser dialog = new JFileChooser(System.getProperty("user.dir")); // user.dir gets user's working directory
private String currentFile = "Untitled";
private boolean changed = false;
public static int numPrograms = 1;
public static void main(String[] args){
textEditor tester = new textEditor();
}
public textEditor(){
// Set up JFrame
window.setTitle(currentFile);
window.setSize(500, 600);
window.setVisible(true);
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// Set up the text editing area
area.setFont(new Font("Serif", Font.PLAIN, 16));
area.setLineWrap(true);
area.setWrapStyleWord(true);
JScrollPane jsp = new JScrollPane(area);
window.add(jsp, BorderLayout.CENTER);
// Menu bar
JMenuBar JMB = new JMenuBar();
window.setJMenuBar(JMB);
JMenu file = new JMenu("File");
JMenu edit = new JMenu("Edit");
JMB.add(file);
JMB.add(edit);
file.add(New); // TODO add the New action to the file menu
file.add(Open);
file.add(Save);
file.add(SaveAs);
file.addSeparator();
file.add(Exit);
// Stops the user from trying to save the text that's exactly the same as that
Save.setEnabled(false);
SaveAs.setEnabled(false);
// Stops the images specified in the Actions from showing up in the file menu
for(int i=0; i<3; i++)
file.getItem(i).setIcon(null);
file.getItem(4).setIcon(null);
edit.add(Cut);
edit.add(Copy);
edit.add(Paste);
edit.getItem(0).setText("Cut");
edit.getItem(1).setText("Copy");
edit.getItem(2).setText("Paste");
// Toolbar
JToolBar tool = new JToolBar();
tool.setFloatable(false); // Prevents the user from being able to take out the toolbar as a separate window
window.add(tool, BorderLayout.NORTH);
//tool.add(New);
tool.add(Open);
tool.add(Save);
tool.addSeparator();
// Adds cut, copy and paste buttons to the JToolBar
JButton cut = tool.add(Cut), cop = tool.add(Copy), pas = tool.add(Paste);
cut.setText(null); cut.setIcon(new ImageIcon("cut.gif"));
cop.setText(null); cop.setIcon(new ImageIcon("copy.gif"));
pas.setText(null); pas.setIcon(new ImageIcon("paste.gif"));
area.addKeyListener(new KeyAdapter() {
@Override
// If a key is pressed, allow the user to save the document and note that the document has changed
public void keyPressed(KeyEvent e) {
changed = true;
Save.setEnabled(true);
SaveAs.setEnabled(true);
}
});
}
private void saveFileAs() {
if(dialog.showSaveDialog(null)==JFileChooser.APPROVE_OPTION)
saveFile(dialog.getSelectedFile().getAbsolutePath());
}
private int saveOld() {
int saveOldNum = JOptionPane.CANCEL_OPTION;
if(changed) {
saveOldNum = JOptionPane.showConfirmDialog(window, "Would you like to save "+ currentFile +" ?",
"Save", JOptionPane.YES_NO_CANCEL_OPTION);
if(saveOldNum == JOptionPane.YES_OPTION)
if(currentFile.equals("Untitled"))
saveFileAs();
else
saveFile(currentFile);
}
return saveOldNum;
}
private void readInFile(String fileName) {
try {
FileReader r = new FileReader(fileName);
area.read(r,null); // loads in data as plain text
r.close();
currentFile = fileName;
window.setTitle(currentFile);
changed = false;
}
catch(IOException e) {
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(window,"Editor can't find the file called " + fileName);
}
}
private void saveFile(String fileName) {
try {
FileWriter w = new FileWriter(fileName);
area.write(w); // stores only plain text
w.close();
currentFile = fileName;
window.setTitle(currentFile);
changed = false;
Save.setEnabled(false);
}
catch(IOException e) {
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(window, "Cannot save file to " + fileName);
}
}
// ACTIONS
// New
Action New = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if(saveOld() != JOptionPane.CANCEL_OPTION){
Process process = new ProcessBuilder("textEditor.java").start();
}
}
};
// Open
Action Open = new AbstractAction("Open", new ImageIcon("open.gif")) {
@Override
public void actionPerformed(ActionEvent e) {
if(saveOld() != JOptionPane.CANCEL_OPTION && dialog.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)
readInFile(dialog.getSelectedFile().getAbsolutePath());
SaveAs.setEnabled(true);
}
};
// Save
Action Save = new AbstractAction("Save", new ImageIcon("save.gif")) {
@Override
public void actionPerformed(ActionEvent e) {
if(currentFile.equals("Untitled"))
saveFileAs();
else
saveFile(currentFile);
}
};
// Save as
Action SaveAs = new AbstractAction("Save as...") {
@Override
public void actionPerformed(ActionEvent e) {
saveFileAs();
}
};
// Exit
Action Exit = new AbstractAction("Exit") {
@Override
public void actionPerformed(ActionEvent e) {
if(saveOld() != JOptionPane.CANCEL_OPTION)
System.exit(0);
}
};
ActionMap m = area.getActionMap();
Action Cut = m.get(DefaultEditorKit.cutAction);
Action Copy = m.get(DefaultEditorKit.copyAction);
Action Paste = m.get(DefaultEditorKit.pasteAction);
}
答案 0 :(得分:1)
以下是您的回答,但下次您需要解释更多您希望我们做的事情时,这不仅仅是帮助我为我的会话构建代码。 希望这有助于任何其他需要帮助尝试在这里他们对Java有很大的帮助。 http://www.mkyong.com/java/how-to-write-to-file-in-java-bufferedwriter-example/
package com.mkyong;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class WriteToFileExample {
public static void main(String[] args) {
try {
String content = "This is the content to write into file";
File file = new File("/users/mkyong/filename.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
}