2014-12-05 80 views
-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); 
      } 
     }); 
    } 

} 
+4

你的问题是什么? – ItachiUchiha 2014-12-05 10:40:21

回答

0

哇,对不起,我刚才看到我没有与发射(参数)的重要手段;对不起,我很快就拉起扳机。我一直在燃烧两端的蜡烛。

+1

Javafx不需要一个主要的方法来执行,除非你使用的IDE需要'main()' – ItachiUchiha 2014-12-05 10:58:53