首页  >  文章  >  JAVA:按下按钮时在边框窗格中移动对象

JAVA:按下按钮时在边框窗格中移动对象

PHPz
PHPz转载
2024-02-10 13:40:08419浏览

php小编新一今天为大家介绍一种有趣的JAVA编程技巧:按下按钮时在边框窗格中移动对象。这种技巧可以为用户界面增加一些交互性,让用户能够通过点击按钮来移动对象。这种功能的实现方法相对简单,只需要通过监听按钮的点击事件,并在事件处理方法中更新对象的位置即可。通过这种方式,我们可以为用户提供更加生动、有趣的界面体验。下面我们就来详细介绍一下这种技巧的实现过程。

问题内容

我正在做一项家庭作业,我需要在窗格中创建一个圆圈并使用屏幕底部的按钮移动它。我能够让圆圈和按钮出现在窗格中,但是当我按下按钮时,圆圈不会移动。

我的主要方法如下:

import javafx.application.application;
import javafx.event.actionevent;
import javafx.event.eventhandler;
import javafx.geometry.insets;
import javafx.geometry.pos;
import javafx.scene.scene;
import javafx.scene.control.button;
import javafx.scene.layout.borderpane;
import javafx.scene.layout.hbox;
import javafx.scene.layout.pane;
import javafx.scene.paint.color;
import javafx.scene.shape.circle;
import javafx.stage.stage;


public class moveball extends application {  
  @override
  public void start(stage primarystage) {
    circle ball = new circle(10);
    button btup = new button("up");
    button btdown = new button("down");
    button btleft = new button("left");
    button btright = new button("right");
    
    
    hbox pane = new hbox();
    borderpane bpane = new borderpane();
    
    
    
    ball.setfill(color.red);
    ball.setstroke(color.black);
    pane.setspacing(10);
    pane.setalignment(pos.center);
    pane.getchildren().addall(btup, btdown, btleft, btright);
    bpane.setcenter(ball);
    bpane.setbottom(pane);
    
    btup.setonaction((actionevent e) -> ballcontrol.moveup(ball));
    btdown.setonaction((actionevent e) -> ballcontrol.movedown(ball));
    btleft.setonaction((actionevent e) -> ballcontrol.moveleft(ball));
    btright.setonaction((actionevent e) -> ballcontrol.moveright(ball));
    
    
  
  scene scene = new scene(bpane, 400, 400);
  primarystage.setscene(scene);
  primarystage.settitle("move the ball");
  primarystage.show();
  }
  
  public static void main (string[] args)  
  {  
      launch(args);  
  }  
  
  
}

实际移动圆的方法在这里:

class BallControl{
  public static void moveUp(Circle circle){
    if(circle.getCenterY() - circle.getRadius() - 10 < 0) return;
      circle.setCenterY(circle.getCenterY() - 10);
  }
      
  public static void moveDown(Circle circle){
    if(circle.getCenterY() + circle.getRadius() + 10 > 400) return;
    circle.setCenterY(circle.getCenterY() + 10);
  }
          
  public static void moveLeft(Circle circle){
    if(circle.getCenterX() - circle.getRadius() - 10 < 0) return;
    circle.setCenterX(circle.getCenterX() - 10);
  }
              
    public static void moveRight(Circle circle){
      if(circle.getCenterX() + circle.getRadius() + 10 > 400) return;
      circle.setCenterX(circle.getCenterX() + 10);
    }
}

ballcontrol 方法的目的是检查移动圆是否会将其延伸到窗口边界之外,如果不会,则移动它。但按下按钮时,圆圈永远不会移动。

解决方法

borderpane 是一种“布局窗格”,这意味着它将根据自己的算法布局其子节点。特别是,如果该节点可调整大小,并且在其最小、最大和首选大小指定的约束范围内,则 borderpane 将扩展 center 区域中的节点以填充整个区域,然后将其在该区域内居中。 circle 不可调整大小,因此它仅在该区域居中。

