![](/img/trans.png)
[英]How can I show an image using the ImageView component in javafx and fxml?
[英](JavaFX) How can I change a picture in an ImageView thats coded in an fxml file with a basic java class?
我當前正在編寫一個程序,我想單擊一個按鈕,然后它運行另一個類,並且根據該類的輸出,我希望與單擊的按鈕在同一場景上的圖像視圖被更改。 我已經將所有代碼編碼到輸出更改圖像的程度。 我該怎么做呢?
Java類
public static void USPSCase() {
printUSPS();
}
public static void printUSPS(){
Random rand = new Random();
int gunSelect = rand.nextInt(99)+1;
if(gunSelect<=30){ //Calculates which gun you will get
LeadConduitUSPS(CaseSpinners.USPSCaseSpinController.setUSPImage);
}
else if(gunSelect>=31 && gunSelect<=60){
NightOpsUSPS();
}
else if(gunSelect>=61 && gunSelect<=90){
TorqueUSPS();
}
else if(gunSelect>=91 && gunSelect<=93.5){
GuardianUSPS();
}
else if(gunSelect>=94.5 && gunSelect<=97){
CyrexUSPS();
}
else if(gunSelect>=98 && gunSelect<=99){
CaimanUSPS();
}
else if(gunSelect==100){
KillConfirmedUSPS();
}
}
//some code... these two methods are connected
public static void LeadConduitUSPS(ImageView setUSPImage){
System.out.println("Lead Conduit");
Random rand = new Random();
int wareSelect = rand.nextInt(99)+1;
if(wareSelect<=10){ //Calculates the ware the gun has
setUSPImage.setImage(new Image("csgocaseopener/icon.png"));
}
else if(wareSelect>=11 && wareSelect<=25){
setUSPImage.setImage(new Image("csgocaseopener/icon.png"));
}
else if(wareSelect>=26 && wareSelect<=60){
setUSPImage.setImage(new Image("csgocaseopener/icon.png"));
}
else if(wareSelect>=61 && wareSelect<=80){
setUSPImage.setImage(new Image("csgocaseopener/icon.png"));
}
else if(wareSelect>=81){
setUSPImage.setImage(new Image("csgocaseopener/icon.png"));
}
}
fxml文件
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="CaseSpinners.USPSCaseSpinController">
<children>
<ImageView fitHeight="400.0" fitWidth="600.0" pickOnBounds="true">
<image>
<Image url="@../csgocaseopener/back.png" />
</image>
</ImageView>
<ImageView fx:id="spinmechback" fitHeight="45.0" fitWidth="45.0" onMouseClicked="#handleSpinMechBack" pickOnBounds="true" preserveRatio="true" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="-1.0">
<image>
<Image url="@../csgocaseopener/backbtn.png" />
</image>
</ImageView>
<Button fx:id="SpinUSPS" layoutX="235.0" layoutY="301.0" mnemonicParsing="false" onAction="#SpinUSPSCase" text="SPIN">
<font>
<Font name="System Bold" size="36.0" />
</font>
</Button>
<ImageView fx:id="setAWPImage" fitHeight="200.0" fitWidth="200.0" layoutX="201.0" layoutY="100.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../csgocaseopener/bprof.png" />
</image></ImageView>
</children>
</AnchorPane>
fxml控制器
public class USPSCaseSpinController implements Initializable {
@FXML
public static ImageView setUSPImage;
@FXML
private void handleSpinMechBack(MouseEvent event) throws IOException{
Parent handleInventoryBackParent = FXMLLoader.load(getClass().getResource("/csgocaseopener/OpenCase.fxml"));
Scene OPBackScene = new Scene(handleInventoryBackParent);
Stage handleInventoryBackStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
handleInventoryBackStage.setScene(OPBackScene);
handleInventoryBackStage.show();
}
@FXML
private void SpinUSPSCase(ActionEvent event) throws IOException{
test test = new test();
test.LeadConduitUSPS(setUSPImage);
}
@FXML
public void SetUSPImage(){
setUSPImage.setImage(new Image("AWPCase.png"));
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
首先在支持者類中為LeadConduitUSPS()
創建參數化方法。 像這樣,
public static void LeadConduitUSPS(ImageView image){
System.out.println("Lead Conduit");
Random rand = new Random();
int wareSelect = ((int)(rand.nextDouble() * 99)) +1;
if(wareSelect<=10){ //Calculates the ware the gun has
System.out.println("Factory New");
}
else if(wareSelect>=11 && wareSelect<=25){
//Changes Image here!!
image.setImage(new Image("new_image1.png");
}
else if(wareSelect>=26 && wareSelect<=60){
//Changes Image here!!
image.setImage(new Image("new_image2.png");
}
else if(wareSelect>=61 && wareSelect<=80){
//Changes Image here!!
image.setImage(new Image("new_image3.png");
}
else if(wareSelect>=81){
//Changes Image here!!
image.setImage(new Image("new_image4.png");
}
}
然后從Controller類的SpinUSPSCase()
調用LeadConduitUSPS()
方法,
public class USPSCaseSpinController implements Initializable {
@FXML
private ImageView setUSPImage;
@FXML
private void handleSpinMechBack(MouseEvent event) throws IOException{
Parent handleInventoryBackParent = FXMLLoader.load(getClass().getResource("/csgocaseopener/OpenCase.fxml"));
Scene OPBackScene = new Scene(handleInventoryBackParent);
Stage handleInventoryBackStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
handleInventoryBackStage.setScene(OPBackScene);
handleInventoryBackStage.show();
}
@FXML
private void SpinUSPSCase(ActionEvent event) throws IOException{
//call from here
YourAnotherClass test = new YourAnotherClass();
test.LeadConduitUSPS(USPImage);
}
@FXML
public void SetUSPImage(){
setUSPImage.setImage(new Image("AWPCase.png"));
}
@Override
public void initialize(URL location, ResourceBundle resources) {
}
}
您可以將FXML提供的ImageView實例(USPImage)傳遞給LeadConduitUSPS(ImageView imgView)
方法。 然后,您可以在相關類imgView
Image設置為此imgView
。 希望它是有用的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.