【发布时间】:2015-03-28 11:56:26
【问题描述】:
我正在用 javaFX 在 netbeans 中编写程序 视图中有几个按钮和一些坏按钮(比如炸弹是扫雷),我试图在按下坏按钮时冻结程序,但我不知道该怎么做
谢谢!
【问题讨论】:
-
Freeze the program是什么意思?你想freeze the button吗?
我正在用 javaFX 在 netbeans 中编写程序 视图中有几个按钮和一些坏按钮(比如炸弹是扫雷),我试图在按下坏按钮时冻结程序,但我不知道该怎么做
谢谢!
【问题讨论】:
Freeze the program 是什么意思?你想freeze the button吗?
您的问题有多种解决方案。其中 2 个只是简单地忽略操作事件或禁用按钮,如下所示:
public class ButtonAction extends Application {
final BooleanProperty buttonActionProperty = new SimpleBooleanProperty();
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) {
FlowPane root = new FlowPane();
CheckBox checkBox = new CheckBox( "Enabled");
checkBox.setSelected(true);
// solution 1: check if action is allowed and process it or not
buttonActionProperty.bind( checkBox.selectedProperty());
Button button = new Button( "Click Me");
button.setOnAction(e -> {
if( buttonActionProperty.get()) {
System.out.println( "Allowed, processing action");
} else {
System.out.println( "Not allowed, no action");
}
});
// solution 2: remove comments to activate the code
// button.disableProperty().bind(buttonActionProperty.not());
root.getChildren().addAll(checkBox, button);
primaryStage.setScene(new Scene(root, 600, 200));
primaryStage.show();
}
}
【讨论】:
添加一个使用所有类型事件(鼠标、键盘等)的 ROOT 类型的事件过滤器
btnThatHasHiddenMine.setOnAction(( ActionEvent event ) ->
{
System.out.println("Ohh no! You just stepped over the mine!");
getGameboardPane().addEventFilter( EventType.ROOT, Event::consume );
});
仅将过滤器添加到您的 GameboardPane,因为我们不想冻结应用的其他部分。
【讨论】: