2016-10-02 124 views
0

這是我的第一篇文章。JavaFX文本區:在每行的末尾添加「 - 」

我正在使用NetBeans IDE,並且正在爲我的JavaFX項目使用Scene Builder。 我有變量「定義」 文本區域我也有一個按鈕與動作

void addAction(ActionEvent event) { 
} 

我試圖在文本區行與結束「 - 」。

爲了顯現它: 輸入:

happy 
sad 
good 
bad 

輸出:

happy - 
sad - 
good - 
bad - 

我有這樣的for循環:

for (int i = 0 ; definitions.getText().split("\\n"); i++){ 
     String previous = definitions.getText(); 
     definitions.setText(previous + " - "); 
     } 

但輸出是:

happy 
sad 
good 
bad - - - - 

有人可以幫我嗎? 非常感謝大家。 MineRockers

這裏是案件的完整代碼你們需要它:

package vocabulary.javafx; 

import java.net.URL; 
import java.awt.Desktop; 
import java.io.BufferedWriter; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.net.URI; 
import java.net.URISyntaxException; 
import java.util.ResourceBundle; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.*; 

public class FXMLDocumentController implements Initializable { 

    //Declaration... 
    private boolean Email; 
    private boolean Save; 
    private String Word; 

    @FXML 
    private Button add; 

    @FXML 
    private TextArea sentences; 

    @FXML 
    private Button searchS; 

    @FXML 
    private TextField helpD; 

    @FXML 
    private Button finish; 

    @FXML 
    private Label label; 

    @FXML 
    private TextField helpS; 

    @FXML 
    private TextArea definitions; 

    @FXML 
    private Button searchD; 

    @FXML 
    void addAction(ActionEvent event) { 
     String previous = definitions.getText(); 
     String[] linecountS = definitions.getText().split("\n"); 
     for() 
     int lineCount = Integer.parseInt(linecountS); 
     //for (int i = 0 ; definitions.getText().split("\\n"); i++){ 
     // String previous = definitions.getText(); 
     // definitions.setText(previous + "-"); 
     //} 
    } 

    @FXML 
    void helpDAction(ActionEvent event) { 
     //Enter Key Pressed 
     Word = helpD.getText(); 
     if (Desktop.isDesktopSupported()) { 
      try { 
       Desktop.getDesktop().browse(new URI("http://www.dictionary.com/browse/" + Word + "?s=t")); 
      } catch (URISyntaxException | IOException ex) { 
      } 
     } 
    } 

    @FXML 
    void helpSAction(ActionEvent event) { 
     //Enter Key Pressed 
     Word = helpS.getText(); 
     if (Desktop.isDesktopSupported()) { 
      try { 
       Desktop.getDesktop().browse(new URI("http://sentence.yourdictionary.com/" + Word)); 
      } catch (URISyntaxException | IOException ex) { 
      } 
     } 
    } 

    @FXML 
    void searchDAction(ActionEvent event) { 
     //Button Clicked 
     Word = helpD.getText(); 
     if (Desktop.isDesktopSupported()) { 
      try { 
       Desktop.getDesktop().browse(new URI("http://www.dictionary.com/browse/" + Word + "?s=t")); 
      } catch (URISyntaxException | IOException ex) { 
      } 
     } 
    } 

    @FXML 
    void searchSAction(ActionEvent event) { 
     //Button Clicked 
     Word = helpS.getText(); 
     if (Desktop.isDesktopSupported()) { 
      try { 
       Desktop.getDesktop().browse(new URI("http://sentence.yourdictionary.com/" + Word)); 
      } catch (URISyntaxException | IOException ex) { 
      } 
     } 
    } 

    @FXML 
    void finishAction(ActionEvent event) { 
     String Content1 = definitions.getText(); 
     String Content2 = sentences.getText(); 
     String Finalized = "Auto-generated message by Vocabulary 2.0 by MineRocker\n\nDefinitions:\n" + Content1 + " \n\n\nSentences:\n" + Content2; 

     if (Desktop.isDesktopSupported()) { 
      try { 
       Desktop.getDesktop().browse(new URI("gmail.com")); 
      } catch (URISyntaxException | IOException ex) { 
      } 

      FileWriter fileWriter; 
      try { 
       fileWriter = new FileWriter("AFile.txt"); 
       try (BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) { 
        bufferedWriter.write(Finalized); 
       } 

      } catch (IOException ex) { 
      } 
     } 
    } 

    @Override 
    public void initialize(URL url, ResourceBundle rb) { 

    } 

} 
+0

傢伙,任何的建議是好的,不要」毫不猶豫地發表評論或給出答案! –

回答

0

該代碼添加 「 - 」 任意行之後:

String str = definitions.getText().replaceAll("\n", " -\n"); 
str = str + " -";// add "-" to last line 
definitions.setText(str); 
+0

哦,我的天哪,它的作品!非常感謝。 –