java/javafx奇怪的字母打印错误

发布于 2025-02-09 23:57:51 字数 4773 浏览 1 评论 0原文

我必须在Windowns PC上使用Javafx创建GUI;运行程序时,字母完全弄清楚了,几乎就像是另一种语言一样。

我尝试将001输入其中一个文本字段,但它键入223

“时髦的文本示例”

标签看起来正常的唯一原因是因为我已经指定了它的字体必须成为Verdana。另外,将文本打印到控制台时,它会正确打印出来;即使在打印时,按钮的文字在控制台上也是正确的。

这是代码:

package com.itjva2.question3;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.TilePane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;


/**
 * JavaFX App
 */
public class App extends Application {

    @Override
    public void start(Stage primaryStage) {
        
        TextField txtPatientID = new TextField();
        TextField txtName = new TextField();
        TextField txtGender = new TextField();
        TextField txtWeight = new TextField();
        TextField txtHeight = new TextField();
        TextField txtMessage = new TextField();
        RadioButton rbMale = new RadioButton("Male");
        RadioButton rbFemale = new RadioButton("Female");
        
 //Variables for font height width color & more
        double dHeight = 40;
        double dWidth = 250;

//      Create 6 labels and 6 corresponding text boxes
        //Labels
        Label lblPatientID = new Label("Patient_ID");
        double FontSize = 13;
        lblPatientID.setFont(Font.font("Verdana", FontWeight.BOLD, FontSize));
        Label lblName = new Label("Name");
        lblName.setFont(Font.font("Verdana", FontWeight.BOLD, FontSize));
        Label lblGender = new Label("Gender");
        lblGender.setFont(Font.font("Verdana", FontWeight.BOLD, FontSize));
        Label lblWeight = new Label("Weight in Kg");
        lblWeight.setFont(Font.font("Verdana", FontWeight.BOLD, FontSize));
        Label lblHeight = new Label("Height in metres");
        lblHeight.setFont(Font.font("Verdana", FontWeight.BOLD, FontSize));
        Label lblMessage = new Label("Message");
        lblMessage.setFont(Font.font("Verdana", FontWeight.BOLD, FontSize));

        //RadioButton
        TilePane r = new TilePane();
        rbMale.setFont(Font.font("Verdana", FontWeight.BOLD, FontSize));
        rbFemale.setFont(Font.font("Verdana", FontWeight.BOLD, FontSize));
        r.getChildren().add(rbMale);
        r.getChildren().add(rbFemale);
        r.setHgap(195);
        r.setPrefTileHeight(40);
        r.relocate(0, 40*3);

        //Buttons
        Button btnCalculate = new Button("Calculate BMI");
        Button btnDisplay = new Button("Display");

//      Position all elements on the screen correctly
        lblPatientID.relocate(5, 15);
        txtPatientID.relocate(250, 0);
        txtPatientID.setPrefWidth(dWidth);
        txtPatientID.setPrefHeight(dHeight);
        lblName.relocate(5, 55);
        txtName.relocate(250, 40);
        txtName.setPrefWidth(dWidth);
        txtName.setPrefHeight(dHeight);
        lblGender.relocate(5, 95);
        txtGender.relocate(250, 40*2);
        txtGender.setPrefWidth(dWidth);
        txtGender.setPrefHeight(dHeight);
        r.setStyle("-fx-background-color: #D6D5CB;");
        lblWeight.relocate(5, 175);
        txtWeight.relocate(250, 40*4);
        txtWeight.setPrefWidth(dWidth);
        txtWeight.setPrefHeight(dHeight);
        lblHeight.relocate(5, 215);
        txtHeight.relocate(250, 40*5);
        txtHeight.setPrefWidth(dWidth);
        txtHeight.setPrefHeight(dHeight);
        lblMessage.relocate(5, 255);
        txtMessage.relocate(250, 40*6);
        txtMessage.setPrefWidth(dWidth);
        txtMessage.setPrefHeight(dHeight);
        btnCalculate.relocate(0, 40*7);
        btnCalculate.setPrefWidth(dWidth);
        btnCalculate.setPrefHeight(dHeight);
        btnDisplay.relocate(250, 40*7);
        btnDisplay.setPrefWidth(dWidth);
        btnDisplay.setPrefHeight(dHeight);



//      Group all elements together and add to the scene
        Group grpAll = new Group(lblPatientID,lblName,lblGender,lblWeight,lblHeight,r,lblMessage,txtPatientID,txtName,txtGender,txtWeight,txtHeight,txtMessage,btnCalculate,btnDisplay);

    //  FIX BUG Code
//        grpAll.setStyle(chinese_fix);


        Scene sceneMain = new Scene(grpAll, 500, 365);
        sceneMain.setFill(Color.YELLOW);

//      Set height & title of the APP-Window
        primaryStage.setTitle("GWC Timer App");
        primaryStage.setWidth(500);
        primaryStage.setHeight(365);

//      Set the scene to the stage and show the stage
        primaryStage.setScene(sceneMain);
        primaryStage.show();

        System.out.println(btnDisplay.getText());
    }

