この問題を解決できますか?javafxボタンに追加のアクションを追加
クラスCell
にボタンを作成し、このボタンにいくつかのデフォルトアクションを追加します。クラスGame
で
public class Cell {
private Button button;
public Cell() {
this.button = new Button();
this.button.setOnAction(e -> System.out.println("Default action"));
}
public Button getButton() {
return button;
}
}
私は、このボタンに追加のアクションを追加したいが、私はどちらかのデフォルトのアクションを維持したいと思います。
public class Game {
public Game(Button button) {
// this add new action to button but delete previous one
// I want use both actions
button.setOnAction(e -> System.out.println("additional action"));
}
}
私は新しい動作が前の動作よりも優先されることを知っています。ボタンをクリックすると、additional action
だけが印刷されます。
public class Main extends Application{
public static void main(String[] args) {
launch();
}
@Override
public void start(Stage primaryStage) throws Exception {
Cell cell = new Cell();
Button button = cell.getButton();
Game game = new Game(button);
VBox vBox = new VBox(button);
Scene scene = new Scene(vBox);
primaryStage.setScene(scene);
primaryStage.show();
}
}
ボタンに新しいアクションを追加することは可能ですか?
何を達成しようとしていますか?両方のアクションが必要な場合は、そのアクションを1つのアクションで書くのはなぜですか? –