JavaFX编程第一小题源代码
package javaseniorprograme; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.CheckBox; import javafx.scene.control.ComboBox; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.text.Font; import javafx.scene.text.FontPosture; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; import javafx.stage.Stage; /** * 16.14 * @author ASUS */ public class Exercise16_14 extends Application{ @Override public void start(Stage stage){ // 文字 Text[] text = new Text[]{new Text("Font Name"),new Text("Font Size"),new Text("Come On! Freestyle")}; text[0].setFont(new Font(14)); text[1].setFont(new Font(14)); String[] str = (String[])Font.getFamilies().toArray(); // ComboBox<String> ComboBox<String> left = new ComboBox<>(); ObservableList<String> items1 = FXCollections.observableArrayList(str); left.getItems().addAll(items1); // ComboBox<Integer> ComboBox<Integer> right = new ComboBox<>(); Integer[] integer = new Integer[100]; for(int i = 1; i < 101; i++){ integer[i-1]=new Integer(i); } ObservableList<Integer> items2 = FXCollections.observableArrayList(integer); right.getItems().addAll(items2); // CheckBox CheckBox[] cb = new CheckBox[]{new CheckBox("Bold"),new CheckBox("Italic")}; // HBox HBox hbox1 = new HBox(); hbox1.getChildren().addAll(text[0],left,text[1],right); HBox hbox2 = new HBox(); hbox2.getChildren().addAll(cb[0],cb[1]); hbox2.setSpacing(20); hbox2.setAlignment(Pos.CENTER); // BorderPane BorderPane borderPane = new BorderPane(); borderPane.setTop(hbox1); borderPane.setCenter(text[2]); borderPane.setBottom(hbox2); Scene scene = new Scene(borderPane,500,300); stage.setTitle("Exercise16_14"); stage.setScene(scene); stage.show(); EventHandler<ActionEvent> action = new EventHandler<ActionEvent>(){ @Override public void handle(ActionEvent e){ if(cb[0].isSelected()){ if(cb[1].isSelected()){ text[2].setFont(Font.font(str[items1.indexOf(left.getValue())+1],FontWeight.BOLD,FontPosture.ITALIC,integer[items2.indexOf(right.getValue())+1])); }else{ text[2].setFont(Font.font(str[items1.indexOf(left.getValue())+1],FontWeight.BOLD,integer[items2.indexOf(right.getValue())+1])); } }else{ if(cb[1].isSelected()){ text[2].setFont(Font.font(str[items1.indexOf(left.getValue())+1],FontPosture.ITALIC,integer[items2.indexOf(right.getValue())+1])); }else{ text[2].setFont(Font.font(str[items1.indexOf(left.getValue())+1],integer[items2.indexOf(right.getValue())+1])); } } } }; left.setOnAction(action); right.setOnAction(action); cb[0].setOnAction(action); cb[1].setOnAction(action); } public static void main(String[] args){ Application.launch(args); } }
爱我没结果!