当我移动圆圈时,文本不会重新定位。
这是我的代码:
Text t = new Text(x,y, "HELLO");
Circle c = new Circle(x,y,radius);
t.xProperty().bind(c.centerXProperty());
t.yProperty().bind(c.centerYProperty());
完整代码:
package netfx;
import java.util.HashMap;
import java.util.Optional;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Cursor;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextInputDialog;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import javafx.scene.text.TextBoundsType;
import javafx.stage.Stage;
class RouterView extends Group {
private String name;
private double x;
private double y;
private double radius;
public void Routerview() {
}
RouterView(String n, double x, double y, double r){
this.name = n;
this.x = x;
this.y = y;
this.radius = r;
}
}
class LinkView {
private String name;
private RouterView src;
private RouterView dst;
public void EdgeView() {
}
public void EdgeView(String name, RouterView s, RouterView d) {
this.name = name;
this.src = s;
this.dst = d;
}
}
public class NetFX extends Application {
Circle circle_Red, circle_Green, circle_Blue;
double orgSceneX, orgSceneY;
double orgTranslateX, orgTranslateY;
HashMap<String, RouterView> routers = new HashMap<String, RouterView>();
HashMap<String, LinkView> links = new HashMap<String, LinkView>();
HBox hBox = new HBox();
Group root = new Group();
public String getRouterName(String msg){
TextInputDialog dialog = new TextInputDialog(" Router Name ");
dialog.setTitle("Router Name");
dialog.setHeaderText("Topology manager");
dialog.setContentText(msg);
Optional<String> result = dialog.showAndWait();
if (result.isPresent()){
return result.get();
}
return null;
}
@Override
public void start(Stage primaryStage) {
hBox.setSpacing(10.0);
Button addBtn = new Button("ADD NODE");
addBtn.setOnAction((event) -> {
String name = getRouterName("Enter Router Name: ");
if (name != null) {
AddRouter(name, 300,300,60);
primaryStage.show();
}
});
Button delBtn = new Button("ADD EDGE");
delBtn.setOnAction((event) -> {
String sname = getRouterName("Enter Source Router: ");
RouterView sp = routers.get(sname);
System.out.println(sname+" = "+sp);
if (sp != null) {
String dname = getRouterName("Enter Destination Router: ");
RouterView dp = routers.get(dname);
System.out.println(dname+" = "+dp);
if (dp != null) {
//EdgeView e = AddEdge(sp,dp);
//edges.put(sname+"-"+dname,e);
primaryStage.show();
}
}
});
Button saveBtn = new Button("SAVE TOPOLOGY");
Button exitBtn = new Button("EXIT");
exitBtn.setOnAction((event) -> { System.exit(0);});
hBox.setPadding(new Insets(0, 20, 10, 20));
hBox.getChildren().addAll(addBtn,delBtn,saveBtn, exitBtn);
root.getChildren().addAll(hBox);
//primaryStage.setResizable(false);
primaryStage.setScene(new Scene(root, 400,350));
primaryStage.setTitle("TOPOLOGY MANAGER");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public void AddRouter(String name, double x, double y, double r){
RouterView router = new RouterView(name,x,y,r);
Circle c = new Circle(r, Color.GREEN);
c.setCenterX(x);
c.setCenterY(y);
Text t = new Text(x-10, y, name);
c.setOnMousePressed(circleOnMousePressedEventHandler);
c.setOnMouseDragged(circleOnMouseDraggedEventHandler);
c.centerXProperty().bind(t.xProperty());
c.centerYProperty().bind(t.yProperty());
root.getChildren().addAll(c,t);
routers.put(name,router);
}
public void AddEdge(){
}
EventHandler<MouseEvent> circleOnMousePressedEventHandler = new
EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
orgSceneX = t.getSceneX();
orgSceneY = t.getSceneY();
orgTranslateX = ((Circle)(t.getSource())).getTranslateX();
orgTranslateY = ((Circle)(t.getSource())).getTranslateY();
System.out.println("orgSceneX = "+orgSceneX);
System.out.println("orgSceneY = "+orgSceneY);
System.out.println("orgTranslateX = "+orgTranslateX);
System.out.println("orgTranslateY = "+orgTranslateY);
}
};
EventHandler<MouseEvent> circleOnMouseDraggedEventHandler = new
EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
double offsetX = t.getSceneX() - orgSceneX;
double offsetY = t.getSceneY() - orgSceneY;
double newTranslateX = orgTranslateX + offsetX;
double newTranslateY = orgTranslateY + offsetY;
System.out.println("offsetX = "+offsetX);
System.out.println("offsetY = "+offsetY);
System.out.println("newTranslateX = "+newTranslateX);
System.out.println("newTranslateY = "+newTranslateY);
((Circle)(t.getSource())).setTranslateX(newTranslateX);
((Circle)(t.getSource())).setTranslateY(newTranslateY);
}
};
}
答案 0 :(得分:0)
文本的位置绑定到圆圈的centerX
和centerY
属性。但是,当您移动圆圈时,不会更改这些属性:您可以通过更改translateX
和translateY
属性来移动它。
更改centerX
和centerY
属性,而不是translateX
和translateY
属性:
EventHandler<MouseEvent> circleOnMousePressedEventHandler = t -> {
orgSceneX = t.getSceneX();
orgSceneY = t.getSceneY();
orgTranslateX = ((Circle)(t.getSource())).getCenterX();
orgTranslateY = ((Circle)(t.getSource())).getCenterY();
System.out.println("orgSceneX = "+orgSceneX);
System.out.println("orgSceneY = "+orgSceneY);
System.out.println("orgTranslateX = "+orgTranslateX);
System.out.println("orgTranslateY = "+orgTranslateY);
};
EventHandler<MouseEvent> circleOnMouseDraggedEventHandler = t -> {
double offsetX = t.getSceneX() - orgSceneX;
double offsetY = t.getSceneY() - orgSceneY;
double newTranslateX = orgTranslateX + offsetX;
double newTranslateY = orgTranslateY + offsetY;
System.out.println("offsetX = "+offsetX);
System.out.println("offsetY = "+offsetY);
System.out.println("newTranslateX = "+newTranslateX);
System.out.println("newTranslateY = "+newTranslateY);
((Circle)(t.getSource())).setCenterX(newTranslateX);
((Circle)(t.getSource())).setCenterY(newTranslateY);
};