图像是可单击的,但在javafx中不可见

时间:2019-05-25 14:55:46

标签: java javafx fxml scenebuilder

我需要在运行时在javafx中加载并显示一些图像,我设法将其中之一加载,我创建了将该图像作为参数传递的ImageView,但是该图像没有显示。我发现图片是可点击的(它可以识别鼠标事件),但是我无法显示

控制器

public class fx extends Application {
    @FXML
    // The reference of inputText will be injected by the FXML loader
    private TextField inputUsername;
    @FXML
    private TextField inputUrl;
    @FXML
    private ComboBox inputConnectionType;
    @FXML
    private ComboBox inputColour;
    @FXML
    private Button submit;
    @FXML
    private ImageView imageView;
    @FXML
    private AnchorPane anchor;


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

    @Override
    public void start(Stage stage) throws Exception {
        Image image = new Image("/AD_weapons_IT_0211.png");
        imageView = new ImageView(image);
        Parent root = FXMLLoader.load(getClass().getResource("/fxml/form.fxml"));
        Scene scene = new Scene(root);
        stage.setWidth(415);
        stage.setHeight(200);
        stage.setScene(scene);
        stage.sizeToScene();
        stage.show();
    }

    public void submit()
    {
        System.out.println(inputUsername.getText()+"\n");
        System.out.println(inputColour.getValue().toString()+"\n");
        System.out.println(inputConnectionType.getValue().toString()+"\n");
        System.out.println(inputUrl.getText()+"\n");
    }

    public void press()
    {
        System.out.println("CLICKED");
    }
}

fxml文件

<?xml version="1.0" encoding="UTF-8"?>

<?language JavaScript?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.image.Image?>

<TitledPane animated="false" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" text="untitled" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.172-ea" fx:controller="provaProj.fx">
  <content>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
         <children>
            <ImageView fx:id = "imageView" fitHeight="150.0" fitWidth="200.0" layoutX="199.0" layoutY="112.0" onMousePressed="#press" pickOnBounds="true" preserveRatio="false" />
         </children></AnchorPane>
  </content>
</TitledPane>

1 个答案:

答案 0 :(得分:0)

对于FXML应用程序,您应该具有2个类和1个fxml文件:

主类:

public class TestApplication extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLTest.fxml"));
        stage.setTitle("Application");
        Scene scene = new Scene(root);
        stage.setWidth(415);
        stage.setHeight(200);
        stage.setScene(scene);
        stage.sizeToScene();
        stage.show();
    }

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

您的fxml文件 和控制器类:

public class Controller {
    @FXML
    // The reference of inputText will be injected by the FXML loader
    private TextField inputUsername;
    @FXML
    private TextField inputUrl;
    @FXML
    private ComboBox inputConnectionType;
    @FXML
    private ComboBox inputColour;
    @FXML
    private Button submit;
    @FXML
    private ImageView imageView;
    @FXML
    private AnchorPane anchor;

    public void submit()
    {
        System.out.println(inputUsername.getText()+"\n");
        System.out.println(inputColour.getValue().toString()+"\n");
        System.out.println(inputConnectionType.getValue().toString()+"\n");
        System.out.println(inputUrl.getText()+"\n");
    }
@FXML
public void press()
{
    System.out.println("CLICKED");
    Image img = new Image("yourURL");
    imageView.setImage(img);
}

}

然后您就可以在Controller-Class中更改图像了。就像我在press方法中所做的一样。