Изображение кликабельно, но невидимо в javafx - PullRequest
0 голосов
/ 25 мая 2019

Мне нужно загрузить и отобразить некоторые изображения во время выполнения в 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 голосов
/ 25 мая 2019

У вас должно быть 2 класса и 1 файл fxml для приложения 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 ... что-то вроде того, что я сделал в методе печати. ​​

...