如何在javafx上设置操作

w41d8nur  于 2023-02-18  发布在  Java
关注(0)|答案(2)|浏览(118)

我应该做一个简单的计算器。

import javafx.application.Application;
 import javafx.geometry.Pos;
 import javafx.scene.Scene;
 import javafx.scene.layout.HBox;
 import javafx.stage.Stage;
 import javafx.scene.control.Button;
 import javafx.scene.control.Label;
   import javafx.scene.control.TextField;

   public class SimpleCalculaterFX extends Application {
   public static void main(String[] args) {
    launch(args);
   }

   @Override
   public void start(Stage stage) {
    // TODO Auto-generated method stub
   stage.setTitle("simple Calculater");
   HBox hbox = new HBox();
   HBox hbox2 = new HBox();
   Scene scene = new Scene(hbox);
   Scene scene2 = new Scene(hbox2);
   Label lb1 = new Label("Nuber1");
   Label lb2 = new Label("Nuber2");
   Label lb3 = new Label("Result");
   TextField tx1 = new TextField();
   TextField tx2 = new TextField();
   TextField tx3 = new TextField();
   Button b1= new Button("ADD");
   Button b2= new Button("SUBTRACT");
   Button b3= new Button("DIVIDE");
   Button b4= new Button("MULTIPLY");
   hbox.getChildren().addAll(lb1,tx1,lb2,tx2,lb3,tx3);
   hbox.setAlignment(Pos.TOP_RIGHT);
   hbox2.getChildren().addAll(b1,b2,b3,b4);
   hbox2.setAlignment(Pos.BOTTOM_CENTER);
   stage.setScene(scene);
   stage.setScene(scene2);
    stage.show();

   //  Integer num1 = Integer.parseInt(tx1.getText());
   //nteger num2 = Integer.parseInt(tx2.getText());
   b1.setOnAction(e->{

      Integer num1 = Integer.parseInt(tx1.getText());
      Integer num2 = Integer.parseInt(tx2.getText());
      Integer res;
      res= num1+num2;
      tx3.setText(String.format("%d", res));
   });
   b2.setOnAction(e->{

      Integer num1 = Integer.parseInt(tx1.getText());
      Integer num2 = Integer.parseInt(tx2.getText());
      Integer res;
      res= num1-num2;
      tx3.setText(String.format("%d", res));

   });
   b3.setOnAction(e->{

      Integer num1 = Integer.parseInt(tx1.getText());
      Integer num2 = Integer.parseInt(tx2.getText());
      Integer res;
      res=num1/num2;
      tx3.setText(String.format("%d", res));

   });
   b4.setOnAction(e->{

      Integer num1 = Integer.parseInt(tx1.getText());
      Integer num2 = Integer.parseInt(tx2.getText());
      Integer res;
      res= num1*num2;
      tx3.setText(String.format("%d", res));

  });

}

}

我写了这个,但它说什么显示。我教它,因为错位的方法,但它没有工作。有谁能告诉我,我犯了错误。

sxpgvts3

sxpgvts31#

你应该只有一个scene,在你的例子中,你可以把你的hbox放到一个vbox中,而把vbox放到scene中。
下面是代码的修改版本:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class SimpleCalculaterFX extends Application {

    @Override
    public void start(Stage stage) {

        stage.setTitle("simple Calculater");

        Label lb1 = new Label("Nuber1");
        Label lb2 = new Label("Nuber2");
        Label lb3 = new Label("Result");
        TextField tx1 = new TextField();
        TextField tx2 = new TextField();
        TextField tx3 = new TextField();

        HBox hbox = new HBox();
        hbox.getChildren().addAll(lb1, tx1, lb2, tx2, lb3, tx3);
        hbox.setAlignment(Pos.TOP_RIGHT);

        Button b1 = new Button("ADD");
        Button b2 = new Button("SUBTRACT");
        Button b3 = new Button("DIVIDE");
        Button b4 = new Button("MULTIPLY");

        HBox hbox2 = new HBox();
        hbox2.getChildren().addAll(b1, b2, b3, b4);
        hbox2.setAlignment(Pos.BOTTOM_CENTER);

        VBox vBox = new VBox();
        vBox.getChildren().addAll( hbox, hbox2);

        Scene scene = new Scene(vBox);
        stage.setScene(scene);
        stage.show();

        // Integer num1 = Integer.parseInt(tx1.getText());
        // nteger num2 = Integer.parseInt(tx2.getText());
        b1.setOnAction(e -> {

            Integer num1 = Integer.parseInt(tx1.getText());
            Integer num2 = Integer.parseInt(tx2.getText());
            Integer res;
            res = num1 + num2;
            tx3.setText(String.format("%d", res));
        });
        b2.setOnAction(e -> {

            Integer num1 = Integer.parseInt(tx1.getText());
            Integer num2 = Integer.parseInt(tx2.getText());
            Integer res;
            res = num1 - num2;
            tx3.setText(String.format("%d", res));

        });
        b3.setOnAction(e -> {

            Integer num1 = Integer.parseInt(tx1.getText());
            Integer num2 = Integer.parseInt(tx2.getText());
            Integer res;
            res = num1 / num2;
            tx3.setText(String.format("%d", res));

        });
        b4.setOnAction(e -> {

            Integer num1 = Integer.parseInt(tx1.getText());
            Integer num2 = Integer.parseInt(tx2.getText());
            Integer res;
            res = num1 * num2;
            tx3.setText(String.format("%d", res));

        });

    }

    public static void main(String[] args) {
        launch(args);
    }

}
3df52oht

3df52oht2#

首先,你需要创建一个控制器类,并在其中声明你的按钮:

主控制器.java

public class MainController implements Initializable {
    
    @FXML
    private Button button1;
    
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        System.out.println("The document loaded!!!");
        
        button1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("button1 clicked!!");
            }
        });
        
    }

}

将控制器链接到FXML文件(fx:controller="ma.desktop.MainController"):

主布局.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>

<Pane fx:controller="ma.desktop.MainController" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="472.0" prefWidth="692.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Button fx:id="button1" layoutX="65.0" layoutY="14.0" mnemonicParsing="false" text="Button" textFill="#19d713" />
   </children>
</Pane>

最后,在应用程序类上加载FXML文件:

Java应用程序

public class App extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        Pane pane = FXMLLoader.load(getClass().getResource("MainLayout.fxml"));

        Scene scene = new Scene(pane);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

}

注意:我使用场景生成器作为FXML文件的编辑器

快乐编码

相关问题