Screen(屏幕)
/**
* 针对屏幕,而不是窗体
*/
public class Screen类使用 extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
Screen primary = Screen.getPrimary();
//获得整个屏幕的高宽
Rectangle2D bounds = primary.getBounds();
//获取可视屏幕的高宽
Rectangle2D visualBounds = primary.getVisualBounds();
// System.out.println(bounds+"..."+visualBounds);
//获取DPI
double dpi = primary.getDpi();
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
public class Screen二 extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
//利用浏览器,打开网页
HostServices hostServices = getHostServices();
hostServices.showDocument("www.baidu.com");
Button btn=new Button("按钮");
btn.setPrefHeight(200);
btn.setPrefWidth(200);
//可以不同区域,设置不同的鼠标样式
btn.setCursor(Cursor.MOVE);
primaryStage.setTitle("JavaFX");
Group group=new Group();
group.getChildren().add(btn);
Scene scene=new Scene(group);
//设置鼠标的样式,此时是变成手
scene.setCursor(Cursor.HAND);
URL url=this.getClass().getClassLoader().getResource("icon/icon.jpg");
//还可以添加路径资源
scene.setCursor(Cursor.cursor(url.toExternalForm()));
primaryStage.setScene(scene);
primaryStage.setHeight(800);
primaryStage.setWidth(800);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Group
public class Group容器的使用 extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Button b1=new Button("b1");
Button b2=new Button("b2");
Button b3=new Button("b3");
b1.setLayoutX(0);
b1.setLayoutY(0);
b1.setPrefWidth(50);//设置宽度
//设置坐标
b2.setLayoutX(30);
b2.setLayoutY(30);
b3.setLayoutX(60);
b3.setLayoutY(60);
Group group=new Group();
group.getChildren().addAll(b1,b2,b3);
//按钮b1添加点击事件
b1.setOnAction(event -> {
group.getChildren().addAll(new Button("b4"));
});
//子元素的数量变化的监听
group.getChildren().addListener((ListChangeListener<Node>) c -> {
System.out.println("子元素数量==="+c.getList().size());
});
// group.setAutoSizeChildren(true); 这地方就是默认值,子元素宽高自适应,可以设置false就全是0了
// group.getChildren().remove(0); 移除元素
// group.getChildren().clear();清空元素
// group.setOpacity(0.5);//会影响所有子组件。其他操作也类似
//获取group中所有子组件的左上角原点,是否包含0,0位置
// System.out.println(group.contains(0,0));
//获取所有子组件的数组
// Object[] objects = group.getChildren().toArray();
Scene scene=new Scene(group);
primaryStage.setScene(scene);
primaryStage.setHeight(300);
primaryStage.setWidth(300);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Button
public class Button相关 extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
Button b1=new Button();
b1.setText("按钮B1");
//设置字体
b1.setFont(new Font("snas-serif",20));
b1.setLayoutX(100);
b1.setLayoutY(100);
b1.setPrefWidth(100);
b1.setPrefHeight(50);
//设置按钮的背景色,圆角等
// BackgroundFill backgroundFill=new BackgroundFill(Paint.valueOf("#8fbc8f"),new CornerRadii(20),new Insets(1));
// Background background=new Background(backgroundFill);
// b1.setBackground(background);
//设置边框样式
// b1.setBorder(new Border(new BorderStroke(Paint.valueOf("#8a2be2"),BorderStrokeStyle.SOLID,new CornerRadii(20),new BorderWidths(2))));
//设置字体颜色
// b1.setTextFill(Paint.valueOf("#cd0000"));
//设置类似css样式
b1.setStyle(" -fx-background-color: aqua;"+"-fx-background-radius: 20");
//设置单击事件
b1.setOnAction(event -> {
//获取事件源
Button btn= (Button) event.getSource();
System.out.println("b1的文本==="+btn.getText());
System.out.println(event.getTarget()+"...."+event.getSource());
});
Group group=new Group();
group.getChildren().add(b1);
Scene scene=new Scene(group);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFX");
primaryStage.setWidth(800);
primaryStage.setHeight(800);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
双击和键盘检测
双击不止针对Buton,此处只是拿Button举例
public class Button双击事件和检测键盘按键 extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Button b1=new Button("按钮B1");
b1.setLayoutX(100);
b1.setLayoutY(100);
b1.setPrefWidth(100);
b1.setPrefHeight(50);
//事件传递从子到父
b1.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
// System.out.println("鼠标单击");
if (event.getClickCount() == 2) {
System.out.println("双击事件");
}
//获取鼠标是左击(PRIMARY)还是右击(SECONDARY),中键(MIDDLE)
System.out.println("鼠标按键=="+event.getButton().name());
if (event.getButton().name().equals(MouseButton.PRIMARY.name())) {
System.out.println("鼠标左键点击");
}
});
//父到子
// b1.addEventFilter();
//键盘按键按下
b1.setOnKeyPressed(event -> {
System.out.println("按下: "+event.getCode().getName());
});
//键盘按键释放
b1.setOnKeyReleased(event -> {
System.out.println("释放: "+event.getCode().getName());
if (event.getCode().getName().equals(KeyCode.A.getName())) {
System.out.println("按下了A");
}
});
Group group=new Group();
group.getChildren().add(b1);
Scene scene=new Scene(group);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFX");
primaryStage.setWidth(800);
primaryStage.setHeight(800);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
快捷键注册
public class 快捷键 extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Button b1=new Button("b1按钮");
Group group=new Group();
group.getChildren().add(b1);
Scene scene=new Scene(group);
//只记录最常用的两种种
KeyCombination keyCombination=new KeyCodeCombination(KeyCode.Y,KeyCombination.ALT_DOWN);
scene.getAccelerators().put(keyCombination, () -> {
//还是UI线程
System.out.println("同时按下Y和ALT ===="+Thread.currentThread().getName());//JavaFX Application Thread
});
//第二种:绑定按钮已有事件
KeyCombination k1=new KeyCodeCombination(KeyCode.X,KeyCombination.ALT_DOWN);
Mnemonic mnemonic=new Mnemonic(b1,k1);
scene.addMnemonic(mnemonic);
b1.setOnAction(event -> System.out.println("执行"));
primaryStage.setScene(scene);
primaryStage.setHeight(200);
primaryStage.setWidth(200);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}