javafx进制转换器

package sample;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

import java.beans.EventHandler;

public class Main extends Application {
    TextField t1 = new TextField();
    TextField t2 = new TextField();
    TextField t3 = new TextField();

//    Button btOK = new Button();
    @Override
    public void start(Stage primaryStage) throws Exception{
        GridPane pane = new GridPane();
        pane.setAlignment(Pos.CENTER);
        pane.setHgap(5);
        pane.setVgap(5);
        t1.setAlignment(Pos.BASELINE_RIGHT);
        t2.setAlignment(Pos.BASELINE_RIGHT);
        t3.setAlignment(Pos.BASELINE_RIGHT);

//        t2.setEditable(false);
//        t3.setEditable(false);
        pane.add(new Label("Decimal"),0,0);
        pane.add(t1,1,0);
        pane.add(new Label("Hex"),0,1);
        pane.add(t2,1,1);
        pane.add(new Label("Binary"),0,2);
        pane.add(t3,1,2);

        t1.setOnAction(e->{
//            String strT1=t1.getText();
            int a = Integer.parseInt(t1.getText());
//            Integer.toBinaryString(a);
//            Integer.toHexString(a);
            t2.setText(Integer.toHexString(a)+"");
            t3.setText(Integer.toBinaryString(a)+"");
        });
        t2.setOnAction(e->{
            String str = t2.getText();
            int a = Integer.parseInt(str,16);
            t1.setText(a+"");
            t3.setText(Integer.toBinaryString(a)+"");
        });
        t3.setOnAction(e->{

            String str = t3.getText();
            int a = Integer.parseInt(str,2);
            t1.setText(a+"");
            t2.setText(Integer.toHexString(a)+"");

        });
        primaryStage.setTitle("Exercise16_05");
        primaryStage.setScene(new Scene(pane, 300, 150));
        primaryStage.show();
    }

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaFX中提供了FXML来将UI界面与Java代码分离,使得UI设计师可以专注于设计UI界面,而Java开发者可以专注于编业务逻辑代码。因此,将JavaFX代码转换为FXML文件可以提高开发效率和可维护性。 下面是将JavaFX代码转换为FXML的步骤: 1. 创建FXML文件 在项目的src/main/resources目录下创建一个新的FXML文件。可以使用FXML Editor或者任何文本编辑来创建和编辑FXML文件。 2. 将JavaFX代码复制到FXML文件中 将JavaFX代码中的UI界面部分复制到FXML文件中的fx:root标签内。如下所示: ``` <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <fx:root type="AnchorPane" xmlns:fx="http://javafx.com/fxml/1"> <children> <Button text="Click Me!" /> </children> </fx:root> ``` 3. 添加FXML注释 在FXML文件中添加FXML注释,以指定FXML文件和Java类之间的关联。如下所示: ``` <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <!-- Sample.fxml --> <fx:root type="AnchorPane" xmlns:fx="http://javafx.com/fxml/1"> <children> <Button text="Click Me!" /> </children> </fx:root> ``` 4. 在Java类中加载FXML文件 在Java类中使用FXMLLoader类来加载FXML文件,并将其与UI界面关联。如下所示: ``` public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ FXMLLoader loader = new FXMLLoader(getClass().getResource("Sample.fxml")); Parent root = loader.load(); primaryStage.setScene(new Scene(root)); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ``` 以上就是将JavaFX代码转换为FXML文件的步骤。通过使用FXML,可以更好地管理UI界面和业务逻辑代码,提高开发效率和可维护性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值