修改圆的 centerxcentery 坐标在这里不会有帮助:圆的布局边界将是一个大约 20x20 像素的矩形(因为半径为 10,所以这是包含圆的最小矩形;“大约”在这里是因为笔划可能需要一些额外的空间)。该矩形将具有一个从中心半径开始并延伸到中心+半径的坐标系,但随后它将根据边框窗格的布局策略在中心区域居中。实际上,圆心的坐标发生了变化,但这些坐标仅在圆本身的坐标系内,而不在边框窗格的坐标系内。

一种解决方案是将圆圈包裹在不执行布局的常规 pane 中,并将 pane 放置在 borderpane 的中心。 pane 的大小是可调整的,因此 borderpane 会将其大小调整为中心区域的完整大小。 pane 不会对圆进行布局,因此它保留在 pane 中,并将 pane 放置在 borderpane 的中心。 pane 的大小是可调整的,因此 borderpane 会将其大小调整为中心区域的完整大小。 pane 不会对圆进行布局,因此它保留在 centerxcentery 定义的坐标上,而无需任何其他布局。 (实际上,您使圆的坐标系与窗格的坐标系相同。)这是我在下面的代码中使用的解决方案。

另一个解决方案是操作圆的 translatextranslatey 属性。这些变换在布局之后应用。然而,使用此解决方案,防止圆脱离其容器的边界变得更加复杂。 (我没有在下面的代码中展示这个解决方案。)

请参阅布局文档 了解更多详情。

这里是使此工作正常进行的修改。请注意,我还修改了边界的计算方式,因此即使调整窗口大小,它仍然有效。

package org.jamesd.examples.movingball;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;


public class MoveBall extends Application {
    @Override
    public void start(Stage primaryStage) {
        Circle ball = new Circle(200, 200, 10);
        Button btUp = new Button("Up");
        Button btDown = new Button("Down");
        Button btLeft = new Button("Left");
        Button btRight = new Button("Right");


        HBox controls = new HBox();
        BorderPane bPane = new BorderPane();



        ball.setFill(Color.RED);
        ball.setStroke(Color.BLACK);
        controls.setSpacing(10);
        controls.setAlignment(Pos.CENTER);
        controls.getChildren().addAll(btUp, btDown, btLeft, btRight);

        Pane ballPane = new Pane(ball);
        bPane.setCenter(ballPane);
        bPane.setBottom(controls);

        btUp.setOnAction((ActionEvent e) -> moveUp(ball));
        btDown.setOnAction((ActionEvent e) -> moveDown(ball));
        btLeft.setOnAction((ActionEvent e) -> moveLeft(ball));
        btRight.setOnAction((ActionEvent e) -> moveRight(ball));



        Scene scene = new Scene(bPane, 400, 400);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Move the Ball");
        primaryStage.show();
    }

    public static void main (String[] args)
    {
        launch(args);
    }

    public void moveUp(Circle circle){
        if(circle.getCenterY() - circle.getRadius() - 10 < 0) return;
        circle.setCenterY(circle.getCenterY() - 10);
    }

    public void moveDown(Circle circle){
        if(circle.getCenterY() + circle.getRadius() + 10 > circle.getParent().getBoundsInLocal().getHeight()) return;
        circle.setCenterY(circle.getCenterY() + 10);
    }

    public void moveLeft(Circle circle){
        System.out.println(circle.getBoundsInLocal());
        if(circle.getCenterX() - circle.getRadius() - 10 < 0) return;
        circle.setCenterX(circle.getCenterX() - 10);
    }

    public void moveRight(Circle circle){
        if(circle.getCenterX() + circle.getRadius() + 10 > circle.getParent().getBoundsInLocal().getWidth()) return;
        circle.setCenterX(circle.getCenterX() + 10);
    }
}

以上是JAVA:按下按钮时在边框窗格中移动对象的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:stackoverflow.com。如有侵权,请联系admin@php.cn删除