【问题标题】:JavaFX, how to prevent Label text resize during ScrollPane focus?JavaFX,如何防止在 ScrollPane 焦点期间调整标签文本大小?
【发布时间】:2018-12-03 23:00:59
【问题描述】:

如何阻止 ScrollPane 焦点调整标签文本的大小?

我正在 JavaFX 中制作this car loan calculator 的副本。

Almost everything works as intended

Until the ScrollPane gets focus,右侧的标题和文字被调整大小

A brief description of the layout

所以,问题是当 ScrollPane 获得焦点时,我的标签文本正在调整大小。我尝试通过调整 ScrollPane 的属性、ScrollPane 中包含的布局以及标签本身来解决此问题。我还搜索了以前是否有人问过类似的问题。我还没有找到任何东西。

谢谢

编辑:

添加了一些代码,我尽量减少它。如果您关注 TextField,则所有标签文本的大小都符合预期。如果您聚焦 ScrollPane,则蓝色的大文本会缩小以匹配较小的黑色文本。在此示例中显然不需要滚动,但它仍然显示了我的项目确实需要滚动的问题。

package application;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.stage.Stage;

public class Main extends Application
{
    public static void main(String[] args)
    {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage)
    {
        Stage stage = primaryStage;

        HBox myHBox = new HBox();

        Label smallerText = new Label("this is small\ntext");
        smallerText.setStyle("-fx-font-weight: bold");
        smallerText.setTextFill(Color.web("#000000"));
        smallerText.setFont(Font.font("Leelawadee UI", FontPosture.REGULAR, 12));

        Label biggerText = new Label("this is big\ntext");
        biggerText.setStyle("-fx-font-weight: bold");
        biggerText.setTextFill(Color.web("#005FBA"));
        biggerText.setFont(Font.font("Leelawadee UI", FontPosture.REGULAR, 24));

        TextField myTextField = new TextField();

        myHBox.getChildren().addAll(smallerText, biggerText);

        VBox myVBox = new VBox();
        myVBox.getChildren().addAll(myHBox, myTextField);

        ScrollPane myScrollPane = new ScrollPane(myVBox);
        myScrollPane.setHbarPolicy(ScrollBarPolicy.NEVER);
        myScrollPane.setVbarPolicy(ScrollBarPolicy.ALWAYS);

        Scene scene = new Scene(myScrollPane);
        stage.setScene(scene);
        stage.show();
    }

}

再次编辑:

问题已解决。谢谢! :)

【问题讨论】:

  • 为什么不在没有ScrollPane 的情况下确保所有内容都适合窗口?
  • 根据 TextFields 中的值,屏幕上会添加额外的文本。空间不足,无法容纳所需字体大小的所有文本。
  • 如果您使用任何 css 实现,我怀疑 css 文件会影响某些东西。我能注意到的是,在关注滚动窗格时,所有字体大小都设置为某个值。你能发布你的css文件吗?(如果有的话)
  • 我对 css 的了解不够,无法回答您的问题。但是,如果我理解正确,某些代码确实会更改 css。我在帖子中添加了示例代码,可能有助于澄清事情。
  • 哦,有一个“application.css”文件,但是里面只有默认的注释。

标签: java javafx resize focus scrollpane


【解决方案1】:

正如怀疑的那样,默认的 CSS 文件实现有一些影响。您绝对应该为此提出 JIRA 罚单。

但暂时您可以通过将所有样式声明移至 css 文件来解决此问题。一切都按预期进行。

以下是我所做的。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class FontResizeIssueOnFocus extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        Stage stage = primaryStage;
        HBox myHBox = new HBox();

        Label smallerText = new Label("this is small\ntext");
        smallerText.getStyleClass().add("smaller-text");

        Label biggerText = new Label("this is big\ntext");
        biggerText.getStyleClass().add("bigger-text");

        TextField myTextField = new TextField();
        myHBox.getChildren().addAll(smallerText, biggerText);

        VBox myVBox = new VBox();
        myVBox.getChildren().addAll(myHBox, myTextField);

        ScrollPane myScrollPane = new ScrollPane(myVBox);
        myScrollPane.setHbarPolicy(ScrollBarPolicy.NEVER);
        myScrollPane.setVbarPolicy(ScrollBarPolicy.ALWAYS);

        Scene scene = new Scene(myScrollPane);
        scene.getStylesheets().add(getClass().getResource("scrollpanefontissue.css").toString());
        stage.setScene(scene);
        stage.show();
    }
}

并且在 css 文件中...

.smaller-text{
  -fx-font-weight:bold;
  -fx-text-fill:#000000;
  -fx-font-family: "Leelawadee UI";
  -fx-font-size:12px;
}
.bigger-text{
  -fx-font-weight:bold;
  -fx-text-fill:#005FBA;
  -fx-font-family: "Leelawadee UI";
  -fx-font-size:24px;
}

或者,如果您对 css 文件的实现不感兴趣,您可以声明使用内联 css。这也将解决您的问题。

Label smallerText = new Label("this is small\ntext");
smallerText.setStyle("-fx-font-weight: bold;-fx-text-fill:#000000;-fx-font-family:\"Leelawadee UI\";-fx-font-size:12px;");

Label biggerText = new Label("this is big\ntext");
biggerText.setStyle("-fx-font-weight: bold;-fx-text-fill:#005FBA;-fx-font-family:\"Leelawadee UI\";-fx-font-size:24px;");

【讨论】:

  • 太棒了!这解决了我在项目中遇到的问题。我选择使用内联 css。我很乐意为此提出 JIRA 票,但不知道如何开始这样做。您(或其他任何人)有链接让我开始吗?
  • 它就像魔术一样修复了。非常感谢朋友。
【解决方案2】:

在我的情况下,我无法通过 CSS 修复它,因为我必须使用计算的填充值,并且在侦听器中使用 setStyle() 之类的东西太丑陋了。这是由 requestLayout() 方法引起的明显错误,该方法通过 ScrollPaneBehavior here 设置/调用。当滚动窗格获得焦点时,它还会以某种方式删除任何继承的样式和内容样式。所以我通过停止鼠标事件传播来解决这个问题:

scrollPane.setOnMousePressed(Event::consume);
scrollPaneContentNode.setOnMousePressed(Event::consume);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    • 2018-01-19
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 2014-02-20
    相关资源
    最近更新 更多