(JavaFX)(Scene Builder) 如何在单击按钮后显示按钮?
(JavaFX)(Scene Builder) How can I make a button appear after a button is clicked?
所以对于某些上下文,我想要做的就是在单击不同的按钮后出现两个按钮,然后这两个按钮在被单击时会执行某些操作。有什么想法吗?
控制器Class:
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>
我只想让已经创建的按钮执行上面解释的操作!
你的问题有点含糊,我会尽量回答的。
有几种方法可以做到这一点。最简单的可能如下所示:
首先,您可以在您的 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
class 中执行此操作,您应该首先在 class:
的顶部定义它们
@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
}
所以对于某些上下文,我想要做的就是在单击不同的按钮后出现两个按钮,然后这两个按钮在被单击时会执行某些操作。有什么想法吗?
控制器Class:
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>
我只想让已经创建的按钮执行上面解释的操作!
你的问题有点含糊,我会尽量回答的。
有几种方法可以做到这一点。最简单的可能如下所示:
首先,您可以在您的 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
class 中执行此操作,您应该首先在 class:
@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
}