如何在JavaFX中按下按钮时显示图像

问题描述 投票:0回答:1

我在弄清楚按下按钮时如何显示图像时遇到麻烦。这是我到目前为止的内容:


公共类标志扩展了应用程序{

@Override
public void start(Stage primaryStage) {
   //Create a label to display a prompt
    Label promptLabel = new Label("Select Button to Display a Flag");

   //Create buttons to display the flags
    Button americanButton = new Button("American Flag");
    Button canadianButton = new Button("Canadian Flag");
    Button frenchButton = new Button("French Flag");
    Button germanButton = new Button("German Flag");
    Button mexicanButton = new Button("Mexican Flag");


    //Creating an HBox
    HBox hbox = new HBox(10, promptLabel);
    HBox hbox1 = new HBox(10, americanButton);
    HBox hbox2 = new HBox(10, canadianButton);
    HBox hbox3 = new HBox(10, frenchButton);
    HBox hbox4 = new HBox(10, germanButton);
    HBox hbox5 = new HBox(10, mexicanButton);

    americanButton.setOnAction(new AmericanButtonHandler());


    //Creating a VBox
    VBox vbox = new VBox(10, hbox, hbox1, hbox2, hbox3, hbox4, hbox5);

    //set vbox padding
    vbox.setPadding(new Insets(10));

    //set vbox alignment
    hbox.setAlignment(Pos.CENTER);
    //gridpane.setAlignment(Pos.CENTER);
    hbox1.setAlignment(Pos.CENTER);
    hbox2.setAlignment(Pos.CENTER);
    hbox3.setAlignment(Pos.CENTER);
    hbox4.setAlignment(Pos.CENTER);
    hbox5.setAlignment(Pos.CENTER);

    //Create a scene
    Scene scene = new Scene(vbox);

    primaryStage.setTitle("Flags");
    primaryStage.setScene(scene);
    primaryStage.show();
}

class AmericanButtonHandler implements EventHandler<ActionEvent>{
    @Override
    public void handle(ActionEvent event){
        Pane aPane = new HBox(10);

        //Create new image
        Image americanImage = new Image("file:America.png");

        //Create new imageView
        ImageView aImageView = new ImageView(americanImage);

        aImageView.setImage(americanImage);
    }
}


/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}


所以我想发生的事情是,当按下americanButton时,将调用事件处理程序以允许显示图像。我已经到了这一点,似乎无法通过。有人帮忙!

image button javafx imageview eventhandler
1个回答
0
投票

我会尝试像底部函数这样的操作(注意:我修改了一些代码以符合自己的喜好,但这不会影响您的端倪,就像我喜欢看它那样)。我看到您不是在创建场景舞台或展示如果您看一下最底层的功能,那么应该使您对应该做的事情有个好主意。我通过的唯一事情是文件路径,因此应该相当容易实现和理解。

public class Main extends Application {


    @Override
    public void start(Stage primaryStage) throws Exception {
        //Create a label to display a prompt
        Label promptLabel = new Label("Select Button to Display a Flag");

        //Create buttons to display the flags
        Button americanButton = new Button("American Flag");
        americanButton.setOnAction(event -> {
            showFlagAction(getClass().getResource("/TestImage.png").toExternalForm());
        });

        Button canadianButton = new Button("Canadian Flag");
        Button frenchButton = new Button("French Flag");
        Button germanButton = new Button("German Flag");
        Button mexicanButton = new Button("Mexican Flag");


        //Creating an HBox
        HBox hbox = new HBox(10, promptLabel);
        hbox.setAlignment(Pos.CENTER);


        HBox hbox1 = new HBox(10, americanButton);
        hbox1.setAlignment(Pos.CENTER);

        HBox hbox2 = new HBox(10, canadianButton);
        hbox2.setAlignment(Pos.CENTER);

        HBox hbox3 = new HBox(10, frenchButton);
        hbox3.setAlignment(Pos.CENTER);

        HBox hbox4 = new HBox(10, germanButton);
        hbox4.setAlignment(Pos.CENTER);

        HBox hbox5 = new HBox(10, mexicanButton);
        hbox5.setAlignment(Pos.CENTER);


        //Creating a VBox
        VBox vbox = new VBox(10, hbox, hbox1, hbox2, hbox3, hbox4, hbox5);
        vbox.setPadding(new Insets(10));

        primaryStage.setTitle("Flags");
        primaryStage.setScene(new Scene(vbox));
        primaryStage.show();
    }

    private void showFlagAction(String filePath){
        ImageView imageView = new ImageView(new Image(filePath));

        VBox vBox = new VBox();
        vBox.setAlignment(Pos.CENTER);
        vBox.getChildren().add(imageView);

        Stage stage = new Stage();
        stage.setScene(new Scene(vBox));
        stage.show();
    }

}
    
© www.soinside.com 2019 - 2024. All rights reserved.