JavaFX是Java的下一代图形用户界面工具包。JavaFX是一组图形和媒体API,我们可以用它们来创建和部署富客户端应用程序。
JAVAFX案例
Fxml载入项目
public void start(Stage stage) throws Exception { stage.setTitle("calculator"); Pane load = FXMLLoader.load(getClass().getResource("cal.fxml")); Scene scene =new Scene(load); stage.setScene(scene); stage.show(); }
或
Parent load = FXMLLoader.load(getClass().getResource("src/brickcatalogue/viewpane/brickgame.fxml"));
Fxml添加事件
public class eventcc { @FXML public void onevent(Event event){ EventType<? extends Event> eventType = event.getEventType(); System.out.println(eventType); } }
从Fxml通过id引用控件
模拟简易计算器
calculator
public class calculator extends Application { public static void main(String[] args) { launch(); } @Override public void start(Stage stage) throws Exception { stage.setTitle("calculator"); Pane load = FXMLLoader.load(getClass().getResource("cal.fxml")); Scene scene =new Scene(load); stage.setScene(scene); stage.show(); } }
cal.fxml
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Button?> <?import javafx.scene.control.Label?> <?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.text.Font?> <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="431.0" prefWidth="458.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="eventcc"> <children> <AnchorPane layoutX="1.0" layoutY="-3.0" prefHeight="126.0" prefWidth="458.0" style="-fx-background-color: #cdcdcd;"> <children> <Label fx:id="vid" layoutX="375.0" layoutY="39.0" prefHeight="15.0" prefWidth="55.0" text="0" /> <Label fx:id="result" layoutX="349.0" layoutY="68.0" text="0"> <font> <Font size="34.0" /> </font> </Label> </children> </AnchorPane> <AnchorPane layoutX="5.0" layoutY="124.0" prefHeight="307.0" prefWidth="450.0"> <children> <Button layoutX="14.0" layoutY="14.0" mnemonicParsing="false" text="/" onAction="#onevent"/> <Button layoutX="14.0" layoutY="51.0" mnemonicParsing="false" text="Button" onAction="#onevent"/> <Button layoutX="14.0" layoutY="89.0" mnemonicParsing="false" text="7" onAction="#onevent"/> <Button layoutX="14.0" layoutY="130.0" mnemonicParsing="false" text="4" onAction="#onevent"/> <Button layoutX="14.0" layoutY="166.0" mnemonicParsing="false" text="1" onAction="#onevent"/> <Button layoutX="14.0" layoutY="210.0" mnemonicParsing="false" text="+/-" onAction="#onevent"/> <Button layoutX="96.0" layoutY="14.0" mnemonicParsing="false" text="CE" onAction="#onevent"/> <Button layoutX="96.0" layoutY="51.0" mnemonicParsing="false" text="Button" onAction="#onevent"/> <Button layoutX="96.0" layoutY="89.0" mnemonicParsing="false" text="8" onAction="#onevent"/> <Button layoutX="96.0" layoutY="130.0" mnemonicParsing="false" text="5" onAction="#onevent"/> <Button layoutX="96.0" layoutY="166.0" mnemonicParsing="false" text="2" onAction="#onevent"/> <Button layoutX="96.0" layoutY="210.0" mnemonicParsing="false" text="0" onAction="#onevent"/> <Button layoutX="197.0" layoutY="14.0" mnemonicParsing="false" text="C" onAction="#onevent"/> <Button layoutX="197.0" layoutY="51.0" mnemonicParsing="false" text="Button" onAction="#onevent"/> <Button layoutX="197.0" layoutY="89.0" mnemonicParsing="false" text="9" onAction="#onevent"/> <Button layoutX="197.0" layoutY="130.0" mnemonicParsing="false" text="6" onAction="#onevent"/> <Button layoutX="197.0" layoutY="166.0" mnemonicParsing="false" text="3" onAction="#onevent"/> <Button layoutX="197.0" layoutY="210.0" mnemonicParsing="false" text="." onAction="#onevent"/> <Button layoutX="299.0" layoutY="14.0" mnemonicParsing="false" text="x" onAction="#onevent"/> <Button layoutX="299.0" layoutY="51.0" mnemonicParsing="false" text="后退" onAction="#returnval"/> <Button layoutX="299.0" layoutY="89.0" mnemonicParsing="false" text="\%" onAction="#onevent"/> <Button layoutX="299.0" layoutY="130.0" mnemonicParsing="false" text="*" onAction="#onevent"/> <Button layoutX="299.0" layoutY="166.0" mnemonicParsing="false" text="-" onAction="#onevent"/> <Button layoutX="299.0" layoutY="210.0" mnemonicParsing="false" text="=" onAction="#onevent"/> </children> </AnchorPane> </children> </AnchorPane>
eventcc
import javafx.event.Event; import javafx.event.EventTarget; import javafx.event.EventType; import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.Label; public class eventcc { @FXML private Label vid; @FXML private Label result; boolean isNumber = true; @FXML public void onevent(Event event){ EventTarget target = event.getTarget(); Button button =(Button) target; System.out.println(button.getText()); //获取button的值 String text= button.getText(); try { long i = Long.parseLong(text); if (isNumber){ String resultval =result.getText(); long newVal =Long.parseLong(resultval) * 10 +i; result.setText(newVal +""); }else { result.setText(text); } }catch (Exception ex){ switch (text){ case "+": // long a = Long.parseLong(result.getText()); vid.setText(result.getText() + "+"); break; case "-": vid.setText(result.getText() + "-"); break; case "*": vid.setText(result.getText() + "*"); break; case "%": vid.setText(result.getText() + "%"); break; case "=": String express = vid.getText() +result.getText(); vid.setText(vid.getText() +result.getText()+"="); if (express.contains("+")){ String[] split =express.split("\\+"); long jisuan = jisuan(Integer.parseInt(split[0]), Integer.parseInt(split[1]), "+"); result.setText(String.valueOf(jisuan)); }else if (express.contains("-")){ String[] split =express.split("\\-"); long jisuan = jisuan(Integer.parseInt(split[0]), Integer.parseInt(split[1]), "-"); result.setText(String.valueOf(jisuan)); }else if (express.contains("*")){ String[] split =express.split("\\*"); long jisuan = jisuan(Integer.parseInt(split[0]), Integer.parseInt(split[1]), "*"); result.setText(String.valueOf(jisuan)); }else if (express.contains("%")){ String[] split =express.split("\\%"); long jisuan = jisuan(Integer.parseInt(split[0]), Integer.parseInt(split[1]), "%"); result.setText(String.valueOf(jisuan)); } break; default: break; } } isNumber =false; } private long jisuan(int a,int b ,String operator) { long result = 0; switch (operator){ case "+": result = a + b; break; case "-": result = a - b; break; case "*": result = a * b; break; case "%": result = a % b; break; case "=": break; default: break; } return result; } //退一步 @FXML public void returnval(Event event){ try { String resultval =result.getText(); long newVal =Long.parseLong(resultval) / 10; result.setText(newVal +""); }catch (Exception ex){ } } }
简易拖拽事件
Main类
public class DragMain extends Application { public static Scene scene; public static Stage stage; public static void main(String[] args) { launch(); } @Override public void start(Stage stage) throws Exception { stage.setTitle("SimpleDrag"); Pane load = FXMLLoader.load(getClass().getResource("com/javafx/view/Windows.fxml")); //或者parent scene =new Scene(load); stage.setScene(scene); stage.show(); } }
Windows.fxml
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Button?> <?import javafx.scene.control.Label?> <?import javafx.scene.control.Menu?> <?import javafx.scene.control.MenuBar?> <?import javafx.scene.control.MenuItem?> <?import javafx.scene.control.SplitPane?> <?import javafx.scene.control.TextField?> <?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.text.Font?> <AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.javafx.controller.WindosController"> <children> <SplitPane dividerPositions="0.29797979797979796" layoutX="-1.0" layoutY="-3.0" prefHeight="408.0" prefWidth="600.0"> <items> <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="398.0" prefWidth="121.0"> <children> <TextField fx:id="xPostion" layoutX="43.0" layoutY="99.0" prefHeight="23.0" prefWidth="111.0" /> <Label layoutX="14.0" layoutY="103.0" text="X" /> <Label layoutX="14.0" layoutY="145.0" text="Y" /> <TextField fx:id="yPosition" layoutX="43.0" layoutY="140.0" prefHeight="23.0" prefWidth="111.0" /> <TextField fx:id="TextContent" layoutX="43.0" layoutY="186.0" prefHeight="23.0" prefWidth="111.0" /> <Label layoutX="9.0" layoutY="189.0" text="Text" /> <Button fx:id="determine" layoutX="62.0" layoutY="256.0" mnemonicParsing="false" text="确定" onAction="#updateProperties"> <font> <Font size="16.0" /> </font> </Button> <MenuBar layoutY="1.0" prefHeight="34.0" prefWidth="175.0"> <menus> <Menu mnemonicParsing="false" text="组件"> <items> <MenuItem mnemonicParsing="false" onAction="#selectLable" text="标签" /> <MenuItem mnemonicParsing="false" onAction="#selectButton" text="形状" /> <MenuItem mnemonicParsing="false" onAction="#cancelselect" text="取消选中" /> </items> </Menu> <Menu mnemonicParsing="false" onAction="#delect" text="操作"> <items> <MenuItem mnemonicParsing="false" text="Delete" /> </items> </Menu> <Menu mnemonicParsing="false" text="帮助"> <items> <MenuItem mnemonicParsing="false" text="About" /> </items> </Menu> </menus> </MenuBar> </children> </AnchorPane> <AnchorPane fx:id="contendPaneShow" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" /> </items> </SplitPane> </children> </AnchorPane>
fx:controller="com.javafx.controller.WindosController"
WindosController
public class WindosController { private static final int LABEL =1; private static final int BUTTON =2; private Node selected; private int currentType=0; @FXML private AnchorPane contendPaneShow; @FXML private Button determine; @FXML private TextField xPostion; @FXML private TextField yPosition; @FXML private TextField TextContent; @FXML public void selectLable(Event event) { currentType =LABEL; } @FXML public void selectButton(Event event) { currentType = BUTTON; } @FXML public void cancelselect(Event event){currentType = 0;}; @FXML public void initialize(){ contendPaneShow.setOnMouseClicked(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { if (currentType <= 0){ return; } double x = event.getX(); double y =event.getY(); if (currentType == 2){ Rectangle rectangle = new Rectangle(); rectangle.setFill(Color.YELLOW); rectangle.setWidth(40); rectangle.setHeight(50); rectangle.setLayoutX(x); rectangle.setLayoutY(y); contendPaneShow.getChildren().add(rectangle); rectangle.setOnMouseClicked((e)->{ currentType =0; //将节点赋给selected selected=rectangle; xPostion.setText(String.valueOf(rectangle.getLayoutX())); yPosition.setText(String.valueOf(rectangle.getLayoutY())); TextContent.setText("形状"); }); //拖拽事件 contendPaneShow.setOnMouseDragged(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { if (selected != null) { selected.setLayoutX(event.getX()); selected.setLayoutY(event.getY()); xPostion.setText(String.valueOf(event.getX())); yPosition.setText(String.valueOf(event.getY())); } } }); } if (currentType == 1){ Label label =new Label("标签"); label.setStyle("-fx-background-color: #548e3f"); label.setLayoutX(x); label.setLayoutY(y); contendPaneShow.getChildren().add(label); label.setOnMouseClicked((e)->{ currentType =0; //将节点赋给selected selected=label; xPostion.setText(String.valueOf(label.getLayoutX())); yPosition.setText(String.valueOf(label.getLayoutY())); TextContent.setText(label.getText()); }); //拖拽事件 contendPaneShow.setOnMouseDragged(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { if (selected != null){ selected.setLayoutX(event.getX()); selected.setLayoutY(event.getY()); xPostion.setText(String.valueOf(label.getLayoutX())); yPosition.setText(String.valueOf(label.getLayoutY())); } } }); } } }); //determine.setAccessibleText(String.valueOf(KeyCombination.valueOf("Enter"))); } public void updateProperties(){ if (selected ==null) {return;} else { selected.setLayoutX(Double.parseDouble(xPostion.getText())); selected.setLayoutY(Double.parseDouble(yPosition.getText())); if (selected instanceof Rectangle){ Rectangle button = (Rectangle) selected; }else if (selected instanceof Label){ Label label = (Label) selected; label.setText(TextContent.getText()); } } } @FXML public void delect(){ boolean b = contendPaneShow.getChildren().removeAll(); System.out.println(b); } }
简易拖拽事件2
Main类
public class DragMain extends Application { public static Scene scene; public static Stage stage; public static void main(String[] args) { launch(); } @Override public void start(Stage stage) throws Exception { stage.setTitle("SimpleDrag"); Pane load = FXMLLoader.load(getClass().getResource("com/javafx/view/Windows.fxml")); //或者parent scene =new Scene(load); stage.setScene(scene); stage.show(); } }
Windows.fxml
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Button?> <?import javafx.scene.control.Label?> <?import javafx.scene.control.Menu?> <?import javafx.scene.control.MenuBar?> <?import javafx.scene.control.MenuItem?> <?import javafx.scene.control.SplitPane?> <?import javafx.scene.control.TextField?> <?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.text.Font?> <AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.javafx.controller.WindosController"> <children> <SplitPane dividerPositions="0.29797979797979796" layoutX="-1.0" layoutY="-3.0" prefHeight="408.0" prefWidth="600.0"> <items> <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="398.0" prefWidth="121.0"> <children> <TextField layoutX="43.0" layoutY="99.0" prefHeight="23.0" prefWidth="111.0" fx:id="xPostion" /> <Label layoutX="14.0" layoutY="103.0" text="X" /> <Label layoutX="14.0" layoutY="145.0" text="Y" /> <TextField fx:id="yPosition" layoutX="43.0" layoutY="140.0" prefHeight="23.0" prefWidth="111.0" /> <TextField fx:id="TextContent" layoutX="43.0" layoutY="186.0" prefHeight="23.0" prefWidth="111.0" /> <Label layoutX="9.0" layoutY="189.0" text="Text" /> <Button fx:id="determine" layoutX="62.0" layoutY="256.0" mnemonicParsing="false" onAction="#updateProperties" text="确定"> <font> <Font size="16.0" /> </font> </Button> <MenuBar layoutY="1.0" prefHeight="34.0" prefWidth="175.0"> <menus> <Menu mnemonicParsing="false" text="组件"> <items> <MenuItem mnemonicParsing="false" onAction="#selectLable" text="标签" /> <MenuItem mnemonicParsing="false" onAction="#selectButton" text="形状" /> <MenuItem mnemonicParsing="false" onAction="#cancelselect" text="取消选中" /> </items> </Menu> <Menu mnemonicParsing="false" text="操作"> <items> <MenuItem mnemonicParsing="false" onAction="#delect" text="清空" /> <MenuItem mnemonicParsing="false" onAction="#copy" text="复制" /> <MenuItem mnemonicParsing="false" onAction="#delete" text="删除" /> </items> </Menu> <Menu mnemonicParsing="false" text="帮助" > <items> <MenuItem mnemonicParsing="false" text="About" onAction="#helpShow"/> </items> </Menu> </menus> </MenuBar> <Label layoutX="9.0" layoutY="57.0" text="被选中组件数:" /> <TextField fx:id="conut" layoutX="108.0" layoutY="53.0" prefHeight="23.0" prefWidth="44.0" /> </children> </AnchorPane> <AnchorPane fx:id="contendPaneShow" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" /> </items> </SplitPane> </children> </AnchorPane>
fx:controller="com.javafx.controller.WindosController"
WindosController
public class WindosController { private static final int LABEL =1; private static final int BUTTON =2; private Node selected; private Polyline route = new Polyline(); private Set<Node> circled =new HashSet<>(); private int currentType=0; @FXML private AnchorPane contendPaneShow; @FXML private Button determine; @FXML private TextField xPostion; @FXML private TextField yPosition; @FXML private TextField TextContent; @FXML private TextField conut; @FXML public void selectLable(Event event) { currentType =LABEL; } @FXML public void selectButton(Event event) { currentType = BUTTON; } @FXML public void cancelselect(Event event){ currentType = 0; selected=null; xPostion.setText(""); yPosition.setText(""); TextContent.setText(""); }; @FXML public void initialize(){ contendPaneShow.getChildren().add(route); contendPaneShow.setOnMouseClicked(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { if (currentType <= 0){ return; } double x = event.getX(); double y =event.getY(); if (currentType == 2){ Rectangle rectangle = new Rectangle(); rectangle.setFill(Color.YELLOW); rectangle.setWidth(40); rectangle.setHeight(50); rectangle.setLayoutX(x); rectangle.setLayoutY(y); contendPaneShow.getChildren().add(rectangle); rectangle.setOnMouseClicked((e)->{ currentType =0; //将节点赋给selected selected=rectangle; xPostion.setText(String.valueOf(rectangle.getLayoutX())); yPosition.setText(String.valueOf(rectangle.getLayoutY())); TextContent.setText("形状"); }); } if (currentType == 1){ Label label =new Label("标签"); label.setStyle("-fx-background-color: #efb3b3"); label.setLayoutX(x); label.setLayoutY(y); contendPaneShow.getChildren().add(label); label.setOnMouseClicked((e)->{ currentType =0; //将节点赋给selected selected=label; xPostion.setText(String.valueOf(label.getLayoutX())); yPosition.setText(String.valueOf(label.getLayoutY())); TextContent.setText(label.getText()); }); } //拖拽事件 contendPaneShow.setOnMouseDragged(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { if (selected != null) { selected.setLayoutX(event.getX()); selected.setLayoutY(event.getY()); xPostion.setText(String.valueOf(event.getX())); yPosition.setText(String.valueOf(event.getY())); }else { route.getPoints().addAll(event.getX(),event.getY()); } } }); //鼠标释放事件 contendPaneShow.setOnMouseReleased(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { //Polygon可自动闭合 Polygon wrapper = new Polygon(); wrapper.getPoints().addAll(route.getPoints()); for (Node child : contendPaneShow.getChildren()) { if (child==route){ continue; } if (child instanceof Label ){ Label lab = (Label)child; if (iscontain(wrapper,lab.getLayoutX(),lab.getLayoutY(),lab.getWidth(),lab.getHeight())){ circled.add(lab); } } if (child instanceof Rectangle ){ Rectangle Rec = (Rectangle)child; if (iscontain(wrapper,Rec.getLayoutX(),Rec.getLayoutY(),Rec.getWidth(),Rec.getHeight())){ circled.add(Rec); } } } route.getPoints().clear(); if (circled.size() > 0){ int counts =circled.size(); conut.setText(String.valueOf(counts)); } } }); } }); } //是否包含组件 private boolean iscontain(Polygon wrapper,double x,double y,double width,double height){ return wrapper.contains(x,y) && wrapper.contains(x+width,y) && wrapper.contains(x,y+height) && wrapper.contains(x+width,y+height); } @FXML public void updateProperties(){ if (selected ==null) {return;} else { selected.setLayoutX(Double.parseDouble(xPostion.getText())); selected.setLayoutY(Double.parseDouble(yPosition.getText())); if (selected instanceof Rectangle){ Rectangle button = (Rectangle) selected; }else if (selected instanceof Label){ Label label = (Label) selected; label.setText(TextContent.getText()); } } } //清空 @FXML public void delect(ActionEvent actionEvent){ contendPaneShow.getChildren().clear(); xPostion.setText(""); yPosition.setText(""); TextContent.setText(""); conut.setText(""); circled.clear(); } // @FXML private void copy(ActionEvent actionEvent){ if (selected != null){ //单选 choiceCopy(selected); //多选 }else if (!circled.isEmpty()){ for (Node node : circled) { choiceCopy(node); } } } @FXML //删除 public void delete(ActionEvent actionEvent){ if (selected != null){ contendPaneShow.getChildren().remove(selected); xPostion.setText(""); yPosition.setText(""); TextContent.setText(""); if (!circled.isEmpty()){ for (Node node : circled) { contendPaneShow.getChildren().remove(node); xPostion.setText(""); yPosition.setText(""); TextContent.setText(""); } } } } @FXML public void helpShow(ActionEvent actionEvent){ Alert alert =new Alert(Alert.AlertType.CONFIRMATION); alert.setTitle("信息框"); alert.setHeaderText("simple拖拽"); alert.setContentText("simple拖拽版本1.0.0"); //alert.show(); Optional<ButtonType> buttonType =alert.showAndWait(); System.out.println(buttonType.get()); } private void choiceCopy(Node node) { if (node instanceof Label) { //赋值 Lable Label cope1 = new Label("cope1"); cope1.setLayoutX(node.getLayoutX() + 50); cope1.setLayoutY(node.getLayoutY() + 50); cope1.setStyle("-fx-background-color: #a3ebf5"); contendPaneShow.getChildren().add(cope1); //点击事件 cope1.setOnMouseClicked((e) -> { currentType = 0; //将节点赋给selected selected = cope1; xPostion.setText(String.valueOf(cope1.getLayoutX())); yPosition.setText(String.valueOf(cope1.getLayoutY())); TextContent.setText(cope1.getText()); }); } else if (node instanceof Rectangle) { //赋值 Rectangle cope2 = new Rectangle(); cope2.setFill(Color.BLUE); cope2.setStroke(Color.YELLOW); cope2.setWidth(40); cope2.setHeight(50); cope2.setLayoutX(node.getLayoutX() + 50); cope2.setLayoutY(node.getLayoutY() + 50); contendPaneShow.getChildren().add(cope2); cope2.setOnMouseClicked((e) -> { currentType = 0; //将节点赋给selected selected = cope2; xPostion.setText(String.valueOf(cope2.getLayoutX())); yPosition.setText(String.valueOf(cope2.getLayoutY())); TextContent.setText("形状"); }); } } }
JavaFX允许开发人员快速构建丰富的跨平台应用程序。JavaFX通过硬件加速图形支持现代GPU。
JavaFX允许开发人员在单个编程接口中组合图形,动画和UI控件。
图表编程语言可用于开发互联网应用程序(RIA)。JavaFX技术主要应用于创建Rich Internet applications(RIAs)。当前的JavaFX包括JavaFX脚本和JavaFX Mobile(一种运营于行动装置的操作系统),今后JavaFX将包括更多的产品。JavaFX Script编程语言(以下称为JavaFX)是一种声明性的、静态类型脚本语言。
JavaFX技术有着良好的前景,包括可以直接调用Java API的能力。因为 JavaFX Script是静态类型,它同样具有结构化代码、重用性和封装性,如包、类、继承和单独编译和发布单元,这些特性使得使用JavaFX技术创建和管理大型程序变为可能。
可用性:
- JavaFX API与Java SE运行时环境(JRE)和Java开发工具包(JDK)捆绑在一起。
- JDK可用于所有主要的桌面平台,Windows,Mac OS X和Linux。 JavaFX应用程序可以在所有主要的桌面平台上编译和运行。
- JDK for ARM平台包括JavaFX的基础,图形和控件组件。