(JavaFX)(场景构建器)如何在单击按钮后显示按钮?

时间:2017-02-10 17:50:00

标签: java button javafx 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{
            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>

我只想要已经创建的按钮来执行上面解释的操作!

1 个答案:

答案 0 :(得分:1)

你的问题有点模糊,但我会尽力回答。
有几种方法可以做到这一点。最简单的可能如下:

首先,您可以在fxml文件中添加定义按钮,并将它们设置为默认不可见。就像使用SpinUSPS一样,您可以将actionEvent附加到每个新按钮:

<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>
<Button fx:id="invisible1" visible="false" layoutX="235.0" layoutY="320.0" mnemonicParsing="false" onAction="#invisibleMethod1" text="Something1"/>
<Button fx:id="invisible2" visible="false" layoutX="235.0" layoutY="340.0" mnemonicParsing="false" onAction="#invisibleMethod2" text="Something2"/>

当您按下SpinUSPS按钮时,您想要更改按钮的可见性。要在USPSCaseSpinController课程中执行此操作,您应首先在课程顶部定义它们:

@FXML
Button invisible1;
@FXML
Button invisible2;

SpinUSPSCase方法中,您可以放置​​以下内容,以便在点击SpinUSPS时显示它们:

invisible1.setVisible(true);
invisible2.setVisible(true);

要实际让这些新按钮执行某些操作,您可以为这些新按钮编写动作事件:

@FXML
private void invisibleMethod1(ActionEvent event) throws IOException {
    // your code goes here
}

@FXML
private void invisibleMethod2(ActionEvent event) throws IOException {
    // your code goes here
}