如何在JFXPanel中添加图标

时间:2014-02-14 17:18:01

标签: java graphics javafx javafx-2

你好我在JavaFx上有点新,我在oracle网站上获得了一个源代码,我想在JFXPanel中添加一个图标。如何在JFXPanel中插入一个图标。图标应该是位置。这是代码:

public class Test {

    private static void initAndShowGUI() {
        // This method is invoked on the EDT thread
        JFrame frame = new JFrame("Swing and JavaFX");

        final JFXPanel fxPanel = new JFXPanel();
        JScrollPane pane = new JScrollPane(fxPanel,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);            

        frame.add(pane);
        frame.setSize(300, 200);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                initFX(fxPanel);
            }
       });
    }

    private static void initFX(JFXPanel fxPanel) {
        // This method is invoked on the JavaFX thread
        Scene scene = createScene();
        fxPanel.setScene(scene);
    }

    private static Scene createScene() {
        ImageIcon icon=new ImageIcon ( "icon/filter.jpg" );
        // add icon here
        return (scene);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                initAndShowGUI();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:2)

而不是ImageIcon使用javafx.scene.image.Image并将Image添加到ImageViewNode,您可以添加到布局。

Image icon = new Image("stackoverflow5.png", 300, 300, false, false);
StackPane pane = new StackPane();
pane.getChildren().add(new ImageView(icon));
Scene scene = new Scene(pane, 300, 300);

请参阅ImageImageView


来自Image课程的关于文件位置的说明

// The image is located in default package of the classpath
Image image1 = new Image("/flower.png", true);

// The image is located in my.res package of the classpath
Image image2 = new Image("my/res/flower.png", 100, 150, false, false);

// The image is downloaded from the supplied URL through http protocol
Image image3 = new Image("http://sample.com/res/flower.png", 100, 0, false, false);

// The image is located in the current working directory
Image image4 = new Image("file:flower.png", 0, 100, false, false);

这是一个简单的例子

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class StackJavaFx extends Application {

    @Override
    public void start(Stage primaryStage) {

        Image icon = new Image("stackoverflow5.png", 300, 300, false, false);
        StackPane pane = new StackPane();
        pane.getChildren().add(new ImageView(icon));
        Scene scene = new Scene(pane, 300, 300);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

enter image description here