sábado, 10 de fevereiro de 2018

Javafx preenchendo array com button

Adaptado do código de uma calculadora que eu
mesmo criei, apresento aqui um exemplo de como
preencher um array de inteiros usando os clickes
do mouse sobre botões personalizados.
O tamanho do código assusta iniciantes, mas quem
já tem alguns conhecimentos com interface gráficas
com javafx sabe que não há nada de complicado nisto.
 
Veja abaixo o vídeo do programa:


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


Veja abaixo o código do programa:


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.Node;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

////////////////////////////////////////////////////////////////////////////////
public class PROJETO extends Application {
     static Boolean x = false;
     static String key = "";
     static String str_3 = "";
     static String str_4 = "";
     static int TAM = 100;
     static int [ ] arr = new int [ TAM ];
     static int a = 0, i;
     static char vet;
     private static Button btn_del = new Button ( );
     private static Button btn_mais = new Button ( "+" );
     private static Button btn_1 = new Button ( "1" );
     private static Button btn_2 = new Button ( "2" );
     private static Button btn_3 = new Button ( "3" );
     private static Button btn_4 = new Button ( "4" );
     private static Button btn_5 = new Button ( "5" );
     private static Button btn_6 = new Button ( "6" );
     private static Button btn_7 = new Button ( "7" );
     private static Button btn_8 = new Button ( "8" );
     private static Button btn_9 = new Button ( "9" );
     private static Button btn_0 = new Button ( "0" );
     static Canvas canvas = new Canvas ( 340, 240 );
     static GraphicsContext ctx = canvas.getGraphicsContext2D ( );
     static Group root = new Group ( );
     static Scene scene = new Scene ( root, 340, 240, Color.BLACK );
     static TextField name = new TextField ( );
     static GridPane grid = new GridPane ( );
     // /////////////////////////////////////////////////////////////////////////
     static Text t_0 = new Text ( );
     static Text t_1 = new Text ( );
     static Text t_2 = new Text ( );
     static Text t_3 = new Text ( );
     static Text t_4 = new Text ( );
     static Text t_5 = new Text ( );
     // /////////////////////////////////////////////////////////////////////////
     public static void Numeros ( ) {
         t_0.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 25 ) );
         t_1.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 25 ) );
         t_2.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 15 ) );
         t_3.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 15 ) );
         t_4.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 15 ) );
         t_5.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 25 ) );
         /////////////////////////
         t_5.setX ( 285 );//coluna
         t_5.setY ( 209 );//linha
         t_5.setText ( ">" );
     }
     // /////////////////////////////////////////////////////////////////////////
     public static Node But_ton_del ( ) {
         btn_del.setStyle ( "-fx-font: 20 arial;"
                   + "-fx-background-color: blue;" );
         btn_del.addEventHandler ( MouseEvent.MOUSE_ENTERED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   btn_del.setStyle ( "-fx-font: 20 arial;"
                            + "-fx-background-color: red;" );
              }
         } );
         btn_del.addEventHandler ( MouseEvent.MOUSE_EXITED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   btn_del.setStyle ( "-fx-font: 20 arial;"
                            + "-fx-background-color: blue;" );
              }
         } );
         btn_del.setPrefWidth ( 40 );// Largura do botão
         btn_del.setLayoutX ( 274 );// Posição do botão coluna
         btn_del.setPrefHeight ( 20 );// altura do botão
         btn_del.setLayoutY ( 181 );// //Posição do botão linha
         btn_del.setOnAction ( new EventHandler < ActionEvent > ( ) {
              public void handle ( ActionEvent event ) {
                   name.setEditable ( true );
                   key = btn_del.getText ( );
                   name.deletePreviousChar ( );
                   name.appendText ( key );
                   str_3 = name.getText ( ).toString ( );
                   i = Integer.parseInt ( str_3 );
                   arr [ a ] = i;
              }
         } );
         return btn_del;
     }
     // /////////////////////////////////////////////////////////////////////////
     public static Node But_ton_9 ( ) {
         btn_9.setStyle ( "-fx-font: 20 arial;"
                   + "-fx-background-color: blue;" );
         btn_9.addEventHandler ( MouseEvent.MOUSE_ENTERED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   btn_9.setStyle ( "-fx-font: 20 arial;"
                            + "-fx-background-color: red;" );
              }
         } );
         btn_9.addEventHandler ( MouseEvent.MOUSE_EXITED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   btn_9.setStyle ( "-fx-font: 20 arial;"
                            + "-fx-background-color: blue;" );
              }
         } );
         btn_9.setPrefWidth ( 40 );// Largura do botão
         btn_9.setLayoutX ( 130 );// Posição do botão coluna
         btn_9.setPrefHeight ( 20 );// altura do botão
         btn_9.setLayoutY ( 181 );// //Posição do botão linha
         btn_9.setOnAction ( new EventHandler < ActionEvent > ( ) {
              public void handle ( ActionEvent event ) {
                   name.appendText ( "9" );
                   str_3 = name.getText ( ).toString ( );
                   i = Integer.parseInt ( str_3 );
                   arr [ a ] = i;
              }
         } );
         return btn_9;
     }
     // /////////////////////////////////////////////////////////////////////////
     public static Node But_ton_8 ( ) {
         btn_8.setStyle ( "-fx-font: 20 arial;"
                   + "-fx-background-color: blue;" );
         btn_8.addEventHandler ( MouseEvent.MOUSE_ENTERED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   btn_8.setStyle ( "-fx-font: 20 arial;"
                            + "-fx-background-color: red;" );
              }
         } );
         btn_8.addEventHandler ( MouseEvent.MOUSE_EXITED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   btn_8.setStyle ( "-fx-font: 20 arial;"
                            + "-fx-background-color: blue;" );
              }
         } );
         btn_8.setPrefWidth ( 40 );// Largura do botão
         btn_8.setLayoutX ( 82 );// Posição do botão coluna
         btn_8.setPrefHeight ( 20 );// altura do botão
         btn_8.setLayoutY ( 181 );// //Posição do botão linha
         btn_8.setOnAction ( new EventHandler < ActionEvent > ( ) {
              public void handle ( ActionEvent event ) {
                   name.appendText ( "8" );
                   str_3 = name.getText ( ).toString ( );
                   i = Integer.parseInt ( str_3 );
                   arr [ a ] = i;
              }
         } );
         return btn_8;
     }
     // /////////////////////////////////////////////////////////////////////////
     public static Node But_ton_7 ( ) {
         btn_7.setStyle ( "-fx-font: 20 arial;"
                   + "-fx-background-color: blue;" );
         btn_7.addEventHandler ( MouseEvent.MOUSE_ENTERED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   btn_7.setStyle ( "-fx-font: 20 arial;"
                            + "-fx-background-color: red;" );
              }
         } );
         btn_7.addEventHandler ( MouseEvent.MOUSE_EXITED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   btn_7.setStyle ( "-fx-font: 20 arial;"
                            + "-fx-background-color: blue;" );
              }
         } );
         btn_7.setPrefWidth ( 40 );// Largura do botão
         btn_7.setLayoutX ( 32 );// Posição do botão coluna
         btn_7.setPrefHeight ( 20 );// altura do botão
         btn_7.setLayoutY ( 181 );// //Posição do botão linha
         btn_7.setOnAction ( new EventHandler < ActionEvent > ( ) {
              public void handle ( ActionEvent event ) {
                   name.appendText ( "7" );
                   str_3 = name.getText ( ).toString ( );
                   i = Integer.parseInt ( str_3 );
                   arr [ a ] = i;
              }
         } );
         return btn_7;
     }
     // /////////////////////////////////////////////////////////////////////////
     public static Node But_ton_6 ( ) {
         btn_6.setStyle ( "-fx-font: 20 arial;"
                   + "-fx-background-color: blue;" );
         btn_6.addEventHandler ( MouseEvent.MOUSE_ENTERED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   btn_6.setStyle ( "-fx-font: 20 arial;"
                            + "-fx-background-color: red;" );
              }
         } );
         btn_6.addEventHandler ( MouseEvent.MOUSE_EXITED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   btn_6.setStyle ( "-fx-font: 20 arial;"
                            + "-fx-background-color: blue;" );
              }
         } );
         btn_6.setPrefWidth ( 40 );// Largura do botão
         btn_6.setLayoutX ( 274 );// Posição do botão coluna
         btn_6.setPrefHeight ( 20 );// altura do botão
         btn_6.setLayoutY ( 135 );// //Posição do botão linha
         btn_6.setOnAction ( new EventHandler < ActionEvent > ( ) {
              public void handle ( ActionEvent event ) {
                   name.appendText ( "6" );
                   str_3 = name.getText ( ).toString ( );
                   i = Integer.parseInt ( str_3 );
                   arr [ a ] = i;
              }
         } );
         return btn_6;
     }
     // /////////////////////////////////////////////////////////////////////////
     public static Node But_ton_5 ( ) {
         btn_5.setStyle ( "-fx-font: 20 arial;"
                   + "-fx-background-color: blue;" );
         btn_5.addEventHandler ( MouseEvent.MOUSE_ENTERED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   btn_5.setStyle ( "-fx-font: 20 arial;"
                            + "-fx-background-color: red;" );
              }
         } );
         btn_5.addEventHandler ( MouseEvent.MOUSE_EXITED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   btn_5.setStyle ( "-fx-font: 20 arial;"
                            + "-fx-background-color: blue;" );
              }
         } );
         btn_5.setPrefWidth ( 40 );// Largura do botão
         btn_5.setLayoutX ( 226 );// Posição do botão coluna
         btn_5.setPrefHeight ( 20 );// altura do botão
         btn_5.setLayoutY ( 135 );// //Posição do botão linha
         btn_5.setOnAction ( new EventHandler < ActionEvent > ( ) {
              public void handle ( ActionEvent event ) {
                   name.appendText ( "5" );
                   str_3 = name.getText ( ).toString ( );
                   i = Integer.parseInt ( str_3 );
                   arr [ a ] = i;
              }
         } );
         return btn_5;
     }
     // /////////////////////////////////////////////////////////////////////////
     public static Node But_ton_4 ( ) {
         btn_4.setStyle ( "-fx-font: 20 arial;"
                   + "-fx-background-color: blue;" );
         btn_4.addEventHandler ( MouseEvent.MOUSE_ENTERED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   btn_4.setStyle ( "-fx-font: 20 arial;"
                            + "-fx-background-color: red;" );
              }
         } );
         btn_4.addEventHandler ( MouseEvent.MOUSE_EXITED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   btn_4.setStyle ( "-fx-font: 20 arial;"
                            + "-fx-background-color: blue;" );
              }
         } );
         btn_4.setPrefWidth ( 40 );// Largura do botão
         btn_4.setLayoutX ( 178 );// Posição do botão coluna
         btn_4.setPrefHeight ( 20 );// altura do botão
         btn_4.setLayoutY ( 135 );// //Posição do botão linha
         btn_4.setOnAction ( new EventHandler < ActionEvent > ( ) {
              public void handle ( ActionEvent event ) {
                   name.appendText ( "4" );
                   str_3 = name.getText ( ).toString ( );
                   i = Integer.parseInt ( str_3 );
                   arr [ a ] = i;
              }
         } );
         return btn_4;
     }
     // /////////////////////////////////////////////////////////////////////////
     public static Node But_ton_3 ( ) {
         btn_3.setStyle ( "-fx-font: 20 arial;"
                   + "-fx-background-color: blue;" );
         btn_3.addEventHandler ( MouseEvent.MOUSE_ENTERED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   btn_3.setStyle ( "-fx-font: 20 arial;"
                            + "-fx-background-color: red;" );
              }
         } );
         btn_3.addEventHandler ( MouseEvent.MOUSE_EXITED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   btn_3.setStyle ( "-fx-font: 20 arial;"
                            + "-fx-background-color: blue;" );
              }
         } );
         btn_3.setPrefWidth ( 40 );// Largura do botão
         btn_3.setLayoutX ( 130 );// Posição do botão coluna
         btn_3.setPrefHeight ( 20 );// altura do botão
         btn_3.setLayoutY ( 135 );// //Posição do botão linha
         btn_3.setOnAction ( new EventHandler < ActionEvent > ( ) {
              public void handle ( ActionEvent event ) {
                   name.appendText ( "3" );
                   str_3 = name.getText ( ).toString ( );
                   i = Integer.parseInt ( str_3 );
                   arr [ a ] = i;
              }
         } );
         return btn_3;
     }
     // /////////////////////////////////////////////////////////////////////////
     public static Node But_ton_2 ( ) {
         btn_2.setStyle ( "-fx-font: 20 arial;"
                   + "-fx-background-color: blue;" );
         btn_2.addEventHandler ( MouseEvent.MOUSE_ENTERED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   btn_2.setStyle ( "-fx-font: 20 arial;"
                            + "-fx-background-color: red;" );
              }
         } );
         btn_2.addEventHandler ( MouseEvent.MOUSE_EXITED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   btn_2.setStyle ( "-fx-font: 20 arial;"
                            + "-fx-background-color: blue;" );
              }
         } );
         btn_2.setPrefWidth ( 40 );// Largura do botão
         btn_2.setLayoutX ( 82 );// Posição do botão coluna
         btn_2.setPrefHeight ( 20 );// altura do botão
         btn_2.setLayoutY ( 135 );// //Posição do botão linha
         btn_2.setOnAction ( new EventHandler < ActionEvent > ( ) {
              public void handle ( ActionEvent event ) {
                   name.appendText ( "2" );
                   str_3 = name.getText ( ).toString ( );
                   i = Integer.parseInt ( str_3 );
                   arr [ a ] = i;
              }
         } );
         return btn_2;
     }
     // /////////////////////////////////////////////////////////////////////////
     public static Node But_ton_1 ( ) {
         btn_1.setStyle ( "-fx-font: 20 arial;"
                   + "-fx-background-color: blue;" );
         btn_1.addEventHandler ( MouseEvent.MOUSE_ENTERED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   btn_1.setStyle ( "-fx-font: 20 arial;"
                            + "-fx-background-color: red;" );
              }
         } );
         btn_1.addEventHandler ( MouseEvent.MOUSE_EXITED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   btn_1.setStyle ( "-fx-font: 20 arial;"
                            + "-fx-background-color: blue;" );
              }
         } );
         btn_1.setPrefWidth ( 40 );// Largura do botão
         btn_1.setLayoutX ( 32 );// Posição do botão coluna
         btn_1.setPrefHeight ( 20 );// altura do botão
         btn_1.setLayoutY ( 135 );// //Posição do botão linha
         btn_1.setOnAction ( new EventHandler < ActionEvent > ( ) {
              public void handle ( ActionEvent event ) {
                   name.appendText ( "1" );
                   str_3 = name.getText ( ).toString ( );
                   i = Integer.parseInt ( str_3 );
                   arr [ a ] = i;
              }
         } );
         return btn_1;
     }
     // /////////////////////////////////////////////////////////////////////////
     public static Node But_ton_0 ( ) {
         btn_0.setStyle ( "-fx-font: 20 arial;"
                   + "-fx-background-color: blue;" );
         btn_0.addEventHandler ( MouseEvent.MOUSE_ENTERED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   btn_0.setStyle ( "-fx-font: 20 arial;"
                            + "-fx-background-color: red;" );
              }
         } );
         btn_0.addEventHandler ( MouseEvent.MOUSE_EXITED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   btn_0.setStyle ( "-fx-font: 20 arial;"
                            + "-fx-background-color: blue;" );
              }
         } );
         btn_0.setPrefWidth ( 40 );// Largura do botão
         btn_0.setLayoutX ( 178 );// Posição do botão coluna
         btn_0.setPrefHeight ( 20 );// altura do botão
         btn_0.setLayoutY ( 181 );// //Posição do botão linha
         btn_0.setOnAction ( new EventHandler < ActionEvent > ( ) {
              public void handle ( ActionEvent event ) {
                   name.appendText ( "0" );
                   str_3 = name.getText ( ).toString ( );
                   i = Integer.parseInt ( str_3 );
                   arr [ a ] = i;
              }
         } );
         return btn_0;
     }
     // /////////////////////////////////////////////////////////////////////////
     public static Node But_ton_mais ( ) {
         btn_mais.setStyle ( "-fx-font: 20 arial;"
                   + "-fx-background-color: blue;" );
         btn_mais.addEventHandler ( MouseEvent.MOUSE_ENTERED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   btn_mais.setStyle ( "-fx-font: 20 arial;"
                            + "-fx-background-color: red;" );
              }
         } );
         btn_mais.addEventHandler ( MouseEvent.MOUSE_EXITED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   btn_mais.setStyle ( "-fx-font: 20 arial;"
                            + "-fx-background-color: blue;" );
              }
         } );
         btn_mais.setPrefWidth ( 40 );// Largura do botão
         btn_mais.setLayoutX ( 226 );// Posição do botão coluna
         btn_mais.setPrefHeight ( 20 );// altura do botão
         btn_mais.setLayoutY ( 181 );// //Posição do botão linha
         btn_mais.setOnAction ( new EventHandler < ActionEvent > ( ) {
              public void handle ( ActionEvent event ) {
                   str_3 = "";
                   i = 0;
                   int b = 0;
                   vet = '+';
                   a++;
                   if ( a == 5 ) {
                        for ( b = 0; b < a; b++ ) {
                            str_4 += arr [ b ] + "\t";
                            x = true;
                        }
                   }
                   if ( x == true ) {
                        t_2.setX ( 90 );
                        t_2.setY ( 45 );
                        t_2.setText ( "Abaixo o array preenchido" );
                        t_3.setX ( 100 );
                        t_3.setY ( 75 );
                        t_3.setText ( str_4 );
                        t_4.setX ( 120 );
                        t_4.setY ( 105 );
                        t_4.setText ( "Por: Samuel Lima" );
                   }
                   name.clear ( );
              }
         } );
         return btn_mais;
     }
     // /////////////////////////////////////////////////////////////////////////
     public static int Text_Field ( ) {
         grid.setVgap ( 5 );
         grid.setHgap ( 5 );
         grid.setMaxSize ( 340, 440 );
         name.setPadding ( new Insets ( 69, 20, 5, 35 ) );
         name.setTranslateX ( 31 );
         name.setTranslateY ( 19 );
         name.setAlignment ( Pos.BOTTOM_RIGHT );
         name.setStyle ( "-fx-font-family: Consolas;"
                   + "-fx-font-style: italic;" + "-fx-font-size: 20px;"
                   + "-fx-base: #183308;" );
         name.setPrefColumnCount ( 21 );
         name.setVisible ( true );
         // Desliga as piscadas do cursor
         name.setEditable ( false );
         return 0;
     }
     // /////////////////////////////////////////////////////////////////////////
     @Override
     public void start ( Stage stage ) throws FileNotFoundException {
         stage.setTitle ( "JAVAFX - PREENCHENDO ARRAY COM BUTTON" );
         Text_Field ( );
         Numeros ( );
         But_ton_0 ( );
         But_ton_1 ( );
         But_ton_2 ( );
         But_ton_3 ( );
         But_ton_4 ( );
         But_ton_5 ( );
         But_ton_6 ( );
         But_ton_7 ( );
         But_ton_8 ( );
         But_ton_9 ( );
         But_ton_del ( );
         But_ton_mais ( );
         root.getChildren ( ).addAll ( canvas, grid, name, btn_del, btn_mais,
                   btn_0, btn_1, btn_2, btn_3, btn_4, btn_5, btn_6, btn_7, btn_8,
                   btn_9, t_0, t_1, t_2, t_3, t_4, t_5 );
         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.