In: Computer Science
JavaFX Two-Stage Application
Write a JavaFX multiple stage application which has at least two stages, a primary one and a secondary one.
The primary stage should have a gridpane which has at least a 2*3 grid.
On each cell of the grid, please place a different node from at least one of the three types: a UI control or a shape or an image view, etc.
On the secondary stage, you should design a layout with binding property.
Your overall project design should reflect a theme, for example, a music event commercial, etc.
package com.chegg.music;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
GridPane
gridpane = new GridPane();
Stage secondaryStage = new
Stage();
Button homeBt
= new Button("Home");
homeBt.setOnAction(new EventHandler
@Override
public void
handle(ActionEvent event) {
// TODO
Auto-generated method stub
show("D:\\Chegg\\javafxChegg\\MusicLibrary\\src\\com\\chegg\\music\\home.jpg",secondaryStage);
}
});
Button treeBt =
new Button("Tree");
treeBt.setOnAction(new EventHandler
@Override
public void
handle(ActionEvent event) {
// TODO
Auto-generated method stub
show("D:\\Chegg\\javafxChegg\\MusicLibrary\\src\\com\\chegg\\music\\tree.jpg",secondaryStage);
}
});
Button rabbitBt
= new Button("Rabbit");
rabbitBt.setOnAction(new EventHandler
@Override
public void
handle(ActionEvent event) {
// TODO
Auto-generated method stub
show("D:\\Chegg\\javafxChegg\\MusicLibrary\\src\\com\\chegg\\music\\rabbit.jpg",secondaryStage);
}
});
Button
tortoiseBt = new Button("Tortoise");
tortoiseBt.setOnAction(new EventHandler
@Override
public void
handle(ActionEvent event) {
// TODO
Auto-generated method stub
show("D:\\Chegg\\javafxChegg\\MusicLibrary\\src\\com\\chegg\\music\\tortoise.jpg",secondaryStage);
}
});
Button
mickyMouseBt = new Button("Mouse");
mickyMouseBt.setOnAction(new
EventHandler
@Override
public void
handle(ActionEvent event) {
// TODO
Auto-generated method stub
show("D:\\Chegg\\javafxChegg\\MusicLibrary\\src\\com\\chegg\\music\\mickyMouse.jpg",secondaryStage);
}
});
Button duckBt =
new Button("Duck");
duckBt.setOnAction(new EventHandler
@Override
public void
handle(ActionEvent event) {
// TODO
Auto-generated method stub
show("D:\\Chegg\\javafxChegg\\MusicLibrary\\src\\com\\chegg\\music\\duck.jpg",secondaryStage);
}
});
gridpane.add(homeBt, 0, 0);
gridpane.add(treeBt, 1, 0);
gridpane.add(rabbitBt, 2, 0);
gridpane.add(tortoiseBt, 0, 1);
gridpane.add(mickyMouseBt, 1, 1);
gridpane.add(duckBt, 2, 1);
Scene scene = new
Scene(gridpane, 400, 100);
primaryStage.setTitle("Library
Welcome");
primaryStage.setScene(scene);
primaryStage.show();
//
} catch(Exception e) {
e.printStackTrace();
}
}
/*
* show image as per name in new stage
*/
public void show(String name, Stage
secondaryStage){
FileInputStream input = null;
try {
input = new
FileInputStream(name);
} catch (FileNotFoundException e)
{
// TODO
Auto-generated catch block
e.printStackTrace();
}
Image image = new
Image(input);
ImageView imageView = new
ImageView(image);
HBox hbox = new HBox(imageView);
Scene sceneImages = new Scene(hbox, 300, 300);
secondaryStage.setScene(sceneImages);
secondaryStage.setTitle("Home");
secondaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}