在javaFM中按下按钮后如何设置按钮默认值
我需要根据按下的按钮更改图片 当按下“微笑”按钮时,脸需要有一个微笑弧,当我按下“皱眉”按钮时,脸需要有一个皱眉弧,当按下“思考”按钮时,脸需要有一条平线 我已经设法做到了这一点,但它似乎是一个我按下思考按钮,它设置的半径永久,不允许它恢复到默认值 当我按下按钮时 微笑-一切都好 皱眉——一切都好 想一想——一切都好 使用“思考”按钮后-皱眉-不会返回原始状态 使用“皱眉”按钮-微笑-后不会返回原始状态在javaFM中按下按钮后如何设置按钮默认值,java,javafx,Java,Javafx,我需要根据按下的按钮更改图片 当按下“微笑”按钮时,脸需要有一个微笑弧,当我按下“皱眉”按钮时,脸需要有一个皱眉弧,当按下“思考”按钮时,脸需要有一条平线 我已经设法做到了这一点,但它似乎是一个我按下思考按钮,它设置的半径永久,不允许它恢复到默认值 当我按下按钮时 微笑-一切都好 皱眉——一切都好 想一想——一切都好 使用“思考”按钮后-皱眉-不会返回原始状态 使用“皱眉”按钮-微笑-后不会返回原始状态 import javafx.application.Application; import
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);
}
}
解决方案是只需在操作a
mouth.setRadiusY(35)
的按钮中包含,这应该可以解决问题,因为它会在按下另一个按钮时重置该值
要设置JavaFX按钮的onAction,请使用以下命令:
按钮微笑按钮=新按钮(“微笑”);
setOnAction(新的EventHandler(){
@重写公共无效句柄(ActionEvent e){
口部毛状突起(35);
嘴。设置长度(-180)
}
});
或者,正如@fabian所建议的:
将lambda表达式中->右侧的单个语句替换为由{}包围的多个语句,例如,smileButton.setOnAction(e->{mouth.setRadiusY(35);mouth.setLength(-180);})代码>,如果要继续使用lambda表达式
-费边
如果您希望在按下其他按钮时半径Y不是零,那么这些按钮的onAction代码实际上应该将半径Y设置为零以外的值?谢谢,如何在一个按钮上包含多个指令onAction@taz3345我已经更新了我的答案,解释了如何在setOnAction()中创建EventHandler@taz3345将lambda表达式中->
右侧的单个语句替换为被{}
包围的多个语句,例如smileButton.setOnAction(e->{mouth.setRadiusY(35);mouth.setLength(-180);})代码>,如果要继续使用lambda表达式。