2015-11-28 53 views
-1

我應該製作一個簡單的計算器。如何在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)); 

    }); 

} 

} 

我寫了這一點,但它說沒什麼display.I教它的,因爲方法misplacment的,但它沒有工作。有沒有人能告訴我我犯了什麼錯誤。

+0

'stage.setScene(場景); stage.setScene(scene2);'第二行重新設置場景。所以第一場景被第二場景取代。這導致添加到第一個場景中的任何節點都不會在舞臺上呈現。只使用一個場景。 –

回答

0

您應該只有1 scene。在你的例子中,你可以把你的盒子放入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); 
    } 

}