試圖儘可能清楚這個程序應該顯示4張隨機選擇的卡,然後在它們下面有一個按鈕,從一個按鈕隨機重置所有卡然而,我對在行動事件中如何去做這件事感到困惑。 (也作爲一個額外的獎勵,我似乎不能讓按鈕顯示在卡片下方,而只是最終將整個屏幕從卡片和按鈕上移開)任何幫助都會感激不盡。如何更改javafx中的圖像與按鈕
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.layout.GridPane;
import javafx.event.ActionEvent;
public class GenerateCards extends Application {
public static void main(String[] args) {
Application.launch(args);
}
public int cardNum(){
int Num = (int) Math.ceil(Math.random()*52);
return Num;
}
public void start(Stage primaryStage) {
primaryStage.setTitle("Random Card");
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setHgap(10);
pane.setVgap(5);
// creates button
Button refresh = new Button("Refresh");
HBox button = new HBox(10);
button.setAlignment(Pos.BOTTOM_CENTER);
button.getChildren().add(refresh);
pane.getChildren().add(refresh);
// creates random card images and adds them to pane
Image img = new Image("card/"+cardNum()+".png");
ImageView imgView = new ImageView(img);
pane.add(imgView, 1, 0);
Image img2= new Image("card/"+cardNum()+".png");
ImageView imgView2 = new ImageView(img2);
pane.add(imgView2, 2, 0);
Image img3= new Image("card/"+cardNum()+".png");
ImageView imgView3 = new ImageView(img3);
pane.add(imgView3, 3, 0);
Image img4= new Image("card/"+cardNum()+".png");
ImageView imgView4 = new ImageView(img4);
pane.add(imgView4,4, 0);
//create scene
Scene scene = new Scene(pane,400,200);
primaryStage.setScene(scene);
primaryStage.show();
refresh.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e){
// where im confused
}
});
}
}
使用VBox佈局按鈕下的按鈕。 VBox vbox = new VBox(cardsPane,refreshButton); –
謝謝你@UlukBiy生病給了我一個鏡頭:) – RyanL
這個問題是以它爲中心的:/ – RyanL