Saturday, September 24, 2016

Scene : Contoh Kedua dengan Merubah Title

Contoh Program Kedua

package javafxsceneswitch1;

import javafx.application.Application;

import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
 *
 * @author Joko
 */
public class JavaFXSceneSwitch1 extends Application {
    ButtonHandler bh;
    Stage stage;
   
    Group group1;
    Scene scene1;
    Button btnToScene2;
   
    Group group2;
    Scene scene2;
    Button btnToScene1;
   
    class ButtonHandler implements EventHandler{

        @Override
        public void handle(Event event) {
            if(event.getSource().equals(btnToScene2)){
                stage.setTitle("I am in scene2");
                stage.setScene(scene2);
            }
            if(event.getSource().equals(btnToScene1)){
                stage.setTitle("I am in scene1");
                stage.setScene(scene1);
            }
        }
       
    }
   
    @Override
    public void start(Stage primaryStage) {
        bh = new ButtonHandler();
        //primaryStage akan disalin rujukannya ke
        //stage
        stage=primaryStage;
       
        //Yang akan ditampilkan pada mulanya adalah
        //scene1
        group1 = new Group();
        btnToScene2 = new Button("Switch to Scene 2");
        btnToScene2.setOnAction(bh);
        group1.getChildren().add(btnToScene2);
        scene1 = new Scene(group1, 300, 250, Color.CADETBLUE);
       
        //Kita juga akan membuat scene2
        group2 = new Group();
        btnToScene1 = new Button("Switch to Scene 1");
        btnToScene1.setOnAction(bh);
        group2.getChildren().add(btnToScene1);
        scene2 = new Scene(group2, 300, 250, Color.MAROON);
       
        primaryStage.setTitle("Scene Switcher");
        primaryStage.setScene(scene1);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
   
}

No comments:

Post a Comment