首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在TextArea中动态更改字体大小及其样式会导致填充错误

在TextArea中动态更改字体大小及其样式会导致填充错误
EN

Stack Overflow用户
提问于 2019-01-17 21:58:53
回答 1查看 82关注 0票数 3

我正在用JavaFX创建一个小的文本编辑器。

为此,我使用TextArea和组合框来动态设置样式:例如,更改字体、大小、加粗、斜体等等。它的工作,但有一个令人不安的视觉错误,我不能容忍。

我试图缩小问题的范围,这里有一个更简单的代码,它有相同的行为和几张图片来理解我在说什么:

(要重现bug,请将大小设置为70,然后选择粗体,您将看到文本是如何远离边缘的。)

代码语言:javascript
复制
public class Main extends Application {
    public void start(Stage stage) {
        textArea = new TextArea("TEST 112123");
        textArea.setPrefWidth(800);
        textArea.setPrefHeight(400);
        textArea.setLayoutY(40);

        CheckBox bold = new CheckBox("BOLD");
        bold.setLayoutX(20);
        bold.setOnAction(e -> {
            Font currentFont = textArea.getFont();

            if (bold.isSelected()) {
                textArea.setFont(
                        new Font("System Bold", currentFont.getSize()));
        //I set new Font each time to save all of it's past properties and
        //change only one of them, this is the only way that I found to do 
        //this as there are no setters in the Font class, only constructors

            } else {
                textArea.setFont(
                        new Font("System", currentFont.getSize()));
            }
        });

        ComboBox sizeBox = new ComboBox();
        //I removed the list of options and the input validity check
        sizeBox.setLayoutX(80);
        sizeBox.setEditable(true);
        sizeBox.setOnAction(e -> {
            textArea.setFont(new Font(textArea.getFont().getName(),
                           Double.valueOf((String)sizeBox.getValue())));
        });

        stage.setScene(new Scene(new Group(textArea, bold, sizeBox), 800, 500));
        stage.show();
    }
}

图片来源:https://imgur.com/a/Cg53nAL

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-17 22:30:05

您可以添加以下样式表。

代码语言:javascript
复制
.text-area .content {
    -fx-padding: 3 7 3 7;
}

它覆盖了modena.css中的填充

代码语言:javascript
复制
.text-area .content {
    /*the is 1px less top and bottom than TextInput because of scrollpane border */
    -fx-padding: 0.25em 0.583em 0.25em 0.583em; /* 3 7 3 7 */
    -fx-cursor: text;
    -fx-background-color:
        linear-gradient(from 0px 0px to 0px 4px, derive(-fx-control-inner-background, -8%), -fx-control-inner-background);
    -fx-background-radius: 2;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54237530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档