JavaFX : Java Graphical Interface Button , Text , Level & Field

 How to work with buttons, text fields and labels in JavaFX. Java Graphical User Interface - How to Create a JavaFX Project in JavaFX Writing - An in-depth discussion.

Let's make a small tax calculator. Which will accept a valid input from the text field, then click the button to calculate the tax and label the tax. We will add a text field, a level, and a button to the seedbillers. I will give the ID of each item.

JavaFX : Java Graphical Interface Button , Text , Level & Field
JavaFX : Java Graphical Interface Button , Text , Level & Field


The ID can also be set from the mood of the text. fx:id="your_id"

Widgets added in Cyan Builder, such as buttons, text views, and labels, to be linked to. Here's how to create: TextField numberFiled;

Button btnCalculate;

Label lblResult;

numberFiled = (TextField) root.lookup("#number");

btnCalculate = (Button) root.lookup("#Calculate");

lblResult = (Shortcut) root.lookup("#result");

We have created an ID, which we are setting the ID. I forgot a lot of time because I forgot the # in front of the project id.

A click composer is added to the button. When the button is clicked, the code inside it runs.

btnCalculate.setOnAction(new event handler() {

@Override

public void handle {ActionEvent} {

// do something

}

});

The data from the text field is read as:

1

numberFiled.getText().toString()

The text is set in the label:

1

lblResult.setText("Great!");

We can then set the result labeled by calculating the tax amount. Full java file:

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.Label;

import javafx.scene.control.TextField;

import javafx.stage.Stage;

public class Main extends application {


Number TextFieldFiled;

Button btnCalculate;

Label lblResult;

double tax;

@Override

public void start(Stage primaryStage) throws an exception {

Parent Root = FXMLLoader.load(getClass().GetResource("sample.fxml"));

primaryStage.setTitle("Hello World");

primaryStage.setScene(new scene(root, 300, 275));

primary scene.show();

numberFiled = (TextField) root.lookup("#number");

btnCalculate = (Button) root.lookup("#Calculate");

lblResult = (Shortcut) root.lookup("#result");

btnCalculate.setOnAction(new event handler() {

@Override

public void handle {ActionEvent} {

Double sum = Double.parseDouble(numberFiled.getText().toString());

tax = (sum * 0.05); // 5% tax

lblResult.setText(tax.toString());

}

});

}

public static void main(string[] args) {

launch(arguments);

}

}

If we run the code, we get such an application.

Although these things seem very simple, they can be used to create many complex programs. They can then be shared with everyone if they want the software.

0 Comments