Java fx事件处理程序

时间:2018-04-24 16:25:17

标签: javafx eventhandler

为什么我的代码没有运行?单击“添加图像”后如何在界面上显示图像?

import javafx.stage.Stage;
import javafx.scene.*;
import javafx.application.Application;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;

public class GUIProject extends Application{

    /**
     * @param args the command line arguments
     */
    public void start (Stage stage) throws Exception
    {
        Pane bPane = new Pane();

        Button btOK = new Button("OK");
        OKAction OKact = new OKAction();
        btOK.setOnAction(OKact);

        Button addImage = new Button("Add Image");
        isAdd addimage = new isAdd();
        addImage.setOnAction(addimage);

        //bPane.getChildren().add(btOK);
        bPane.getChildren().add(addImage);

        Scene scene = new Scene(bPane);

        stage.setScene(scene);

        stage.show();

    }

    public static void main(String[] args) {
        // TODO code application logic here
        Application.launch(args);
    }

}
import java.io.FileInputStream;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;

public class Pane extends StackPane {

   public void addImage() throws Exception
    {
        Image logo = new Image(new FileInputStream("logo.jpg"));
            ImageView logoView = new ImageView(logo);
            this.getChildren().add(logoView);
    }

}
import java.io.FileInputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;


class isAdd implements EventHandler<ActionEvent>{

    public void handle(ActionEvent event)
    {
        try {
            Pane.displayImage();
        }

        catch (Exception ex) {
            System.out.println("Error");;
        }

    }

}

它一直显示静态方法不能用非静态方法,我该如何解决? 有没有网站可以学习javafx事件处理程序?

0 个答案:

没有答案