eclipse使用javaFX写一个HelloWorkld

————————————————————————————————————————————————

操作系统:Ubuntu18.04

EclipseVersion: Oxygen.3a Release (4.7.3a)Build id: 20180405-1200

————————————————————————————————————————————————

Eclipse默认是无法支持javafx的,执行如下操作开启访问javafx

1.右键单击项目->BildPath->Configure Bild Path,选中Library标签,选中Access rules,点击右边的edit。

在弹出的TypeAccessRules窗体上点击add,弹出Edit Access Rule 窗体,Resolution选项选择Accessible,Rule Pattern选项填上“javafx/**”

这样就可以直接在Eclipse中使用javafx了。

写一个helloworld试试:

package start;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class start extends Application{
    public static void main( String[] args)
    {
        System.out.println("hello,world");
        launch(args);
    }
    
    @Override 
    public void start(Stage primaryStage) {  
           primaryStage.setTitle("Hello World!");  
           Button btn = new Button();  
           btn.setText("Say 'Hello World'");  
           btn.setOnAction(new EventHandler<ActionEvent>() {  
               @Override  
               public void handle(ActionEvent event) {  
                    System.out.println("HelloWorld!");  
               }  
           });  
             
           StackPane root = new StackPane();  
           root.getChildren().add(btn);  
           primaryStage.setScene(new Scene(root, 300, 250));  
           primaryStage.show();  
        }  

}

注意,上面有一个Gtk-Message,显示无法载入"canberra-gtk-module"

解决办法如下:

打开终端执行如下命令安装上该模块即可

sudo apt-get install libcanberra-gtk-module

 再次运行,齐活。

原文地址:https://www.cnblogs.com/feipeng8848/p/8970404.html