【发布时间】:2017-02-10 17:50:00
【问题描述】:
因此,在某些情况下,我要做的就是在单击不同按钮后出现两个按钮,然后这两个按钮在单击时会执行某些操作。有什么想法吗?
控制器类:
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{
Random rand = new Random();
int gunSelect = rand.nextInt(99)+1;
test test = new test();
if(gunSelect<=30)
LeadConduitUSPS(setUSPImage);
else if(gunSelect>=31 && gunSelect<=60)
NightOpsUSPS(setUSPImage);
else if(gunSelect>=61 && gunSelect<=90)
TorqueUSPS(setUSPImage);
else if(gunSelect>=91 && gunSelect<=93.5)
GuardianUSPS(setUSPImage);
else if(gunSelect>=94.5 && gunSelect<=97)
CyrexUSPS(setUSPImage);
else if(gunSelect>=98 && gunSelect<=99)
CaimanUSPS(setUSPImage);
else if(gunSelect==100)
KillConfirmedUSPS(setUSPImage);
}
@FXML
public void SetUSPImage(){
setUSPImage.setImage(new Image("AWPCase.png"));
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
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="setUSPImage" 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>
我只希望已经创建的按钮执行上述说明!
【问题讨论】:
-
您有什么代码或想法要提供吗?请尝试提供minimal reproducible example。
-
我添加了fxml文件和控制器类,如果您需要更多代码请询问,感谢您的帮助!
-
如果我理解正确,您希望您的初始
Button显示另外两个Buttons,它们也有自己的动作,但是这些动作(两个按钮的)是预先建立的?换句话说,您已经知道这些按钮会做什么,或者您想在运行时设置它们的操作? -
他们会有预定的动作
-
基本上,您所做的就是删除单击的按钮并将两个新按钮添加到您想要的位置。你应该在代码中完成这一切,尽管我认为隐藏和显示也可以。