如何在javaFM中按下按钮后使按钮默认设置



我需要根据按下的按钮进行图片更改。

按下微笑按钮时,脸需要有一个微笑的弧线,当我按下皱眉按钮时,脸需要有一个相反的弧度来皱眉,当按下思考按钮时,脸需要有一条平线。

我已经设法做到了这一点,但似乎我按下思考按钮它会永久设置 radiusY 并且不允许它恢复到默认值。

例如 - 当我按下时 微笑 - 一切都很好 皱眉 - 一切都很好 思考 - 一切都很好 使用思考按钮后 - 皱眉 - 不会恢复到原来 使用皱眉按钮后 - 微笑 - 不会恢复到原来

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.layout.Background; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.VBox; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Arc; 
import javafx.scene.shape.ArcType; 
import javafx.scene.shape.Circle; 
import javafx.scene.text.Font; 
import javafx.scene.text.Text; 
import javafx.stage.Stage; 
import javafx.geometry.Pos;
public class Changingsmile extends Application {
     @Override     
     public void start(Stage stage)     
     { 
         Circle face = new Circle(125, 125, 80);          
         face.setFill(Color.YELLOW);         
         face.setStroke(Color.RED);  
         Circle rightEye = new Circle(86, 100, 10);         
         rightEye.setFill(Color.YELLOW);         
         rightEye.setStroke(Color.BLUE); 
         Circle leftEye = new Circle(162, 100, 10);         
         leftEye.setFill(Color.YELLOW);         
         leftEye.setStroke(Color.BLUE);   

         Arc mouth = new Arc(125, 150, 45, 35, 0, -180);          
         mouth.setFill(Color.YELLOW);         
         mouth.setStroke(Color.BLUE);         
         mouth.setType(ArcType.OPEN); 
         Text caption = new Text(68, 240, "Changing smile");         
         caption.setFill(Color.BLUE);         
         caption.setFont(Font.font ("Verdana", 15));

         Group group = new Group(face, rightEye, leftEye, mouth,  caption); 

         Button smileButton = new Button("Smile"); 

         Button frownButton = new Button("Frown"); 
         Button thinkButton = new Button("Think"); 
         HBox buttonBox = new HBox(10);         
         buttonBox.setAlignment(Pos.CENTER); 

         buttonBox.getChildren().addAll(smileButton, frownButton, 
         thinkButton);
         VBox root = new VBox(10); 
         root.setBackground(Background.EMPTY);         
         root.setAlignment(Pos.CENTER); 
         //add the button box and the face group to the vertical container         
         root.getChildren().addAll(buttonBox, group);
         // create and configure a new scene         
         Scene scene = new Scene(root, 250, 275, Color.YELLOW);
         // supply the code that is executed when the smile button is pressed  
         smileButton.setOnAction(e -> mouth.setLength(-180));
         // supply the code that is executed when the frown button is pressed   
         frownButton.setOnAction(e -> mouth.setLength(180)); 
         thinkButton.setOnAction(e -> mouth.setRadiusY(0));

         stage.setScene(scene);         
         stage.setTitle("Changing Smile");  

         stage.show();             
    }         
     public static void main(String[] args)     
     {         
         launch(args);     
     }
}

解决方案是简单地在按钮中包含操作一个mouth.setRadiusY(35),这应该可以解决问题,因为它会在按下另一个按钮时重置该值。

要为 JavaFX 按钮设置 onAction,请使用如下所示的内容:

Button smileButton = new Button("Smile"); 
smileButton.setOnAction(new EventHandler<ActionEvent>() {
    @Override public void handle(ActionEvent e) {
        mouth.setRadiusY(35);
        mouth.setLength(-180)
    }
});

或者,正如@fabian所建议的:

将 lambda 表达式中 -> 右侧的单个语句替换为用 {} 括起来的多个语句,例如 smileButton.setOnAction(e -> { mouth.setRadiusY(35); mouth.setLength(-180); });,如果你想继续使用lambda表达式
-fabian

最新更新