【问题标题】:How to change the image of Imageview on MouseClick如何在 MouseClick 上更改 Imageview 的图像
【发布时间】:2015-05-19 06:17:22
【问题描述】:

我正在使用场景生成器在 javafx 中创建一个应用程序。我有一个图像视图,我想在有人点击它时更改图像。我已经在图像视图中添加了一个鼠标事件,但是当我点击它时它并没有改变图像。请在下面找到相同的代码。

 public class HomeScreenController implements Initializable {
        @FXML
        public ImageView updateproject;
            public String updateprojectstyle="-fx-image:url(\"../../../../Images/update-enable.png\");";
        }
        @FXML
        private void updateProject(MouseEvent event) throws IOException
        {
            System.out.println("hello");
            updateproject.setStyle(updateprojectstyle);
        }
        @Override
        public void initialize(URL location, ResourceBundle resources) {
             //To change body of generated methods, choose Tools | Templates.
        }
    }

更新问题 (MCVE)

我的 FXML 看起来像:

 <VBox prefHeight="658.0" prefWidth="162.0" styleClass="sidepanel" BorderPane.alignment="CENTER">
               <children>
                  <Pane maxHeight="138.0" maxWidth="162.0" prefHeight="200.0" prefWidth="200.0">
                     <children>
                        <ImageView fx:id="addproject" fitHeight="60.0" fitWidth="66.0" layoutX="48.0" layoutY="39.0" pickOnBounds="true" preserveRatio="true" styleClass="addproject-img" />
                     </children>
                  </Pane>
                  <Pane maxHeight="138.0" maxWidth="162.0" prefHeight="200.0" prefWidth="200.0">
                     <children>
                        <ImageView fx:id="updateproject" fitHeight="57.0" fitWidth="84.0" layoutX="41.0" layoutY="32.0" onMouseClicked="#updateProject" pickOnBounds="true" preserveRatio="true" styleClass="updateproject-img" />
                     </children>
                  </Pane>
                  <Pane layoutX="10.0" layoutY="148.0" maxHeight="138.0" maxWidth="162.0" prefHeight="200.0" prefWidth="200.0">
                     <children>
                        <ImageView fx:id="deleteproject" fitHeight="59.0" fitWidth="80.0" layoutX="41.0" layoutY="32.0" pickOnBounds="true" preserveRatio="true" styleClass="deleteproject-img" />
                     </children>
                  </Pane>
               </children>
     </VBox>

我的控制器看起来像:

public class HomeScreenController implements Initializable {
    @FXML
    public ImageView updateproject;
     @FXML
    public ImageView addproject;
      @FXML
    public ImageView deleteproject;
     public String updateprojectstyle="-fx-image:url(\"../../../../Images/update-enable.png\");";
    public String addprojectstyle="-fx-image:url(\"../../../../Images/add-disable.png\");";
 @FXML
    private void updateProject(MouseEvent event) throws IOException
{
    System.out.println("hello");
    updateproject.setStyle(updateprojectstyle);
     addproject.setStyle(addprojectstyle);
}
    @Override
    public void initialize(URL location, ResourceBundle resources) {
         //To change body of generated methods, choose Tools | Templates.
    }

}

我的 CSS 看起来像:

.updateproject-img{
   -fx-image:url("../../../../Images/update-disable1.png");
}
.addproject-img{
     -fx-image:url("../../../../Images/add-active.png");
}
.deleteproject-img{
    -fx-image:url("../../../../Images/delete-disable1.png");
}

当用户单击更新项目图像视图时,我想禁用添加项目图标并启用更新项目图标。 click 事件正在触发,但它没有加载图像。

【问题讨论】:

  • 为什么要用css来设置图片?此外,ImageView 上的图像是通过-fx-image:url(...) 设置的,而不是通过-fx-background-image:url(...) 设置的

标签: javafx imageview mouseevent


【解决方案1】:

这是 ImageView 上带有 css 样式的代码。

ImageView 上的图像是通过-fx-image:url(...) 设置的,而不是通过-fx-background-image:url(...) 设置的,正如您在代码中使用的那样。

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


public class ChangeImageViewImage extends Application {


    private static final String IMAGE1 = "http://img1.wikia.nocookie.net/__cb20130120032553/dragonball/images/3/32/Chibi-Ichigo-bleach-anime-33253004-332-400.png";
    private static final String IMAGE2 = "https://40.media.tumblr.com/68f246eef26984be8a44e4367cfedd47/tumblr_n3lsszuyj41txcp2ko1_500.jpg";

    @Override
    public void start(Stage primaryStage) throws Exception {
        ImageView imageView = new ImageView();
        imageView.setFitHeight(400);
        imageView.setFitWidth(300);
        imageView.setStyle("-fx-image: url(\""+ IMAGE1 + "\");");

        imageView.setOnMouseClicked(event -> {
            imageView.setStyle("-fx-image: url(\""+ IMAGE2 + "\");");
        });

        Pane pane = new Pane();
        pane.getChildren().add(imageView);

        Scene scene = new Scene(pane, 300, 400);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

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

【讨论】:

  • 我已将图像设置为 css 文件中的图像视图。现在单击图像视图时我只想更改图像。实际上这个概念是当我加载页面时图像将是在非活动状态下,当我执行点击操作时,我必须激活图像(更改图像)。正如你所建议的,我只是将 -fx-background-image:url(...) 更改为 -fx-image:url (...)但是现在当我点击它时,图像正在消失..
  • 可能是应用程序无法加载图像。使用 css 的问题是,如果 url 不正确,您不会看到异常,而是会收到警告说 WARNING: Error loading image:。你应该看看我的完整例子:)
  • 我在控制台中没有收到任何警告/错误...我正在使用场景生成器,所以我在控制器中使用 @fxml 注释了我的 imageview id...我的图像路径也是正确的并且甚至将相同的图像复制粘贴到我的控制器并检查,但它没有更改图像...我检查了您的示例并更改了几行代码..但它不起作用:(
  • 那么请创建一个 MCVE 并将其添加到您的问题中。
  • fxml 中的 css 是否适用?我没有发现你加载它们。除此之外,您尝试更改图像的方式对我来说看起来不错。我忽略了一些小问题:P
猜你喜欢
  • 1970-01-01
  • 2011-07-07
  • 2011-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-12
相关资源
最近更新 更多