如何在javafx

时间:2015-11-28 06:39:50

标签: java javafx

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

 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));

  });

}

}

我写了这篇文章,但它没有显示任何东西。我教了它因为错误的方法,但它没有用。有谁能告诉我我犯了什么错误。

2 个答案:

答案 0 :(得分:0)

您应该只有1 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);
    }

}

答案 1 :(得分:0)

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

MainController.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") :

MainLayout.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 文件:

App.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();

    }

}

注意:我使用 Scene Builder 作为 FXML 文件的编辑器

快乐编码