私は3つのランダムな象形文字画像を画面に表示するプログラムを持っています。私は象形文字のイメージをリフレッシュするために "リフレッシュ"ボタンを追加しました。ボタンをクリックすると、画像がリフレッシュされ、適切にランダム化されます。ボタンが最初にクリックされた後、それは消えます。私はそれが私のpane.getChildren().clear();
行と何か関係があることはほとんど確信していますが、私はそれを把握しているようです。任意のヒントやアドバイス?クリック1回後にJavaFXリフレッシュボタンが消える
私が間違って投稿した場合、または適切なガイドラインを使用しないと、私はお詫び申し上げます。これは私の最初の投稿の一つです。
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.control.Button;
import javafx.geometry.Pos;
import javafx.scene.layout.HBox;
public class Lab6a extends Application {
@Override
public void start(Stage myStage) {
//Create an HBox layout.
HBox hBox1 = new HBox();
//Set alignment.
hBox1.setAlignment(Pos.CENTER);
getRandomHieroglyphic(hBox1);
//Create a Refresh button.
Button refresh = new Button("Refresh");
refresh.setOnAction(e -> getRandomHieroglyphic(hBox1));
hBox1.getChildren().add(refresh);
//Set the title for the second window.
myStage.setTitle("Random Hieroglyphics with Refresh");
//Create a scene for the window.
Scene myScene = new Scene(hBox1, 400, 400);
//Place the scene in the second window.
myStage.setScene(myScene);
//Show the stage.
myStage.show();
}
public void getRandomHieroglyphic(HBox pane) {
pane.getChildren().clear();
//Create random generators to get a random image
int randomInt1 = (int) (Math.random() * 9) + 1;
int randomInt2 = (int) (Math.random() * 9) + 1;
int randomInt3 = (int) (Math.random() * 9) + 1;
//Create paths for the images to be called
String path1 = "Image/Hieroglyphics/h" + randomInt1 + ".png";
String path2 = "Image/Hieroglyphics/h" + randomInt2 + ".png";
String path3 = "Image/Hieroglyphics/h" + randomInt3 + ".png";
//Add the images into the pane
pane.getChildren().add(new ImageView (path1));
pane.getChildren().add(new ImageView (path2));
pane.getChildren().add(new ImageView (path3));
}
public static void main(String[] args) {
launch(args);
}
}
そうです。 Hboxには最初に4人の子供がいて、3人の画像とボタンがあります。ボタンを 'getRandomHieroglyphic()'メソッドに追加するべきです。 –
しかし、より良い方法は、ボタンを画像から分離することでした。これは、シーンに 'BorderPane'を追加し、' center'にのみ画像を、 'bottom'にボタンを置いて' HBox'を置くと良いでしょう。 –