-1
我正在使用JavaFx進行學校作業。該任務是創建一個使用JavaFx GUI在十進制,十六進制和二進制數之間轉換的程序。 GUI有三個字段,一個用於十進制,十六進制和二進制。你應該在任何字段中輸入一個數字,然後按回車鍵,它會在相應的文本框中給你轉換。任何幫助將不勝感激。JavaFx號碼轉換
在此先感謝。 下面的代碼:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Exercise16_05 extends Application {
private TextField tfDecimal = new TextField();
private TextField tfHex = new TextField();
private TextField tfBinary = new TextField();
@Override
// Override the start method in the Application class
public void start(Stage primaryStage) {
// Create UI
GridPane gridPane = new GridPane();
gridPane.setHgap(5);
gridPane.setVgap(5);
gridPane.add(new Label("Decimal: "), 0, 0);
gridPane.add(tfDecimal, 1, 0);
gridPane.add(new Label("Hex: "), 0, 1);
gridPane.add(tfHex, 1, 1);
gridPane.add(new Label("Binary"), 0, 2);
gridPane.add(tfBinary, 1, 2);
// Create the scene
Scene scene = new Scene(gridPane, 400, 200);
primaryStage.setTitle("Exercise 16_05");
primaryStage.setScene(scene);
primaryStage.show();
tfDecimal.setOnKeyPressed(e -> {
if (e.getCode()== KeyCode.ENTER) { // When ENTER is pressed
int decimal = Integer.parseInt(tfDecimal.getText());
String decToHex = Integer.toHexString(decimal);
String decToBi = Integer.toBinaryString(decimal);
tfBinary.setText(String.format(decToBi));
tfHex.setText(String.format(decToHex));
}
});
tfHex.setOnKeyPressed(e -> {
if (e.getCode()== KeyCode.ENTER) {
int hex = Integer.parseInt(tfHex.getText(), 16); // Get data from text field
String hexToDec = Integer.toString(hex);
String hexToBi = Integer.toBinaryString(hex);
// Display hex to Bi and hex to Dec
tfBinary.setText(String.format(hexToBi));
tfDecimal.setText(hexToDec);
}
});
tfBinary.setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.ENTER) {
int binary = Integer.parseInt(tfBinary.getText(),2);
String biToHex = Integer.toHexString(binary);
String biToDec = Integer.toString(binary);
// String biToDec = Integer.parseInt(binary);
tfHex.setText(String.format(biToHex));
tfDecimal.setText(biToDec);
}
});
}
}
你的問題是什麼? – ItachiUchiha 2014-12-05 10:40:21