我目前所處的情況是,我在螢屏上有一個圓圈,如果我先按住 LMB,然后將游標拖過它,則沒有任何反應。我試過查看我可以使用的不同 EventHandler,但我無法讓它們作業。這是我的代碼。
public class DragTester extends Application
{
public static Group group = new Group();
public static Scene scene = new Scene(group, 500, 500);
@Override
public void start(Stage stage)
{
for(int i = 0; i < 3; i ){
DragBox DB = new DragBox(i * 150 100);
}
stage.setScene(scene);
stage.show();
}
}
public class DragBox
{
private Circle c = new Circle(0, 0, 25);
public DragBox(double x){
DragTester.group.getChildren().add(c);
c.setLayoutX(x);
c.setLayoutY(250);
c.setOnDragDetected(new EventHandler<MouseEvent>(){
@Override public void handle(MouseEvent e){
c.setFill(Color.rgb((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random())));
}
});
}
}
uj5u.com熱心網友回復:
您可以使用MOUSE_DRAG_ENTERED處理程式。此事件僅在“完全按下-拖動-釋放手勢”期間觸發,您需要在DRAG_DETECTED事件上啟動該手勢。有關更多資訊,請參閱Javadoc。
這是您為使用此事件而撰寫的示例。在這個例子中,“完全按下-拖動-釋放手勢”是在場景中啟動的:
import java.util.Random;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class DragTester extends Application {
private Group group = new Group();
private Scene scene = new Scene(group, 500, 500);
private Random rng = new Random();
@Override
public void start(Stage stage) {
for (int i = 0; i < 3; i ) {
DragBox DB = new DragBox(i * 150 100);
}
scene.setOnDragDetected(e -> scene.startFullDrag());
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
class DragBox {
private Circle c = new Circle(0, 0, 25);
public DragBox(double x) {
group.getChildren().add(c);
c.setLayoutX(x);
c.setLayoutY(250);
c.setOnMouseDragEntered(e ->
c.setFill(Color.rgb(rng.nextInt(256), rng.nextInt(256), rng.nextInt(256))));
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/345768.html
上一篇:用字串參考變數
