iT邦幫忙

1

javafx製作出a+b=c

程式設計課要我們弄這個出來
我上網爬很多文章還是不會QQ
希望有大神能幫忙解答!

haward79 iT邦研究生 3 級 ‧ 2021-11-07 18:56:23 檢舉
老師應該有給詳細的 spec 吧?
提供一下吧!
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

2
海綿寶寶
iT邦大神 1 級 ‧ 2021-11-08 11:21:12
最佳解答

https://ithelp.ithome.com.tw/upload/images/20211108/20001787nYoFFkgqmF.png

import javafx.application.Application; 
import static javafx.application.Application.launch; 
import javafx.geometry.Insets; 
import javafx.geometry.Pos; 

import javafx.scene.Scene; 
import javafx.event.EventHandler; 
import javafx.scene.control.Button; 
import javafx.scene.layout.GridPane; 
import javafx.scene.text.Text; 
import javafx.scene.control.TextField; 
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;  
         
public class HelloWorld extends Application { 
   @Override 
   public void start(Stage stage) {      
      //creating label email 
      Text text1 = new Text("a");             
      //creating label password 
      Text text2 = new Text("b"); 
      //creating label password 
      Text text3 = new Text("a+b=c"); 
       
      //Creating Text Filed for email        
      TextField textField1 = new TextField();       
      
      //Creating Text Filed for password        
      TextField textField2 = new TextField();  
      //Creating Text Filed for password        
      Text textField3 = new Text("");  
       
      //Creating Buttons 
      Button button1 = new Button("Add"); 
      Button button2 = new Button("Clear");  
      
      button1.setOnMouseClicked((new EventHandler<MouseEvent>() { 
		public void handle(MouseEvent event) { 
			Integer sum = Integer.parseInt(textField1.getText())+Integer.parseInt(textField2.getText());
			textField3.setText(
			sum.toString()
			);
		} 
		}));
      
      button2.setOnMouseClicked((new EventHandler<MouseEvent>() { 
		public void handle(MouseEvent event) { 
			textField1.setText("");
			textField2.setText("");
			textField3.setText("");
		} 
		}));
		
      //Creating a Grid Pane 
      GridPane gridPane = new GridPane();    
      
      //Setting size for the pane 
      gridPane.setMinSize(400, 200); 
      
      //Setting the padding  
      gridPane.setPadding(new Insets(10, 10, 10, 10)); 
      
      //Setting the vertical and horizontal gaps between the columns 
      gridPane.setVgap(5); 
      gridPane.setHgap(5);       
      
      //Setting the Grid alignment 
      gridPane.setAlignment(Pos.CENTER); 
       
      //Arranging all the nodes in the grid 
      gridPane.add(text1, 0, 0); 
      gridPane.add(textField1, 1, 0); 
      gridPane.add(text2, 0, 1);       
      gridPane.add(textField2, 1, 1); 
      gridPane.add(text3, 0, 2); 
      gridPane.add(textField3, 1, 2); 
      gridPane.add(button1, 0, 3); 
      gridPane.add(button2, 1, 3); 
       
      //Styling nodes  
      button1.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;"); 
      button2.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;"); 
       
      text1.setStyle("-fx-font: normal bold 20px 'serif' "); 
      text2.setStyle("-fx-font: normal bold 20px 'serif' ");  
      text3.setStyle("-fx-font: normal bold 20px 'serif' ");  
      gridPane.setStyle("-fx-background-color: BEIGE;"); 
       
      //Creating a scene object 
      Scene scene = new Scene(gridPane); 
       
      //Setting title to the Stage 
      stage.setTitle("a+b=c"); 
         
      //Adding scene to the stage 
      stage.setScene(scene);
      
      //Displaying the contents of the stage 
      stage.show(); 
   }      
   public static void main(String args[]){ 
      launch(args); 
   } 
}

另外
你說你爬很多文章
我敢說你沒有爬到這篇

Kuma君 iT邦新手 5 級 ‧ 2021-11-08 16:04:22 檢舉

可能他不太能靈活運用吧
就如同即使給了 V=IR 給學生
可能我想要求 電流等於多少時
若他無法變化 I= V/R 的話,一樣無法作答。

我要發表回答

立即登入回答