Program bertukar Scene
package javafxswitchscene;
import javafx.application.Application;
import javafx.event.ActionEvent;
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 Adianto
*/
public class JavaFXSwitchScene extends Application {
ButtonHandler bh;
Group root1;
Group root2;
//Untuk scene1
Scene scene1;
Button btnToScene2;
//Untuk scene2
Scene scene2;
Button btnToScene1;
//Untuk Stage
Stage stage;
//Inner class untuk handler
class ButtonHandler
implements EventHandler <ActionEvent>
{
@Override public void handle(ActionEvent e)
{
System.out.println("ASDF");
if(e.getSource().equals(btnToScene2)){
stage.setScene(scene2);
}
if(e.getSource().equals(btnToScene1)){
stage.setScene(scene1);
}
}
}
@Override
public void start(Stage primaryStage) {
root1 = new Group();
root2 = new Group();
stage=primaryStage;
bh = new ButtonHandler();
btnToScene2 = new Button("Switch To Scene 2");
btnToScene2.setOnAction(bh);
root1.getChildren().add(btnToScene2);
scene1 = new Scene(root1, 250, 250, Color.BISQUE);
btnToScene1 = new Button("Switch To Scene 1");
btnToScene1.setOnAction(bh);
root2.getChildren().add(btnToScene1);
scene2 = new Scene(root2, 250, 250, Color.BLACK);
stage.setTitle("Switcher");
stage.setScene(scene1);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
import javafx.application.Application;
import javafx.event.ActionEvent;
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 Adianto
*/
public class JavaFXSwitchScene extends Application {
ButtonHandler bh;
Group root1;
Group root2;
//Untuk scene1
Scene scene1;
Button btnToScene2;
//Untuk scene2
Scene scene2;
Button btnToScene1;
//Untuk Stage
Stage stage;
//Inner class untuk handler
class ButtonHandler
implements EventHandler <ActionEvent>
{
@Override public void handle(ActionEvent e)
{
System.out.println("ASDF");
if(e.getSource().equals(btnToScene2)){
stage.setScene(scene2);
}
if(e.getSource().equals(btnToScene1)){
stage.setScene(scene1);
}
}
}
@Override
public void start(Stage primaryStage) {
root1 = new Group();
root2 = new Group();
stage=primaryStage;
bh = new ButtonHandler();
btnToScene2 = new Button("Switch To Scene 2");
btnToScene2.setOnAction(bh);
root1.getChildren().add(btnToScene2);
scene1 = new Scene(root1, 250, 250, Color.BISQUE);
btnToScene1 = new Button("Switch To Scene 1");
btnToScene1.setOnAction(bh);
root2.getChildren().add(btnToScene1);
scene2 = new Scene(root2, 250, 250, Color.BLACK);
stage.setTitle("Switcher");
stage.setScene(scene1);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
No comments:
Post a Comment