单击时的 JavaFX 形状

JavaFX shape on click

所以我目前正在创建这个 API。此登录 class 应该只创建一个场景,其中包含制作 GUI 所需的所有框。我遇到的问题是单击时我的形状不会执行任何操作。我有事件侦听器,但它不起作用。

    import java.awt.Container;
    import javafx.application.*;
    import javafx.event.EventHandler;
    import javafx.stage.*;
    import javafx.scene.*;
    import javafx.scene.layout.*;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Rectangle;
    import javafx.scene.text.Font;
    import javafx.scene.text.Text;
    import javafx.scene.text.TextAlignment;
    import javafx.scene.control.*;
    import javafx.scene.image.Image;
    import javafx.scene.image.ImageView;
    import javafx.scene.input.MouseEvent;

public class Login extends MainWindow {
private Stage primaryStage;
public static boolean ifContractor=false;
public static boolean ifClient=false;
private static Group root;
private static ImageView iv;

Login(){

}
public static Scene loginScreen(){
    root = new Group();
    fillBackround();
    createShapes();
    System.out.println("I AM ROOT 3 : "+root);

    Scene loginScene = new Scene(root, 1000,750);
    return loginScene;
}
public static void fillBackround() {
    Image loginBackround = new Image("loginBackround.jpg",true);
    iv = new ImageView();
    iv.setImage(loginBackround);
    root.getChildren().add(iv); 

}
public static void createShapes() {
    Group shapes = new Group();
    Rectangle mainBox = mainBox();
    Pane cornerBox = cornerBox();
    Pane clientBox = clientBox();
    Pane contractorBox = contractorBox();

    shapes.getChildren().addAll(mainBox,cornerBox,clientBox,contractorBox);
    root.getChildren().add(shapes);
}
public static Rectangle mainBox() {
    Rectangle mainBox = new Rectangle(350,100,300,500);
    mainBox.setStroke(Color.BLUE);
    mainBox.setFill(Color.DODGERBLUE);
    mainBox.setStrokeWidth(3);
    mainBox.setArcWidth(25);
    mainBox.setArcHeight(25);
    mainBox.setOpacity(0.5);
    return mainBox;

}
public static Pane cornerBox() {
    Rectangle cornerBox = new Rectangle(350,100,250,75);
    cornerBox.setStroke(Color.BLUE);
    cornerBox.setFill(Color.DODGERBLUE);
    cornerBox.setStrokeWidth(3);
    cornerBox.setArcWidth(25);
    cornerBox.setArcHeight(25);
    cornerBox.setOpacity(0.5);
    Text cornerText = new Text(370,150, null);
    cornerText.setFont(new Font(25));
    cornerText.setFill(Color.WHITESMOKE);
    cornerText.setWrappingWidth(200);
    cornerText.setTextAlignment(TextAlignment.JUSTIFY);
    cornerText.setText("Login as: ");
    Pane cornerStack = new Pane();
    cornerStack.getChildren().addAll(cornerBox,cornerText);
    return cornerStack;
}
public static Pane clientBox() {
    Rectangle clientBox = new Rectangle(400,300,200,75);
    clientBox.setStroke(Color.BLUE);
    clientBox.setFill(Color.DODGERBLUE);
    clientBox.setStrokeWidth(3);
    clientBox.setArcWidth(25);
    clientBox.setArcHeight(25);
    clientBox.setOnMousePressed(new EventHandler<MouseEvent>()
    {
        @Override
        public void handle(MouseEvent t) {  
            ifClient = true;   
            System.out.println("Has been clicked");
        }
    });
    Text clientText = new Text(450,350, null);
    clientText.setFont(new Font(25));
    clientText.setFill(Color.WHITESMOKE);
    clientText.setWrappingWidth(200);
    clientText.setTextAlignment(TextAlignment.JUSTIFY);
    clientText.setText("CLIENT");
    Pane clientStack = new Pane();
    clientStack.getChildren().addAll(clientBox,clientText);
    return clientStack;
}
public static Pane contractorBox() {
    Rectangle contractorBox = new Rectangle(400,400,200,75);
    contractorBox.setStroke(Color.BLUE);
    contractorBox.setFill(Color.DODGERBLUE);
    contractorBox.setStrokeWidth(3);
    contractorBox.setArcWidth(25);
    contractorBox.setArcHeight(25);
    Text contractorText = new Text(415,450, null);
    contractorText.setFont(new Font(25));
    contractorText.setFill(Color.WHITESMOKE);
    contractorText.setWrappingWidth(200);
    contractorText.setTextAlignment(TextAlignment.JUSTIFY);
    contractorText.setText("CONTRACTOR");
    Pane contractorStack = new Pane();
    contractorStack.getChildren().addAll(contractorBox,contractorText);
    return contractorStack;
}

}

问题是您的 Panes 相互重叠。当你这样称呼时:

shapes.getChildren().addAll(mainBox,cornerBox,clientBox,contractorBox);

contractorBox(这是一个窗格)将在顶部,消耗所有的点击事件。

您应该在所有方法中使用相同的窗格。为此,您可以使用像这样的成员:

private static Pane pane = new Pane();

然后例如 cornerBox:

pane.getChildren().addAll(cornerBox,cornerText);

这是针对这种情况的解决方案,但是:

整个class看起来很奇怪。为什么要扩展 MainWindow?没有意义。还有为什么一切都是静态的?

你需要什么,class 扩展任何 Parent(例如 Group),它用你想要的每个控件填充 Parent,并且 returns Parent。然后你可以把它放在任何你想要的地方:

Scene scene = new Scene(new Login());

我也不明白你为什么要画矩形和文字。您基本上需要的是两个按钮。您可以使用 CSS 修改按钮的外观:Example1, Example2.