通过使用另一个类 JavaFX 更改矩形的属性。

huangapple go评论97阅读模式
英文:

Change the property of a rectangle by using another class JavaFX

问题

我正在研究一个项目,我想在按下按钮时出现一个矩形。然而,我想通过将按钮点击导向不同的类来实现这一点。以下是我尝试过的内容:

这是我的第一个类,"Main"

  1. static boolean btnClicked = false;
  2. @Override
  3. public void start(Stage primaryStage) {
  4. Button btn = new Button("Make Popup Visible");
  5. Rectangle menu = new Rectangle(40, 40, 200, 200);
  6. menu.setFill(Color.BLACK);
  7. menu.setOpacity(0);
  8. btn.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent e) -> {
  9. AddRect.showMenu();
  10. });
  11. if (btnClicked == true) {
  12. menu.setOpacity(1);
  13. }
  14. Group root = new Group();
  15. root.getChildren().addAll(btn, menu);
  16. Scene scene = new Scene(root, 400, 400);
  17. scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
  18. primaryStage.setScene(scene);
  19. primaryStage.show();
  20. }
  21. public static void main(String[] args) {
  22. launch(args);
  23. }

和我的第二个类,"AddRect"

  1. public class AddRect {
  2. static void showMenu() {
  3. Main.btnClicked = true;
  4. }
  5. }

然而,这不起作用,我不知道为什么。有人能帮助我吗?我甚至不知道这是否是最好的方法(使用两个类),但如果有更好的方法,请告诉我。先谢谢!

[注意:以上是代码的翻译,不包括问题和其他内容。]

英文:

I'm working on a project and I want a rectangle to appear when I press a button. However, I want to do this by directing the button click to a different class. Here is what I've tried:

Here is my first class, "Main"

  1. static boolean btnClicked = false;
  2. @Override
  3. public void start(Stage primaryStage) {
  4. Button btn = new Button("Make Popup Visible");
  5. Rectangle menu = new Rectangle(40,40,200,200);
  6. menu.setFill(Color.BLACK);
  7. menu.setOpacity(0);
  8. btn.addEventHandler(MouseEvent.MOUSE_CLICKED,(MouseEvent e) ->{
  9. AddRect.showMenu();
  10. });
  11. if(btnClicked == true) {
  12. menu.setOpacity(1);
  13. }
  14. Group root = new Group();
  15. root.getChildren().addAll(btn, menu);
  16. Scene scene = new Scene(root,400,400);
  17. scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
  18. primaryStage.setScene(scene);
  19. primaryStage.show();
  20. }
  21. public static void main(String[] args) {
  22. launch(args);
  23. }

And my second class, "AddRect"

  1. public class AddRect {
  2. static void showMenu() {
  3. Main.btnClicked = true;
  4. }
  5. }

However, this isn't working, and I don't know why. Can somebody help me? I don't even know if this is the best way to do it (Using two classes), but if there is a better way please let me know. Thanks in advance!

答案1

得分: 0

我明白了!我只需要将条件移到 EventHandler 内部

  1. btn.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent e) -> {
  2. AddRect.showMenu();
  3. if (btnClicked == true) {
  4. menu.setOpacity(1);
  5. }
  6. });
英文:

I figured it out! I just had to move the conditional inside the EventHandler

  1. btn.addEventHandler(MouseEvent.MOUSE_CLICKED,(MouseEvent e) ->{
  2. AddRect.showMenu();
  3. if(btnClicked == true) {
  4. menu.setOpacity(1);
  5. }
  6. });

huangapple
  • 本文由 发表于 2020年8月14日 05:01:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63403149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定