如何停止ScrollPane焦点调整标签文本的大小?
我正在用JavaFX复制this car loan calculator。
Almost everything works as intended
Until the ScrollPane gets focus,右侧的标题和文本会调整大小
A brief description of the layout
因此,问题在于,当ScrollPane获得焦点时,正在调整我的Labels文本的大小。我尝试通过调整ScrollPane的属性,ScrollPane中包含的布局以及Labels自身来解决此问题。我还搜索过是否曾问过类似的问题。我什么都没找到。
谢谢
编辑:
添加了一些代码,我试图将其保持在最低限度。如果将焦点放在TextField上,则所有Label文本的大小均按预期大小。如果将焦点放在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();
}
}
再次编辑:
问题已解决。谢谢! :)
答案 0 :(得分: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;");
答案 1 :(得分:0)
就我而言,我无法通过 CSS 修复它,因为我必须使用计算出的填充值,而在侦听器中使用 setStyle()
之类的东西太难看了。这是由通过 ScrollPaneBehavior here 设置/调用的 requestLayout()
方法引起的明显错误。当滚动窗格获得焦点时,它也会以某种方式删除任何继承和内容样式。所以我通过停止鼠标事件传播来解决这个问题:
scrollPane.setOnMousePressed(Event::consume);
scrollPaneContentNode.setOnMousePressed(Event::consume);