Java - JavaFX Nueva ventana al hacer clic en un botón

 
Vista:
Imágen de perfil de Cindy

JavaFX Nueva ventana al hacer clic en un botón

Publicado por Cindy (8 intervenciones) el 08/08/2017 07:33:03
Estoy aprendiendo javafx. Tengo un evento handler sobre escrito quiero hacer clic y que se me abra una nueva ventana. Como lo hago?

1
2
3
4
5
6
7
8
9
10
11
12
13
public void handle(ActionEvent event) {
	//si se presiona el boton nuevo juego @Cindy
	if((Button)event.getSource()==NuevoJuego){
		--AQUI SE ABRE LA VENTANA NUEVO JUEGO
	}//si se presiona el boton historial @Cindy
	else if((Button)event.getSource()==historial){
		--AQUI SE ABRE LA VENTANA CON EL HISTORIAL
	}
	else{//si se presiona el boton salir @Cindy
		//Salir del juego @Cindy
		System.exit(0);
	}
}

-------------EL SIGUIENTE ES TODO MI CODIGO, LO ESCRITO ANTES ES UNA PARTE DEL SIGUEINTE--------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package principal;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javax.xml.ws.handler.Handler;
 
 
/**
 *
 * @author cindybohorquezsantana
 */
public class Principal extends Application implements EventHandler<ActionEvent>{
    Button NuevoJuego;
    Button historial;
    Button salir;
    @Override
    public void start(Stage stage) throws Exception {
 
       //Creating an image  @Cindy
      Image image = new Image(new FileInputStream("/Users/cindybohorquezsantana/NetBeansProjects/RT/src/Imagenes/intro.png") );
 
      //Setting the image view @Cindy
      ImageView imageView = new ImageView(image) ;
 
      //Setting the position of the image @Cindy
      imageView.setX(0) ;
      imageView.setY(0) ;
 
      //setting the fit height and width of the image view @Cindy
      imageView.setFitHeight(450) ;
      imageView.setFitWidth(752) ;
 
      //Setting the preserve ratio of the image view @Cindy
      imageView.setPreserveRatio(true) ;
 
      //Creating a Group object  @Cindy
      StackPane root = new StackPane(imageView) ;
 
      //Creating a scene object @Cindy
      Scene scene = new Scene(root, 550,450) ;
 
      //Setting title to the Stage  @Cindy
      stage.setTitle("Loading an image") ;
 
      //Adding scene to the stage  @Cindy
      stage.setScene(scene) ;
 
 
               //Creacion del boton NuevoJuego @Cindy
               NuevoJuego = new Button("Nuevo Juego");
                NuevoJuego.setDefaultButton(true);
            //Tamaño del NuevoJuego @Cindy
                NuevoJuego.setPrefSize(100, 50);
            //Posicion dle NuevoJuego @Cindy
                NuevoJuego.setLayoutX(100);
                NuevoJuego.setLayoutY(100);
            //Se coloca el boton NuevoJuego en la parte inferior izquierda @Cindy
            StackPane.setAlignment(NuevoJuego, Pos.BOTTOM_LEFT);
 
            //Creacion del boton Historial de Jugadores @Cindy
               historial = new Button("Historial de Jugadores");
                historial.setDefaultButton(true);
            //Tamaño del NuevoJuego @Cindy
                historial.setPrefSize(160, 50);
            //Posicion dle NuevoJuego
                historial.setLayoutX(100);
                historial.setLayoutY(100);
            //Se coloca el boton NuevoJuego en la parte inferior izquierda @Cindy
            StackPane.setAlignment(historial, Pos.BOTTOM_CENTER);
 
 
            //Creacion del boton Salir @Cindy
               salir = new Button("Salir");
                salir.setDefaultButton(true);
            //Tamaño del NuevoJuego @Cindy
               salir.setPrefSize(100, 50);
            //Posicion dle NuevoJuego @Cindy
                salir.setLayoutX(100);
                salir.setLayoutY(100);
            //Se coloca el boton NuevoJuego en la parte inferior izquierda @Cindy
            StackPane.setAlignment(salir, Pos.BOTTOM_RIGHT);
 
            //Se agregan los botones @Cindy
            root.getChildren().addAll(NuevoJuego,historial,salir);
 
            //Displaying the contents of the stage  @Cindy
      stage.show() ;
 
    }
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
 
    @Override
    public void handle(ActionEvent event) {
        //si se presiona el boton nuevo juego @Cindy
        if((Button)event.getSource()==NuevoJuego){
            --AQUI SE ABRE LA VENTANA NUEVO JUEGO
        }//si se presiona el boton historial @Cindy
        else if((Button)event.getSource()==historial){
             --AQUI SE ABRE LA VENTANA HISTORIAL DE JUEGO
        }
        else{//si se presiona el boton salir @Cindy
            //Salir del juego @Cindy
            System.exit(0);
        }
    }
 
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Pedro
Val: 305
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

JavaFX Nueva ventana al hacer clic en un botón

Publicado por Pedro (15 intervenciones) el 10/08/2017 23:03:25
Primero de todo un Stage es una ventana como de windows pero sin alma oséa vacía.
Dentro de un Stage creamos el escenario oséa Scene

Y ya a partir de aquí dentro del handle del evento hay que poner algo como este ejemplo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Hay que manejar posibles errores dentro de un try catch  ¯\_()_/¯  
 
try{
 
//Te lo comento para que  lo comprendamos tú y yo
//Léeme el source del archivo que te digo fxml y te pongo el path
 
FXMLLoader fxmlLoader= new FXMLLoader(getClass().getResource("path_to.fxml"));
Parent root1= (Parent)fxmlLoader.load();
 
//Creame un nuevo Stage o para que nos entendamos una nueva ventana windows vacía
Stage stage= new Stage();
 
//Y ahora dentro del Stage me metes la escena que anteriormente hemos leído y metido en root1
stage.setScene(new Scene(root1));
 
// Y ahora le digo que me muestres el stage
stage.show();
 
}

Espero haber sido lúcido y explícito en mi explicación de principiante
catch (Exception e){e.printStackTrace();}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar