JavaFX是Java的下一代图形用户界面工具包。JavaFX是一组图形和媒体API,我们可以用它们来创建和部署富客户端应用程序。
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"));
public class eventcc {
@FXML
public void onevent(Event event){
EventType extends Event> eventType = event.getEventType();
System.out.println(eventType);
}
}
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
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
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() {
@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() {
@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() {
@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);
}
}
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
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 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() {
@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() {
@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() {
@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 =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的基础,图形和控件组件。