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
不可調整大小,因此它只在該區域居中。
修改圓的centerx
和centery
座標在這裡不會有幫助:圓的佈局邊界將是一個大約20x20 像素的矩形(因為半徑為10,所以這是包含圓的最小矩形;“大約”在這裡是因為筆劃可能需要一些額外的空間)。此矩形將具有從中心半徑開始並延伸到中心 半徑的座標系,但隨後它將根據邊框窗格的佈局策略在中心區域居中。實際上,圓心的座標發生了變化,但這些座標僅在圓本身的座標系內,而不是在邊框窗格的座標系內。
一種解決方案是將圓圈包裹在不執行佈局的常規 pane
中,並將 pane
放置在 borderpane
的中心。 pane
的大小是可調整的,因此 borderpane
會將其大小調整為中心區域的完整大小。 pane
不會對圓進行佈局,因此它保留在 centerx
和 centery
定義的座標上,而無需任何其他佈局。 (實際上,您使圓的座標系與窗格的座標系相同。)這是我在下面的程式碼中使用的解決方案。
另一個解是操作圓的 translatex
和 translatey
屬性。這些變換在佈局之後應用。 然而,使用此解決方案,防止圓脫離其容器的邊界變得更加複雜。 (我沒有在下面的程式碼中展示這個解決方案。)
請參閱佈局文件 以了解更多詳情。
這裡是使此工作正常進行的修改。請注意,我還修改了邊界的計算方式,因此即使調整視窗大小,它仍然有效。
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中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Atom編輯器mac版下載
最受歡迎的的開源編輯器