JavaFX文本区:在每行的末尾添加“ - ”

时间:2016-10-02 10:31:21

标签: user-interface javafx textarea

这是我的第一篇文章。

我正在使用NetBeans IDE,而我正在将Java Builder用于JavaFX项目。 我有一个带有变量“定义”的文本区域 我还有一个带动作的按钮

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) {

    }

}

1 个答案:

答案 0 :(得分:0)

此代码在任何行后添加“ - ”:

IsChecked