简体   繁体   English

单击JavaFX形状

[英]JavaFX shape on click

So I am currently creating this API. 因此,我目前正在创建此API。 this login class should just create a scene with all of the needed boxes in it to make a GUI. 该登录类应该只创建一个场景,并在其中创建GUI所需的所有框。 The problem I am having is that my shapes won't do anything when clicked. 我遇到的问题是单击形状时我的形状什么也做不了。 I have the event listener but it won't work. 我有事件监听器,但无法正常工作。

    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;
}

}

The problem is your Panes are overlapping each other. 问题是您的Panes彼此重叠。 When you call this: 当您致电:

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

contractorBox (which is a Pane) will be on the top, consuming all of the click events. contractorBox (它是一个窗格)将位于顶部,使用所有的click事件。

You should use the same pane in all of your methods instead. 您应该在所有方法中使用相同的窗格。 For that you could use a member like: 为此,您可以使用类似以下的成员:

private static Pane pane = new Pane();

then for example in cornerBox : 然后例如在cornerBox

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

This is a solution for this case, BUT: 这是这种情况的解决方案,但:

This whole class looks strange. 整个班级看起来都很奇怪。 Why are you extending MainWindow? 为什么要扩展MainWindow? Makes no sense. 没有意义。 Also why everyting is static? 还有为什么一切都是静态的?

What you would need, a class extending any Parent (eg Group ) , which fills up the Parent with every control you want, and returns the Parent. 您需要的是一个扩展了任何Parent (例如Group )的类,该类将您想要的每个控件填充到Parent中,并返回Parent。 Then you could you it anywhere you want as: 然后,您可以在任何需要的地方使用它:

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

I also don't understand why are you drawing rectangles and texts. 我也不明白为什么要绘制矩形和文本。 What you basically need is two buttons. 您基本上需要两个按钮。 You can modify the look of buttons using CSS: Example1 , Example2 . 您可以使用CSS修改按钮的外观: Example1Example2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM