javaFx
Posted dup4
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javaFx相关的知识,希望对你有一定的参考价值。
Hw8
1 import javafx.application.Application; 2 import javafx.geometry.Insets; 3 import javafx.geometry.Pos; 4 import javafx.scene.Scene; 5 import javafx.scene.control.Button; 6 import javafx.scene.control.Label; 7 import javafx.scene.control.TextField; 8 import javafx.scene.layout.HBox; 9 import javafx.scene.layout.VBox; 10 import javafx.stage.Stage; 11 12 public class Homework8 extends Application { 13 14 private final int WINDOWS_LENGTH = 600; 15 private final int WINDOWS_HEIGHT = 100; 16 17 private Label lab_text; 18 private TextField TF_text; 19 private Button Btn_For, Btn_Back; 20 21 private HBox Hpane; 22 private VBox Vpane; 23 24 public void start(Stage MyStage) throws Exception { 25 MyStage.setTitle("String Game"); 26 27 lab_text = new Label("Click "Forward" to run a word, click "Backward" to reverse"); 28 TF_text = new TextField(); 29 Btn_For = new Button("Forward"); 30 Btn_Back = new Button("Backward"); 31 32 Btn_For.setOnAction(e-> 33 { 34 String str = TF_text.getText(); 35 int Rand = ((int)(Math.random() * 100)) & 1; 36 if (Rand == 1) str += "male"; 37 else str += "female"; 38 TF_text.setText(str); 39 }); 40 41 Btn_Back.setOnAction(e-> 42 { 43 String str = TF_text.getText(); 44 String now = ""; 45 if (str.length() != 0) 46 { 47 if (str.length() <= 6) TF_text.setText(""); 48 else 49 { 50 int pos = 0; 51 if (str.charAt(str.length() - 6) == ‘f‘) 52 pos = str.length() - 6; 53 else 54 pos = str.length() - 4; 55 for (int i = 0; i < pos; ++i) now += str.charAt(i); 56 TF_text.setText(now); 57 } 58 } 59 }); 60 61 Hpane = new HBox(15); 62 Hpane.setAlignment(Pos.CENTER); 63 Hpane.getChildren().addAll(Btn_For, Btn_Back); 64 65 Vpane = new VBox(15); 66 Vpane.setAlignment(Pos.CENTER); 67 Vpane.setPadding(new Insets(40, 40, 40, 40)); 68 Vpane.getChildren().addAll(lab_text, TF_text, Hpane); 69 70 Scene MyScene = new Scene(Vpane, this.WINDOWS_LENGTH, this.WINDOWS_HEIGHT); 71 MyStage.setScene(MyScene); 72 MyStage.show(); 73 } 74 75 public static void main(String args[]) { launch(args); } 76 }
T9
1 import javafx.application.Application; 2 import javafx.geometry.Insets; 3 import javafx.geometry.Pos; 4 import javafx.scene.Scene; 5 import javafx.scene.layout.BorderPane; 6 import javafx.scene.control.Button; 7 import javafx.scene.control.Label; 8 import javafx.scene.control.TextField; 9 import javafx.scene.layout.HBox; 10 import javafx.stage.Stage; 11 12 public class Test9 extends Application { 13 14 private final int WINDOWS_LENGTH = 800; 15 private final int WINDOWS_HEIGHT = 100; 16 17 private Label label_num1, label_num2, label_res; 18 private TextField TF_num1, TF_num2, TF_res; 19 private Button Btn_Add, Btn_Sub, Btn_Mul, Btn_Div; 20 21 private BorderPane pane; 22 private HBox firstPane; 23 private HBox secondPane; 24 25 public void start(Stage MyStage) throws Exception { 26 MyStage.setTitle("Calculator"); 27 28 label_num1 = new Label("Number 1: "); 29 TF_num1 = new TextField(); 30 31 label_num2 = new Label("Number 2: "); 32 TF_num2 = new TextField(); 33 34 label_res = new Label("Result: "); 35 TF_res = new TextField(); 36 37 Btn_Add = new Button("Add"); 38 Btn_Sub = new Button("Subtract"); 39 Btn_Mul = new Button("Multiply"); 40 Btn_Div = new Button("Divide"); 41 42 Btn_Add.setOnAction(e->{TF_res.setText(String.valueOf(Double.valueOf(TF_num1.getText()) + Double.valueOf(TF_num2.getText())));}); 43 Btn_Sub.setOnAction(e->{TF_res.setText(String.valueOf(Double.valueOf(TF_num1.getText()) - Double.valueOf(TF_num2.getText())));}); 44 Btn_Mul.setOnAction(e->{TF_res.setText(String.valueOf(Double.valueOf(TF_num1.getText()) * Double.valueOf(TF_num2.getText())));}); 45 Btn_Div.setOnAction(e->{TF_res.setText(String.valueOf(Double.valueOf(TF_num1.getText()) / Double.valueOf(TF_num2.getText())));}); 46 47 firstPane = new HBox(15); 48 firstPane.setAlignment(Pos.CENTER); 49 firstPane.setPadding(new Insets(20, 0, 0, 0)); 50 firstPane.getChildren().addAll(label_num1, TF_num1, label_num2, TF_num2, label_res, TF_res); 51 52 secondPane = new HBox(15); 53 secondPane.setAlignment(Pos.CENTER); 54 secondPane.getChildren().addAll(Btn_Add, Btn_Sub, Btn_Mul, Btn_Div); 55 56 pane = new BorderPane(); 57 pane.setTop(firstPane); 58 pane.setCenter(secondPane); 59 60 Scene MyScene = new Scene(pane, this.WINDOWS_LENGTH, this.WINDOWS_HEIGHT); 61 MyStage.setScene(MyScene); 62 MyStage.show(); 63 } 64 65 public static void main(String args[]) { launch(args); } 66 }
T10
1 import javafx.application.Application; 2 import javafx.geometry.Pos; 3 import javafx.scene.Scene; 4 import javafx.scene.control.Button; 5 import javafx.scene.control.CheckBox; 6 import javafx.scene.control.Label; 7 import javafx.scene.control.TextField; 8 import javafx.scene.layout.GridPane; 9 import javafx.scene.layout.HBox; 10 import javafx.scene.layout.VBox; 11 import javafx.scene.text.Font; 12 import javafx.scene.text.FontWeight; 13 import javafx.stage.Stage; 14 15 public class Test10 extends Application { 16 17 private final int WINDOWS_LENGTH = 500; 18 private final int WINDOWS_HEIGHT = 300; 19 20 final String names[] = new String[] {"商品类型", "商品单价", "数量", "小计", "总计", "108", "28", "8", "11朵玫瑰", "精美包装盒", "贺卡", "计算", "重置"}; 21 private Label[] label_names = new Label[names.length - 5]; 22 private TextField[] tfs = new TextField[7]; 23 private Button[] btns = new Button[2]; 24 private CheckBox[] cbs = new CheckBox[3]; 25 private GridPane pane; 26 private HBox hbox; 27 private VBox vbox; 28 29 public void start(Stage MyStage) throws Exception { 30 MyStage.setTitle("Rose Shop"); 31 32 for (int i = 0; i < names.length - 5; ++i) label_names[i] = new Label(names[i]); 33 for (int i = 0; i < 4; ++i) label_names[i].setFont(Font.font("Timer New Roman", FontWeight.BOLD, 15)); 34 for (int i = 0; i < 7; ++i) tfs[i] = new TextField(); 35 for (int i = 3; i < 7; ++i) tfs[i].setDisable(true); 36 for (int i = 0; i < 3; ++i) cbs[i] = new CheckBox(names[i + 8]); 37 for (int i = 0; i < 2; ++i) btns[i] = new Button(names[i + 11]); 38 39 pane = new GridPane(); 40 hbox = new HBox(15); 41 vbox = new VBox(15); 42 hbox.setAlignment(Pos.CENTER); 43 vbox.setAlignment(Pos.CENTER); 44 pane.setAlignment(Pos.CENTER); 45 pane.setHgap(10); 46 pane.setVgap(10); 47 48 for (int i = 0; i < 4; ++i) pane.add(label_names[i], i, 0); 49 for (int i = 0; i < 3; ++i) pane.add(cbs[i], 0, i + 1); 50 for (int i = 0; i < 3; ++i) pane.add(label_names[i + 5], 1, i + 1); 51 for (int i = 0; i < 2; ++i) for (int j = 0; j < 3; ++j) pane.add(tfs[3 * i + j], i + 2, j + 1); 52 pane.add(label_names[4], 3, 4); 53 pane.add(tfs[6], 3, 5); 54 hbox.getChildren().addAll(btns[0], btns[1]); 55 vbox.getChildren().addAll(pane, hbox); 56 57 btns[0].setOnAction(e->{ 58 int res = 0; 59 int[] price = new int[]{108, 28, 8}; 60 for (int i = 0; i < 3; ++i) 61 { 62 if (cbs[i].isSelected() == false) continue; 63 double num; 64 try 65 { 66 num = Double.valueOf(tfs[i].getText()); 67 }catch(Exception Exception) { continue; } 68 double tmp = price[i] * num; 69 tfs[3 + i].setText(String.valueOf(tmp)); 70 res += tmp; 71 } 72 tfs[6].setText(String.valueOf(res)); 73 }); 74 75 btns[1].setOnAction(e-> 76 { 77 for (int i = 0; i < 7; ++i) tfs[i].setText(""); 78 for (int i = 0; i < 3; ++i) cbs[i].setSelected(false); 79 }) ; 80 81 Scene MyScene = new Scene(vbox, this.WINDOWS_LENGTH, this.WINDOWS_HEIGHT); 82 MyStage.setScene(MyScene); 83 MyStage.show(); 84 } 85 86 public static void main(String args[]) { launch(args); } 87 }
以上是关于javaFx的主要内容,如果未能解决你的问题,请参考以下文章
初始化后,在javafx中的控制器之间传递变量[duplicate]