Temperature conversion applet--JavaFX

  • As a beginner of the Java language, I was learning JavaFX recently. I saw that most of the small programs on CSDN used swing to realize the simple functions of the GUI, so I thought about using JavaFX to realize it. I chose a very simple temperature conversion program, and it was very fast. One day, I finally finished writing. Although it is simple, I still want to record it~~~

The realization result is as follows:

insert image description here

Code idea:

  • Use the GridPane layout to implement the required TextField, Label, RadioButton, and Button , and set the position
  • Set the SetonAction method for the two Buttons

code show as below:

package GUIs;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.image.Image;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class TemperatureChange extends Application{
    
    
    public static void main(String[] args) {
    
    
        launch(args);
    }
    @Override
    public void start(Stage mystage) throws Exception{
    
            
        GridPane root = new GridPane();
        root.setStyle("-fx-background-color:linear-gradient(to right,#ff00ff,#ffff00)");
        
        Button btn1 = new Button("温度转换");
        btn1.setFont(Font.font(16));
        Button btn2 = new Button("退出");
        btn2.setFont(Font.font(16));
        Label lab1 = new Label("摄氏度°C  =");
        lab1.setFont(Font.font(16));
        Label lab2 = new Label("华氏度°F");
        lab2.setFont(Font.font(16));
        TextField txt1 = new TextField("");
        txt1.setFont(Font.font(16));
        txt1.setPrefWidth(100);
        TextField txt2 = new TextField();
        txt2.setPrefWidth(50);
        txt2.setFont(Font.font(16));
        RadioButton rb1 = new RadioButton("摄氏转华氏");
        rb1.setFont(Font.font(16));
        RadioButton rb2 = new RadioButton("华氏转摄氏");
        rb2.setFont(Font.font(16));
        ToggleGroup tg = new ToggleGroup();
        rb1.setToggleGroup(tg);
        rb2.setToggleGroup(tg);
        
        root.add(txt1, 0, 0);
        root.add(lab1, 1, 0);
        root.add(txt2, 2, 0);
        root.add(lab2, 3, 0);
        root.add(rb1, 1, 1);
        root.add(rb2, 2, 1);
        root.add(btn1, 1, 2);
        root.add(btn2, 2, 2);
        root.setHgap(20);//水平间距
        root.setVgap(35);//垂直间距
        root.setAlignment(Pos.CENTER);
               
        btn1.setOnAction(new EventHandler<ActionEvent>(){
    
    
            @Override
            public void handle(ActionEvent event){
    
    
            double tem1;
            double tem2;
            double c ;
            double f;
            if(rb1.isSelected()){
    
    
                tem1 = Double.valueOf(txt1.getText()) ;
                f=1.8*tem1+32;
                txt2.setText(String.format("%.2f", f));
            }
            if( rb2.isSelected()){
    
    
                tem2 = Double.valueOf(txt2.getText()) ;
                c=5*(tem2-32)/9;
                txt1.setText(String.format("%.2f", c));
            }
            }
        });
        btn2.setOnAction(new EventHandler<ActionEvent>() {
    
    
            @Override
            public void handle(ActionEvent event) {
    
    
                System.exit(0);
            }
        });
        
        Scene scene = new Scene(root, 600, 300);
        mystage.setTitle("TemperatureChange");
        mystage.getIcons().add(new Image("Java.png"));
        mystage.setScene(scene);
        mystage.show();
    }
}
  • I made a small modification, because I found that there was a small problem when the previous code was running, and then changed the appearance by the way.
  • This is the picture from the previous version
    insert image description here

Guess you like

Origin blog.csdn.net/m0_50115641/article/details/111144826