【问题标题】:JavaFX Text Area: Adding "-" to the end of each lineJavaFX 文本区域:在每行末尾添加“-”
【发布时间】:2016-10-02 10:31:21
【问题描述】:

这是我的第一篇文章。

我正在使用 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 -  -  -  - 

有人可以帮我吗? 非常感谢大家。 矿工

这里是完整的代码,以防你们需要它:

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

    }

}

【问题讨论】:

  • 小伙伴们,有什么好的建议,不要犹豫,评论或给出答案!

标签: user-interface javafx textarea


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2013-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 2012-07-03
    • 2014-05-29
    相关资源
    最近更新 更多