我正在制作一个程序,我希望点击一个按钮并运行另一个类,并且根据该类的输出,我希望在与所单击的按钮相同的场景上显示图像视图。我将所有内容编码到输出更改图像的位置。我怎么能这样做呢?
Java Class
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
}
}
答案 0 :(得分:1)
首先在支持者类中为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类的LeadConduitUSPS()
,
SpinUSPSCase()
方法
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) {
}
}
答案 1 :(得分:0)
您可以将FXML提供的ImageView实例(USPImage)传递给您的/<dict.*file/
方法。然后,您可以在相关课程中将图像设置为此LeadConduitUSPS(ImageView imgView)
。希望它有用。