我正在尝试学习 JavaFX,只是偶然发现了一些令我困惑的事情。我在 IntelliJ 中创建了一个新项目并添加了一个按钮。
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
private Button firstButton;
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
firstButton = new Button("My First Button");
StackPane layout = new StackPane();
layout.getChildren().add(firstButton);
Scene scene = new Scene(layout, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
当我将文本更改为较小的内容时(我认为如果文本长于按钮长度,则会出错),它变得更加奇怪。这是我尝试的下一个例子:
firstButton = new Button("E");
我用谷歌搜索了很多,但没有发现有人在谈论这个。要么我是个糟糕的 Google 员工,要么这不是一个常见问题。我最初的想法是这是一个编码问题。在我看到神秘的 E -> G 之后,我想可能还有其他问题。
有什么想法吗?
谢谢!
而不是这个:
primaryStage.setScene(new Scene(root, 300, 250));
你可以这样做:
Scene scene = new Scene(root, 300, 250);
scene.getRoot().setStyle("-fx-font-family: 'serif'");
primaryStage.setScene(scene);