【问题标题】:JAVAFX: Buttons with actions (open files)JAVAFX:带有操作的按钮(打开文件)
【发布时间】:2016-12-15 18:10:20
【问题描述】:

我是 Javafx 的新手,我希望能够将“操作”添加到我的工具栏按钮,例如“教程”以从项目根目录打开 PDF。

我有点难过,我想知道,有没有一种简单的方法可以在这段代码中添加带有操作的按钮?

@Override
    public void start(Stage primaryStage) throws Exception {
        Button btnNewGame = new Button("New Game");
        Button btnConcede = new Button("Concede");
        Button btnNetwork = new Button("Network");
        Button btnTutorial = new Button("Tutorial");

        ToolBar toolBar = new ToolBar();
        toolBar.getItems().addAll( new Separator(), btnNewGame, btnConcede, btnNetwork, btnTutorial);

        BorderPane pane = new BorderPane();

        pane.setTop(toolBar);
        pane.setCenter(createContent());

        Scene scene = new Scene(pane, 640, 675);

        primaryStage.setTitle("Dam spill - OBJ2000 Eksamen 2016");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

【问题讨论】:

  • 你读过Oracle tutorial吗?还是其他教程?
  • 您只需要EventHandler,当然还需要在其中执行的操作!
  • @James_D 谢谢,我去看看教程!

标签: file button javafx action toolbar


【解决方案1】:

试试这个:

button.setOnAction(event -> {
    System.out.println("button pressed");
} );

【讨论】:

  • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
猜你喜欢
  • 2018-10-12
  • 1970-01-01
  • 2016-01-20
  • 1970-01-01
  • 2017-12-18
  • 1970-01-01
  • 2018-08-27
  • 2010-09-16
  • 2021-11-27
相关资源
最近更新 更多