gpt4 book ai didi

java - 图像可点击但在javafx中不可见

转载 作者:行者123 更新时间:2023-12-02 05:14:36 24 4
gpt4 key购买 nike

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

Controller

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>

最佳答案

对于 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文件和一个 Controller 类:

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 类中更改图像......就像我在 press 方法中所做的那样。

关于java - 图像可点击但在javafx中不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56305936/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com