Java mini project
Decimal conversion calculator using javafx
PROGRAM:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class cal extends Application
{
@Override//from ww w . ja v a2s . co m
public void start(Stage primaryStage) throws Exception
{
// Create the top section of the UI
Text tdec = new Text("Decimal:");
Text tResult = new Text("Result:");
TextField tfdec = new TextField();
TextField tfResult = new TextField();
tfResult.setEditable(false);
// Create the bottom section of the UI
Button btbin = new Button("BINARY");
Button bthex = new Button("HEXA DECIMAL");
Button btoct = new Button("OCTAL");
// Add top and bottom UI to VBox containers
VBox calcTop = new VBox(10);
calcTop.setAlignment(Pos.CENTER);
calcTop.setPadding(new Insets(10));
calcTop.getChildren().addAll(tdec, tfdec, tResult, tfResult);
VBox calcBottom = new VBox(10);
calcBottom.setAlignment(Pos.CENTER);
calcBottom.setPadding(new Insets(10));
calcBottom.getChildren().addAll(btbin, bthex, btoct);
// Add VBox containers to a BorderPane
BorderPane pane = new BorderPane();
pane.setCenter(calcTop);
pane.setBottom(calcBottom);
// Register event handlers for buttons
btbin.setOnAction(e -> {
int a = Integer.parseInt(tfdec.getText());
tfResult.setText(String.valueOf(Integer.toBinaryString(a)));
});
bthex.setOnAction(e -> {
int a = Integer.parseInt(tfdec.getText());
tfResult.setText(String.valueOf(Integer.toHexString(a)));
});
btoct.setOnAction(e -> {
int a = Integer.parseInt(tfdec.getText());
tfResult.setText(String.valueOf(Integer.toOctalString(a)));
});
Scene scene = new Scene(pane);
primaryStage.setTitle("DECIMAL TO BINARY,HEXADECIMAL AND OCTAL");
primaryStage.setScene(scene);
primaryStage.setResizable(true);
primaryStage.show();
}
public static void main(String[] args)
{
Application.launch(args);
}
}
OUTPUT: