JavaFX,如何防止标签文本在 ScrollPane 焦点期间调整大小?
JavaFX, how to prevent Label text resize during ScrollPane focus?
如何阻止 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();
}
}
再次编辑:
问题已解决。谢谢! :)
正如所怀疑的那样,默认的 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 修复它,因为我必须使用计算的填充值,并且在侦听器中使用 setStyle()
之类的东西太丑陋了。这是一个明显的错误,由 requestLayout()
方法引起,该方法是 set/called 通过 ScrollPaneBehavior here。当滚动窗格获得焦点时,它还会以某种方式删除所有继承的样式和内容样式。所以我通过停止鼠标事件传播来解决这个问题:
scrollPane.setOnMousePressed(Event::consume);
scrollPaneContentNode.setOnMousePressed(Event::consume);
如何阻止 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();
}
}
再次编辑:
问题已解决。谢谢! :)
正如所怀疑的那样,默认的 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 修复它,因为我必须使用计算的填充值,并且在侦听器中使用 setStyle()
之类的东西太丑陋了。这是一个明显的错误,由 requestLayout()
方法引起,该方法是 set/called 通过 ScrollPaneBehavior here。当滚动窗格获得焦点时,它还会以某种方式删除所有继承的样式和内容样式。所以我通过停止鼠标事件传播来解决这个问题:
scrollPane.setOnMousePressed(Event::consume);
scrollPaneContentNode.setOnMousePressed(Event::consume);