domingo, 8 de abril de 2018

JavaFX - criando menu e item de menu

A habilidade clássica de selecionar
opções em programas com janelas gráficas
é através de menu.
A biblioteca javaFX oferece recursos
poderosos para criação de menus em estilo
profissional limitado apenas pelo
conhecimento do programador.
Neste exemplo estamos utilizando o recurso
de combinação de teclas para opcionalmente
selecionar um item de menu por atalhos de teclado.


Veja abaixo uma imagem do programa em execução: 


Veja abaixo o vídeo do programa:



Veja abaixo o código do programa:



import java.io.FileInputStream;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.util.Duration;

public class PROJETO extends Application {
     Canvas canvas = new Canvas ( 690, 350 );
     GraphicsContext ctx = canvas.getGraphicsContext2D ( );
     BorderPane root = new BorderPane ( );
     Scene scene = new Scene ( root, 660, 350 );
     MenuBar menuBar = new MenuBar ( );
     Menu mainMenu = new Menu ( "Arq" );
     MenuItem sair = new MenuItem ( "Sair" );
     MenuItem texto = new MenuItem ( "Texto" );
     Menu main_Menu = new Menu ( "Sobre" );
     MenuItem vis_vet = new MenuItem ( "Autor" );
     MenuItem vis_vet_1 = new MenuItem ( "JavaFx" );
     Timeline videoTick;
     Duration duration = Duration.millis ( 2000 );
     public static ImageView imageView = new ImageView ( );
     // /////////////////////////////////////////////////////////////////////////
     public void Informe ( ) {
         ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 13 ) );
         ctx.setFill ( Color.RED );
         ctx.fillText ( "Por: ", 250, 275 );
         ctx.setFill ( Color.BLUE );
         ctx.fillText ( "Samuel Lima", 280, 275 );
         ctx.setFill ( Color.BLACK );
         ctx.fillText ( "sa_sp10@hotmail.com", 250, 290 );
         ctx.setFill ( Color.RED );
         ctx.fillText ( " MUITO OBRIGADO", 280, 320 );
         videoTick = new Timeline ( new KeyFrame ( duration,
                   new EventHandler < ActionEvent > ( ) {
              public void handle ( ActionEvent actionEvent ) {
                   // Limpando a tela
                   // Coluna, linha, comprimento, altura
                   ctx.clearRect ( 240, 260, 330, 150 );
                   Principal ( );
              }
         } ) );
         videoTick.playFromStart ( );
     }
     // /////////////////////////////////////////////////////////////////////////
     public void Texto ( ) {
         ctx.clearRect ( 210, 70, 330, 50 );
         ctx.setFont ( Font.font ( "Arial", FontWeight.BOLD, 15 ) );
         ctx.setFill ( Color.RED );
         ctx.fillText ( "JAVAFX - CRIANDO MENU E ITEM DE MENU", 180, 80 );
         ctx.setFill ( Color.BLUE );
         int i;
         StringBuilder st = new StringBuilder ( );
         ArrayList < Character > arr = new ArrayList <> ( Arrays.asList (            
                   'V', 'I', 'V', 'A', ' ', ' ',
                   'J', 'A', 'V', 'A', ' ', ' ',
                   'V', 'I', 'V', 'A', ' ', ' ',
                   'J', 'A', 'V', 'A', 'F', 'X' ) );
         for ( i = 0; i < arr.size ( ); i++ ) {
              if ( i % 6 == 0 ) {
                   st.append ( "\n" );
              }
              st.append ( arr.get ( i ) + " " );
         }
         ctx.fillText ( " " + st, 220, 110 );
         videoTick = new Timeline ( new KeyFrame ( duration,
                   new EventHandler < ActionEvent > ( ) {
              public void handle ( ActionEvent actionEvent ) {
                   // Limpando a tela
                   // Coluna, linha, comprimento, altura
                   ctx.clearRect ( 210, 100, 330, 150 );
                   Principal ( );
              }
         } ) );
         videoTick.playFromStart ( );
     }
     // /////////////////////////////////////////////////////////////////////////
     public void Menu ( Stage stage ) {
         mainMenu.getItems ( ).addAll ( texto, sair );
         main_Menu.getItems ( ).addAll ( vis_vet, vis_vet_1 );
         menuBar.getMenus ( ).add ( mainMenu );
         menuBar.getMenus ( ).add ( main_Menu );
         sair.setOnAction ( actionEvent -> System.exit ( 0 ) );
         texto.setOnAction ( actionEvent -> Texto ( ) );
         vis_vet.setOnAction ( actionEvent -> Informe ( ) );
         vis_vet_1.setOnAction ( new EventHandler < ActionEvent > ( ) {
              @Override
              public void handle ( ActionEvent e ) {
                   try {
                        Image_View ( );
                   } catch ( FileNotFoundException e1 ) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                   }
              }
         } );
         texto.setAccelerator ( new KeyCodeCombination ( KeyCode.A,
                   KeyCombination.CONTROL_DOWN ) );
         sair.setAccelerator ( new KeyCodeCombination ( KeyCode.B,
                   KeyCombination.CONTROL_DOWN ) );
         vis_vet.setAccelerator ( new KeyCodeCombination ( KeyCode.C,
                   KeyCombination.CONTROL_DOWN ) );
         vis_vet_1.setAccelerator ( new KeyCodeCombination ( KeyCode.D,
                   KeyCombination.CONTROL_DOWN ) );
     }
     // /////////////////////////////////////////////////////////////////////////
     public void Principal ( ) {
         ctx.setFont ( Font.font ( "Arial", FontWeight.BOLD, 15 ) );
         ctx.setFill ( Color.RED );
         ctx.fillText ( "JAVAFX - CRIANDO MENU E ITEM DE MENU", 180, 80 );
     }
     // /////////////////////////////////////////////////////////////////////////
     public int Image_View ( ) throws FileNotFoundException {
         Image image = new Image ( new FileInputStream (
         "H:\\eclipse - luna java\\PROJETOS\\PROJETO\\src\\Foto\\JFX.png" ) );
         imageView = new ImageView ( image );
         //Coluna
         imageView.setX ( 210 );
         //Linha
         imageView.setY ( 90 );
         //Largura
         imageView.setFitHeight ( 640 );
         //Altura
         imageView.setFitWidth ( 250 );
         imageView.setPreserveRatio ( true );
         videoTick = new Timeline ( new KeyFrame ( duration,
                   new EventHandler < ActionEvent > ( ) {
              public void handle ( ActionEvent actionEvent ) {
                   imageView.setVisible ( false );
                   Principal ( );
              }
         } ) );
         videoTick.playFromStart ( );
         root.getChildren ( ).addAll ( imageView );
         return 0;
     }
     // /////////////////////////////////////////////////////////////////////////
     public void start ( Stage stage ) {
         // Criando moldura e aplicando efeitos
         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: cyan;" );
         stage.setTitle ( "JAVAFX - CRIANDO MENU E ITEM DE MENU" );
         Principal ( );
         Menu ( stage );
         root.getChildren ( ).addAll ( canvas );
         root.setTop ( menuBar );
         stage.setScene ( scene );
         stage.show ( );
     }
     // /////////////////////////////////////////////////////////////////////////
     public static void main ( String [ ] args ) {
         launch ( args );
     }
}
 

Nenhum comentário:

Postar um comentário

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