    public static void main(String[] args) {
        launch();
    }

}

I have to create a GUI using JavaFX on a Windowns PC ; when running the program, the letters print out completely weird, almost as if it is a different language.

I tried to type in 001 into one of the text fields, but it typed 223. Example of funky looking text:

Example of funky looking text

The only reason that the labels look normal is because I have specified that its font must be Verdana. Also, when printing text to the console, it prints out correctly; even when printing, the the button's text to the console it is correct.

Here is the code:

package com.itjva2.question3;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.TilePane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;


/**
 * JavaFX App
 */
public class App extends Application {

    @Override
    public void start(Stage primaryStage) {
        
        TextField txtPatientID = new TextField();
        TextField txtName = new TextField();
        TextField txtGender = new TextField();
        TextField txtWeight = new TextField();
        TextField txtHeight = new TextField();
        TextField txtMessage = new TextField();
        RadioButton rbMale = new RadioButton("Male");
        RadioButton rbFemale = new RadioButton("Female");
        
 //Variables for font height width color & more
        double dHeight = 40;
        double dWidth = 250;

//      Create 6 labels and 6 corresponding text boxes
        //Labels
        Label lblPatientID = new Label("Patient_ID");
        double FontSize = 13;
        lblPatientID.setFont(Font.font("Verdana", FontWeight.BOLD, FontSize));
        Label lblName = new Label("Name");
        lblName.setFont(Font.font("Verdana", FontWeight.BOLD, FontSize));
        Label lblGender = new Label("Gender");
        lblGender.setFont(Font.font("Verdana", FontWeight.BOLD, FontSize));
        Label lblWeight = new Label("Weight in Kg");
        lblWeight.setFont(Font.font("Verdana", FontWeight.BOLD, FontSize));
        Label lblHeight = new Label("Height in metres");
        lblHeight.setFont(Font.font("Verdana", FontWeight.BOLD, FontSize));
        Label lblMessage = new Label("Message");
        lblMessage.setFont(Font.font("Verdana", FontWeight.BOLD, FontSize));

        //RadioButton
        TilePane r = new TilePane();
        rbMale.setFont(Font.font("Verdana", FontWeight.BOLD, FontSize));
        rbFemale.setFont(Font.font("Verdana", FontWeight.BOLD, FontSize));
        r.getChildren().add(rbMale);
        r.getChildren().add(rbFemale);
        r.setHgap(195);
        r.setPrefTileHeight(40);
        r.relocate(0, 40*3);

        //Buttons
        Button btnCalculate = new Button("Calculate BMI");
        Button btnDisplay = new Button("Display");

//      Position all elements on the screen correctly
        lblPatientID.relocate(5, 15);
        txtPatientID.relocate(250, 0);
        txtPatientID.setPrefWidth(dWidth);
        txtPatientID.setPrefHeight(dHeight);
        lblName.relocate(5, 55);
        txtName.relocate(250, 40);
        txtName.setPrefWidth(dWidth);
        txtName.setPrefHeight(dHeight);
        lblGender.relocate(5, 95);
        txtGender.relocate(250, 40*2);
        txtGender.setPrefWidth(dWidth);
        txtGender.setPrefHeight(dHeight);
        r.setStyle("-fx-background-color: #D6D5CB;");
        lblWeight.relocate(5, 175);
        txtWeight.relocate(250, 40*4);
        txtWeight.setPrefWidth(dWidth);
        txtWeight.setPrefHeight(dHeight);
        lblHeight.relocate(5, 215);
        txtHeight.relocate(250, 40*5);
        txtHeight.setPrefWidth(dWidth);
        txtHeight.setPrefHeight(dHeight);
        lblMessage.relocate(5, 255);
        txtMessage.relocate(250, 40*6);
        txtMessage.setPrefWidth(dWidth);
        txtMessage.setPrefHeight(dHeight);
        btnCalculate.relocate(0, 40*7);
        btnCalculate.setPrefWidth(dWidth);
        btnCalculate.setPrefHeight(dHeight);
        btnDisplay.relocate(250, 40*7);
        btnDisplay.setPrefWidth(dWidth);
        btnDisplay.setPrefHeight(dHeight);



//      Group all elements together and add to the scene
        Group grpAll = new Group(lblPatientID,lblName,lblGender,lblWeight,lblHeight,r,lblMessage,txtPatientID,txtName,txtGender,txtWeight,txtHeight,txtMessage,btnCalculate,btnDisplay);

    //  FIX BUG Code
//        grpAll.setStyle(chinese_fix);


        Scene sceneMain = new Scene(grpAll, 500, 365);
        sceneMain.setFill(Color.YELLOW);

//      Set height & title of the APP-Window
        primaryStage.setTitle("GWC Timer App");
        primaryStage.setWidth(500);
        primaryStage.setHeight(365);

//      Set the scene to the stage and show the stage
        primaryStage.setScene(sceneMain);
        primaryStage.show();

        System.out.println(btnDisplay.getText());
    }

    public static void main(String[] args) {
        launch();
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文