【问题标题】:(JavaFX)(Scene Builder) How can I make a button appear after a button is clicked?(JavaFX)(场景生成器)如何在单击按钮后显示按钮?
【发布时间】: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,它们也有自己的动作,但是这些动作(两个按钮的)是预先建立的?换句话说,您已经知道这些按钮会做什么,或者您想在运行时设置它们的操作?
  • 他们会有预定的动作
  • 基本上,您所做的就是删除单击的按钮并将两个新按钮添加到您想要的位置。你应该在代码中完成这一切,尽管我认为隐藏和显示也可以。

标签: java button javafx fxml


【解决方案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
}

【讨论】:

  • 是的,我在发布问题后实际上解决了这个问题,这正是我做的!我会确保选择这个作为答案。
猜你喜欢
  • 1970-01-01
  • 2017-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多