只需要这个if(e.getSource() == this.newFile)
部分的答案
如果textArea
包含任何文字,则提示“您要将此文件或 DISCARD 保存到NewFile吗?”
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
public class Notepad extends JFrame implements ActionListener {
private TextArea textArea = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY);
private MenuBar menuBar = new MenuBar();
private Menu File = new Menu();
private MenuItem newFile = new MenuItem();
private MenuItem openFile = new MenuItem();
private MenuItem saveFile = new MenuItem();
private MenuItem close = new MenuItem();
Notepad(){
this.setSize(500,300);
this.setTitle("Java Notepad");
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.textArea.setFont(new Font("Century Gothic", Font.BOLD, 12));
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(textArea);
this.setMenuBar(this.menuBar);
this.menuBar.add(File);
this.File.setLabel("File");
this.newFile.setLabel("New");
this.newFile.addActionListener(this);
this.newFile.setShortcut(new MenuShortcut(KeyEvent.VK_N, false));
this.File.add(this.newFile);
this.openFile.setLabel("Open ");
this.openFile.addActionListener(this);
this.openFile.setShortcut(new MenuShortcut(KeyEvent.VK_O, false));
this.File.add(this.openFile);
this.saveFile.setLabel("Save ");
this.saveFile.addActionListener(this);
this.saveFile.setShortcut(new MenuShortcut(KeyEvent.VK_S, false));
this.File.add(this.saveFile);
this.close.setLabel("Close");
this.close.setShortcut(new MenuShortcut(KeyEvent.VK_F4, false));
this.close.addActionListener(this);
this.File.add(this.close);
}
public void actionPerformed (ActionEvent e){
if(e.getSource() == this.close)
this.dispose();
else if(e.getSource() == this.openFile){
JFileChooser open = new JFileChooser();
int option = open.showOpenDialog(this);
if(option == JFileChooser.APPROVE_OPTION){
this.textArea.setText("");
try{
Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath()));
while(scan.hasNext())
this.textArea.append(scan.nextLine()+"\n");
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
}
}
else if(e.getSource() == this.saveFile){
JFileChooser save = new JFileChooser();
int option = save.showSaveDialog(this);
if(option == JFileChooser.APPROVE_OPTION){
try{
BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath()));
out.write(this.textArea.getText());
out.close();
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
}
}
else if(e.getSource() == this.newFile){
}
}
public static void main(String args[]){
Notepad app = new Notepad();
app.setVisible(true);
}
}
答案 0 :(得分:0)
而不是这样做:
this.saveFile.addActionListener(this);
然后这个:
e.getSource() == this.saveFile
你应该这样:
this.saveFile.addActionListener(this);
this.saveFile.setActionCommand("xy");
然后这个:
e.getActionCommand.equals("xy");
要显示提示,您可以使用JOptionPane
答案 1 :(得分:0)
如果用户说“是”,希望您需要保存文件。
private String message = "Do u want to save this file ?"
int reply = JOptionPane.showConfirmDialog(null, "message", "title",JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION) {
save(this.textArea.getText(), save.getSelectedFile().getPath()));
}else {
//what you need
}
private void save(String content, String path) {
try{
BufferedWriter out = new BufferedWriter(newFileWriter(path) );
out.write(content);
out.close();
} catch(Exception ex){
System.out.println(ex.getMessage());
}`enter code here`
}
你也可以在(e.getSource()== this.saveFile)if else子句上使用save方法。