简体   繁体   English

JavaFX,如何防止在 ScrollPane 焦点期间调整标签文本大小?

[英]JavaFX, how to prevent Label text resize during ScrollPane focus?

How do I stop the ScrollPane focus from resizing my Labels' text?如何阻止 ScrollPane 焦点调整标签文本的大小?

I'm making a copy of this car loan calculator in JavaFX.我正在用 JavaFX 制作这个汽车贷款计算器的副本。

Almost everything works as intended几乎一切都按预期工作

Until the ScrollPane gets focus , the title and text on right side gets resized直到 ScrollPane 获得焦点,右侧的标题和文本才会调整大小

A brief description of the layout布局的简要说明

So, the problem is that my Labels' text is being resized when the ScrollPane gets focus.所以,问题是当 ScrollPane 获得焦点时,我的标签文本正在调整大小。 I've tried fixing this by adjusting properties of the ScrollPane, the layouts contained within the ScrollPane, and the Labels themselves.我尝试通过调整 ScrollPane 的属性、包含在 ScrollPane 中的布局和标签本身来解决这个问题。 I also searched if a similar question has been asked before.我还搜索了之前是否有人问过类似的问题。 I've found nothing yet.我还没有发现任何东西。

Thanks谢谢

Edit:编辑:

Added some code, I tried to keep it as minimal as possible.添加了一些代码,我尽量保持最小。 If you focus the TextField, all Label text is sized as expected.如果您关注 TextField,则所有 Label 文本的大小都会按预期进行。 If you focus the ScrollPane, the big blue text sizes down to match the smaller black text.如果您聚焦 ScrollPane,蓝色大文本会缩小以匹配较小的黑色文本。 There is obviously no need for scrolling in this example, but it still shows the issue I have with my project that does require scrolling.在这个例子中显然不需要滚动,但它仍然显示了我的项目确实需要滚动的问题。

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();
    }

}

Edit again:再次编辑:

Problem has been solved.问题已经解决。 Thank you!谢谢! :) :)

As suspected, something is effecting from the default CSS file implementation.正如所怀疑的那样,默认的 CSS 文件实现正在影响某些事情。 You should definitely raise a JIRA ticket regarding this.您绝对应该就此提出 JIRA 票证。

But for time being you can fix the issue, by moving all your style declaration to a css file.但是暂时您可以通过将所有样式声明移动到 css 文件来解决该问题。 And everthing works as expected.一切都按预期工作。

Below is what I had done.下面是我所做的。

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();
    }
}

And in the css file...在 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;
}

Alternately, if you are not interested in css file implementation, you can declare using inline css.或者,如果您对 css 文件实现不感兴趣,您可以使用内联 css 进行声明。 This will also fix your issue.这也将解决您的问题。

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;");

In my case I can't fix it via CSS, because I have to use computed padding value and utilizing something like setStyle() inside a listener is way too ugly.在我的情况下,我无法通过 CSS 修复它,因为我必须使用计算的填充值,并且在侦听器中使用诸如setStyle()类的东西太难看了。 This is an obvious bug caused by the requestLayout() method which is set/called via ScrollPaneBehavior here .这是引起明显的错误requestLayout()被设置/被叫经由ScrollPaneBehavior方法这里 When scroll pane receives focus it also somehow removes any inherited and content styles.当滚动窗格获得焦点时,它也会以某种方式删除任何继承和内容样式。 So I've fixed this by stopping mouse event propagation:所以我通过停止鼠标事件传播来解决这个问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM