当我在JavaFX中开发文本编辑器时,我需要创建一个与所有其他编辑器一样的“保存”菜单项选项。我打开文件并在TextArea和Save As选项中显示文件。它只是我没有得到的保存选项。
Main.java:
package com.josh.main;
import javafx.application.Application;
import javafx.scene.AccessibleRole;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Main extends Application
{
private BorderPane pane;
public static TextArea area;
private Scene scene;
private MenuBar bar;
private Menu file;
private Menu help;
private MenuItem open;
private MenuItem save;
private MenuItem saveAs;
private MenuItem about;
private int width;
private int height;
private MenuItemEvents events;
public static Stage primaryStage;
@SuppressWarnings("static-access")
@Override
public void start(Stage primaryStage) throws Exception
{
this.primaryStage = primaryStage;
setWidth(800);
setHeight(800);
initComponents();
pair();
pane.setTop(bar);
pane.setCenter(area);
about.setOnAction(event -> events.helpItemEvent());
saveAs.setOnAction(event -> events.saveAsItemEvent());
open.setOnAction(event -> events.openItemEvent());
primaryStage.getIcons().add(getIconImage());
primaryStage.setScene(scene);
primaryStage.setTitle("GigaEdit");
primaryStage.show();
}
public static void main(String[] arguments)
{
Application.launch(arguments);
}
private void initComponents()
{
events = new MenuItemEvents();
pane = new BorderPane();
area = new TextArea();
scene = new Scene(pane, getWidth(), getHeight());
bar = new MenuBar();
file = new Menu("File");
help = new Menu("Help");
open = new MenuItem("Open");
save = new MenuItem("Save");
saveAs = new MenuItem("Save As");
about = new MenuItem("About");
setAccessibilityRoles();
setAccessibilityRoleDescriptions();
setAccessibilityHelp();
}
private void setWidth(int width)
{
this.width = width;
}
private void setHeight(int height)
{
this.height = height;
}
private int getWidth()
{
return width;
}
private int getHeight()
{
return height;
}
private void pair()
{
bar.getMenus().addAll(file, help);
file.getItems().addAll(open, save, saveAs);
help.getItems().add(about);
}
private void setAccessibilityRoles()
{
bar.setAccessibleRole(AccessibleRole.MENU_BAR);
area.setAccessibleRole(AccessibleRole.TEXT_AREA);
}
private void setAccessibilityRoleDescriptions()
{
bar.setAccessibleRoleDescription("Menu Bar. Contains two Menus. First is 'File'. Second is 'Help'");
area.setAccessibleRoleDescription("Edit Box. This is where you type what you need to type inside of your document.");
}
private void setAccessibilityHelp()
{
bar.setAccessibleHelp("This Menu Bar contains two Menus. The First is 'File'. The File Menu has your options for opening other files and saving the current file.");
area.setAccessibleHelp("This text box is used to enter the text that you would like to be in the current document.");
}
private Image getIconImage()
{
return new Image(Main.class.getResourceAsStream("icon.png"));
}
}
MenuItemEvents.java:
package com.josh.main;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.stage.FileChooser;
public class MenuItemEvents
{
public void helpItemEvent()
{
HelpAlert.display("About GigaEdit", "GigaEdit v1.0\nCopyright Josh Root 2016\nAny redistribution of this software without proper permission is prohibited\nAny attempt to do such actions will result in legal action taken upon the offender");
}
public void openItemEvent()
{
FileChooser fc = new FileChooser();
FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Text Files", "*.txt");
fc.getExtensionFilters().add(filter);
File file = fc.showOpenDialog(Main.primaryStage);
if(file != null)
{
Main.area.setText(readFile(file));
}
}
public void saveItemEvent(){}
public void saveAsItemEvent()
{
FileChooser fc = new FileChooser();
FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Text Files", "*.txt");
fc.getExtensionFilters().add(filter);
File file = fc.showSaveDialog(Main.primaryStage);
if(file != null)
{
saveFile(Main.area.getText(), file);
}
}
private void saveFile(String text, File file)
{
try
{
FileWriter fw;
fw = new FileWriter(file);
fw.write(text);
fw.close();
}
catch(IOException e)
{
Logger.getLogger(MenuItemEvents.class.getName()).log(Level.SEVERE, null, e);
}
}
private String readFile(File file)
{
StringBuilder stringBuffer = new StringBuilder();
BufferedReader bufferedReader = null;
try
{
bufferedReader = new BufferedReader(new FileReader(file));
String text;
while((text = bufferedReader.readLine()) != null)
{
stringBuffer.append(text);
}
}
catch(FileNotFoundException e)
{
Logger.getLogger(MenuItemEvents.class.getName()).log(Level.SEVERE, null, e);
}
catch(IOException e)
{
Logger.getLogger(MenuItemEvents.class.getName()).log(Level.SEVERE, null, e);
}
finally
{
try
{
bufferedReader.close();
}
catch(IOException e)
{
Logger.getLogger(MenuItemEvents.class.getName()).log(Level.SEVERE, null, e);
}
}
return stringBuffer.toString();
}
}
答案 0 :(得分:1)
首先,有一个名为saveFile的File实例变量,并将其设置为null
。然后,save方法应该如下所示
public void saveItemEvent(){
if(saveFile == null){
saveAsItemEvent();
}else{
saveFile(Main.area.getText(), saveFile);
}
}
在saveAsItemEvent中,当用户成功选择文件时,将saveFile
设置为该文件。
saveFileName = file;
这是为了保存。在openItemEvent中,当用户成功选择文件时,您还可以将saveFile
设置为该文件,这样当用户打开文件时,它也会自动开始保存到该文件。
答案 1 :(得分:0)
在openItemEvent()中创建的文件应该是实例变量而不是该方法的局部变量。然后saveItemEvent()方法只调用saveFile(),传递该File实例。