2016-08-11 90 views
0

當我在JavaFX中開發文本編輯器時,我需要創建一個與其他編輯器一樣工作的Save菜單項。我已打開文件並在文本區域中顯示文件中的文本,並完成了另存爲選項。這只是我沒有收到的保存選項。存儲保存文件的文件名和位置以保存對該特定文件的更改? [JavaFX]

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

在openItemEvent創建(文件)應該是一個實例變量,而不是一個局部變量來該方法。然後saveItemEvent()方法調用saveFile(),傳遞該File實例。

1

首先,創建一個名爲saveFile的File實例變量,並將其設置爲null。然後保存方法應該是這樣的

public void saveItemEvent(){ 
    if(saveFile == null){ 
     saveAsItemEvent(); 
    }else{ 
     saveFile(Main.area.getText(), saveFile); 
    } 
} 

在你saveAsItemEvent,當用戶成功選擇了一個文件,設置saveFile到該文件。

saveFileName = file; 

這是爲了節省。在您的openItemEvent中,當用戶成功選擇文件時,您還可以將saveFile設置爲該文件,以便當用戶打開文件時,它也會自動開始保存到該文件。

+0

爲什麼你會用'String'來代替'File'呢? –

+0

@James_D唯一的原因是文件不能被序列化,所以如果他曾試圖序列化其中的文件的對象,他不會經過數小時試圖找出文件不能被序列化(這是我當我第一次做文本編輯程序時經歷了)。除此之外,真的沒有理由使用字符串而不是文件。 – Jaboyc

+0

['File'是'Serializable'](http://docs.oracle.com/javase/8/docs/api/java/io/File.html)。雖然它起作用,但不斷在字符串和文件之間進行轉換隻是增加了代碼的冗長度,看起來相當不必要。 –