单击JavaFX形状

时间:2016-05-21 17:28:52

标签: java javafx javafx-2

所以我目前正在创建此API。这个登录类应该只创建一个包含所有需要的框的场景来制作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;
}

}

1 个答案:

答案 0 :(得分:1)

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

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

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

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

private static Pane pane = new Pane();

然后例如在cornerBox中:

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

这是针对此案例的解决方案,但是

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

你需要什么,一个扩展任何Parent(例如Group)的类,它用你想要的每个控件填充Parent,并返回Parent。那么你可以在任何你想要的地方:

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

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