domingo, 22 de abril de 2018

JavaFX - abrindo janelas

Poder abrir novas janelas é fundamental
para programas mais complexo,
Aqui eu mostro um exemplo em JavaFX
de como isto pode ser feito.

Veja abaixo imagens do programa em execução: 




Veja abaixo um vídeo com o funcionamento do programa:

                                                   
Veja abaixo o código do programa:   


import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyCode;
class Janela_3 {
     PROJETO pro = new PROJETO ( );
     BorderPane root = new BorderPane ( );
     Scene scene = new Scene ( root, 500, 300 );
     Canvas canvas = new Canvas ( 500, 300 );
     GraphicsContext ctx = canvas.getGraphicsContext2D ( );
     MenuBar menubar = new MenuBar ( );
     Menu menu = new Menu ( "Arq" );
     MenuItem exit = new MenuItem ( "Sair" );
     MenuItem jan_1 = new MenuItem ( "Janela_1" );
     MenuItem jan_2 = new MenuItem ( "Janela_2" );
     // /////////////////////////////////////////////////////////////////////////
     public void Menu ( Stage stage ) {
         menu.getItems ( ).addAll ( jan_1, jan_2, exit );
         menubar.getMenus ( ).add ( menu );
         exit.setOnAction ( new EventHandler < ActionEvent > ( ) {
              @Override
              public void handle ( ActionEvent e ) {
                   stage.close ( );
              }
         } );
         jan_1.setOnAction ( new EventHandler < ActionEvent > ( ) {
              @Override
              public void handle ( ActionEvent e ) {
                   pro.start ( stage );
              }
         } );
         jan_2.setOnAction ( new EventHandler < ActionEvent > ( ) {
              @Override
              public void handle ( ActionEvent e ) {
                   new Janela_2 ( stage );
              }
         } );
         exit.setAccelerator ( new KeyCodeCombination ( KeyCode.E,
                   KeyCombination.CONTROL_DOWN ) );
         jan_1.setAccelerator ( new KeyCodeCombination ( KeyCode.C,
                   KeyCombination.CONTROL_DOWN ) );
         jan_2.setAccelerator ( new KeyCodeCombination ( KeyCode.D,
                   KeyCombination.CONTROL_DOWN ) );
     }
     // /////////////////////////////////////////////////////////////////////////
     Janela_3 ( Stage stage ) {
         ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 15 ) );
         ctx.setFill ( Color.RED );
         ctx.fillText ( "JAVAFX - TERCEIRA JANELA", 150, 80 );
         // Criando uma moldura na janela
         root.setStyle ( "-fx-padding: 5;" + "-fx-border-style: solid inside;"
                   + "-fx-border-width: 10;" + "-fx-border-insets: 5;"
                   + "-fx-border-radius: 5;" + "-fx-border-color: pink;" );
         Menu ( stage );
         pro.Informe ( ctx );
         root.getChildren ( ).add ( canvas );
         root.setTop ( menubar );
         stage.setScene ( scene );
         stage.show ( );
     }
}
// /////////////////////////////////////////////////////////////////////////////
class Janela_2 {
     PROJETO pro = new PROJETO ( );
     BorderPane root = new BorderPane ( );
     Scene scene = new Scene ( root, 500, 300 );
     Canvas canvas = new Canvas ( 500, 300 );
     GraphicsContext ctx = canvas.getGraphicsContext2D ( );
     MenuBar menubar = new MenuBar ( );
     Menu menu = new Menu ( "Arq" );
     MenuItem exit = new MenuItem ( "Sair" );
     MenuItem jan_1 = new MenuItem ( "Janela_1" );
     MenuItem jan_3 = new MenuItem ( "Janela_3" );
     // /////////////////////////////////////////////////////////////////////////
     public void Menu ( Stage stage ) {
         menu.getItems ( ).addAll ( jan_1, jan_3, exit );
         menubar.getMenus ( ).add ( menu );
         exit.setOnAction ( new EventHandler < ActionEvent > ( ) {
              @Override
              public void handle ( ActionEvent e ) {
                   stage.close ( );
              }
         } );
         jan_1.setOnAction ( new EventHandler < ActionEvent > ( ) {
              @Override
              public void handle ( ActionEvent e ) {
                   pro.start ( stage );
              }
         } );
         jan_3.setOnAction ( new EventHandler < ActionEvent > ( ) {
              @Override
              public void handle ( ActionEvent e ) {
                   new Janela_3 ( stage );
              }
         } );
         exit.setAccelerator ( new KeyCodeCombination ( KeyCode.E,
                   KeyCombination.CONTROL_DOWN ) );
         jan_1.setAccelerator ( new KeyCodeCombination ( KeyCode.C,
                   KeyCombination.CONTROL_DOWN ) );
         jan_3.setAccelerator ( new KeyCodeCombination ( KeyCode.D,
                   KeyCombination.CONTROL_DOWN ) );
     }
     // /////////////////////////////////////////////////////////////////////////
     Janela_2 ( Stage stage ) {
         ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 15 ) );
         ctx.setFill ( Color.RED );
         ctx.fillText ( "JAVAFX - SGUNDA JANELA", 150, 80 );
         // Criando uma moldura na janela
         root.setStyle ( "-fx-padding: 5;" + "-fx-border-style: solid inside;"
                   + "-fx-border-width: 10;" + "-fx-border-insets: 5;"
                   + "-fx-border-radius: 5;" + "-fx-border-color: darkgreen;" );
         Menu ( stage );
         pro.Informe ( ctx );
         root.getChildren ( ).add ( canvas );
         root.setTop ( menubar );
         stage.setScene ( scene );
         stage.show ( );
     }
}
// /////////////////////////////////////////////////////////////////////////////
public class PROJETO extends Application {
     public Canvas canvas = new Canvas ( 500, 300 );
     public GraphicsContext ctx = canvas.getGraphicsContext2D ( );
     BorderPane root = new BorderPane ( );
     Scene scene = new Scene ( root, 500, 300 );
     MenuBar menubar = new MenuBar ( );
     Menu menu = new Menu ( "Arq" );
     MenuItem exit = new MenuItem ( "Sair" );
     MenuItem jan_2 = new MenuItem ( "Janela_2" );
     MenuItem jan_3 = new MenuItem ( "Janela_3" );
     // /////////////////////////////////////////////////////////////////////////
     public void Informe ( GraphicsContext ctx ) {      
         ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 13 ) );
         ctx.setFill ( Color.RED );
         ctx.fillText ( "Por: ", 180, 235 );
         ctx.setFill ( Color.BLUE );
         ctx.fillText ( "Samuel Lima", 210, 235 );
         ctx.setFill ( Color.BLACK );
         ctx.fillText ( "sa_sp10@hotmail.com", 180, 250 );
         ctx.setFill ( Color.RED );
         ctx.fillText ( " MUITO OBRIGADO", 200, 275 );
     }
     // /////////////////////////////////////////////////////////////////////////
     public void Menu ( Stage stage ) {
         menu.getItems ( ).addAll ( jan_2, jan_3, exit );
         menubar.getMenus ( ).add ( menu );
         exit.setOnAction ( new EventHandler < ActionEvent > ( ) {
              @Override
              public void handle ( ActionEvent e ) {
                   stage.close ( );
              }
         } );
         jan_2.setOnAction ( new EventHandler < ActionEvent > ( ) {
              @Override
              public void handle ( ActionEvent e ) {
                   new Janela_2 ( stage );
              }
         } );
         jan_3.setOnAction ( new EventHandler < ActionEvent > ( ) {
              @Override
              public void handle ( ActionEvent e ) {
                   new Janela_3 ( stage );
              }
         } );
         exit.setAccelerator ( new KeyCodeCombination ( KeyCode.E,
                   KeyCombination.CONTROL_DOWN ) );
         jan_2.setAccelerator ( new KeyCodeCombination ( KeyCode.C,
                   KeyCombination.CONTROL_DOWN ) );
         jan_3.setAccelerator ( new KeyCodeCombination ( KeyCode.D,
                   KeyCombination.CONTROL_DOWN ) );
     }
     // /////////////////////////////////////////////////////////////////////////
     public void start ( Stage stage ) {
         stage.setTitle ( "JAVAFX - ABRINDO JANELAS" );
         ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 15 ) );
         ctx.setFill ( Color.RED );
         ctx.fillText ( "JAVAFX - PRIMEIRA JANELA", 150, 80 );
         // Criando uma moldura no texarea
         root.setStyle ( "-fx-padding: 5;" + "-fx-border-style: solid inside;"
                   + "-fx-border-width: 12;" + "-fx-border-insets: 5;"
                   + "-fx-border-radius: 5;" + "-fx-border-color: blue;" );
         Menu ( stage );
         Informe ( ctx );
         root.getChildren ( ).add ( canvas );
         root.setTop ( menubar );
         stage.setScene ( scene );
         stage.show ( );
     }
     // /////////////////////////////////////////////////////////////////////////
     public static void main ( String [ ] args ) {
         Application.launch ( args );
     }
}

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.