package fx.com; import javafx.application.Application; import javafx.application.ConditionalFeature; import javafx.application.HostServices; import javafx.application.Platform; import javafx.collections.ListChangeListener; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.fxml.JavaFXBuilderFactory; import javafx.geometry.Rectangle2D; import javafx.scene.Cursor; import javafx.scene.Group; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.image.Image; import javafx.stage.Screen; import javafx.stage.Stage; import sun.net.www.content.image.png; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; public class Main extends Application { public static void main(String[] args) { launch(Main.class,args); } @Override public void start(Stage primaryStage) throws IOException, InterruptedException { Button button1 = new Button("b1"); Button button2 = new Button("b2"); Button button3 = new Button("b3"); button1.setLayoutX(0); button1.setLayoutY(0); button2.setLayoutX(50); button2.setLayoutY(0); button3.setLayoutX(100); button3.setLayoutY(0); Group group = new Group(); //添加组件到 group group.getChildren().addAll(button1,button2,button3); //移除一个组件 // group.getChildren().remove(button1); //设置group的透明度 影响 改组里面的所有组件 group.setOpacity(0.5); //检测某坐标有没有组件 表示组件的左上角的位置(50,0) boolean contains = group.contains(50, 0); //获取组里面的所有 组件 Object[] objects = group.getChildren().toArray(); for (int i = 0; i < objects.length; i++) { Button button = (Button)objects[i]; button.setPrefWidth(50); } System.out.println(objects.length); //监听 group 里面的元素 修改的时候 group.getChildren().addListener(new ListChangeListener<Node>() { @Override public void onChanged(Change<? extends Node> c) { int size = c.getList().size(); System.out.println("组件数量:"+size); } }); //给button1 添加一个点击事件 button1.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { Button button = new Button("b4"); button.setLayoutX(150); group.getChildren().add(button); } }); Scene scene = new Scene(group); primaryStage.setScene(scene); URL qunlogoUrl = new URL("http://www.haotuo.net.cn/Resources/cq/qunlogo.png"); primaryStage.setWidth(300); primaryStage.setHeight(300); primaryStage.getIcons().add(new Image(qunlogoUrl.toExternalForm())); primaryStage.setTitle("JavaFX"); primaryStage.show(); } }
JavaFX 创建按钮 添加按钮到组件里面 监听组件内元素发生变化 移除组件内部元素 按钮点击事件
最新推荐文章于 2024-06-04 11:16:23 发布