JAVA:按下按鈕時在邊框窗格中移動對象
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脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

利用Golang開發功能強大的桌面應用隨著網路的不斷發展,人們已經離不開各種類型的桌面應用程式。而對於開發人員來說,如何利用高效率的程式語言來開發功能強大的桌面應用至關重要。本文將介紹如何利用Golang(Go語言)來開發功能強大的桌面應用,並提供一些具體的程式碼範例。 Golang是一種由Google開發的開源程式語言,它具有簡潔、高效、並發性強等特點,非常適

layui 登入頁面跳轉設定步驟:新增跳轉代碼:在登入表單提交按鈕點選事件中新增判斷,成功登入後透過 window.location.href 跳到指定頁面。修改 form 配置:在 lay-filter="login" 的 form 元素中新增 hidden 輸入字段,name 為 "redirect",value 為目標頁面位址。

如何為 Vue 中的圖片新增點擊事件?導入 Vue 實例。建立 Vue 實例。在 HTML 模板中新增圖片。使用 v-on:click 指令新增點擊事件。在 Vue 實例中定義 handleClick 方法。

鴻蒙HarmonyOS與Go語言開發簡介鴻蒙HarmonyOS是華為開發的分散式作業系統,而Go是一種現代化的程式語言,兩者的結合為開發分散式應用提供了強大的解決方案。本文將介紹如何在HarmonyOS中使用Go語言進行開發,並透過實戰案例加深理解。安裝與設定要使用Go語言開發HarmonyOS應用,你需要先安裝GoSDK和HarmonyOSSDK。具體步驟如下:#安裝GoSDKgogetgithub.com/golang/go#設定PATH

PHP技巧:快速實現回到上一頁功能在網頁開發中,常常會遇到需要實作返回上一頁的功能。這樣的操作可以提高使用者體驗,讓使用者更方便地在網頁之間進行導航。在PHP中,我們可以透過一些簡單的程式碼來實現這項功能。本文將介紹如何快速實現返回上一頁功能,並提供具體的PHP程式碼範例。在PHP中,我們可以使用$_SERVER['HTTP_REFERER']來取得上一頁的URL

並發程式設計中的事件驅動機制透過在事件發生時執行回呼函數來回應外部事件。在C++中,事件驅動機制可用函數指標實作:函數指標可以註冊回呼函數,在事件發生時執行。 lambda表達式也可以實現事件回調,允許建立匿名函數物件。實戰案例使用函數指標實作GUI按鈕點擊事件,在事件發生時呼叫回呼函數並列印訊息。

答:JavaScript提供了多種取得網頁元素的方法,包括使用id、標籤名、類別名稱和CSS選擇器。詳細描述:getElementById(id):根據唯一id取得元素。 getElementsByTagName(tag):取得具有指定標籤名的元素組。 getElementsByClassName(class):取得具有指定類別名稱的元素組。 querySelector(selector):使用CSS選擇器取得第一個符合元素。 querySelectorAll(selector):使用CSS選擇器取得所有匹配

Tkinter是python標準庫中一個功能強大的GUI工具包,用於創建跨平台的圖形使用者介面(GUI)。它基於Tcl/Tk工具包,提供簡單直覺的語法,使Python開發人員能夠輕鬆快速地創建複雜的使用者介面。 Tkinter的優勢跨平台相容性:Tkinter應用程式可在windows、Mac和linux等所有主要作業系統上運行。簡單易用:其語法清晰且易於學習,使初學者和經驗豐富的開發人員都能輕鬆掌握。可擴展性:Tkinter提供了各種小部件和控件,使開發人員能夠創建各種各樣的使用者介面。集成性:它與P