domingo, 21 de janeiro de 2018

Javafx - preenchendo um array de inteiro com textfield

Aqui está um bom exemplo de como usar o textfield
do javafx para preencher um array de tipo primtivo.
O código é indicado aos iniciantes de javafx.

 Veja abaixo imagens do programa em execução:




  Veja abaixo o código do programa:



import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.input.KeyCode;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.input.KeyEvent;
import java.util.function.UnaryOperator;
import javafx.scene.control.TextFormatter;
import javafx.scene.control.TextFormatter.Change;

public class PROJETO extends Application {
     static int k = 0, l = 0;
     public static int TAM = 10, inicio, fim;
     static Canvas canvas = new Canvas ( 620, 350 );
     static GraphicsContext ctx = canvas.getGraphicsContext2D ( );
     static Pane ro_ot = new Pane ( );
     static Scene sce_ne = new Scene ( ro_ot, 620, 350 );
     static Button btn_1 = new Button ( "TECLE ENTER" );
     static TextField textField_1 = new TextField ( );
     static TextField textField_2 = new TextField ( );
     static HBox hbox_1 = new HBox ( textField_1 );
     static HBox hbox_2 = new HBox ( textField_2 );
     static Text t_0 = new Text ( );
////////////////////////////////////////////////////////////////////////////
     public static void Informe ( ) {
         ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 13 ) );
         ctx.setFill ( Color.RED );
         ctx.fillText ( "Por: ", 200, 242 );
         ctx.setFill ( Color.BLUE );
         ctx.fillText ( "Samuel Lima", 240, 242 );
         ctx.setFill ( Color.BLACK );
         ctx.fillText ( "sa_sp10@hotmail.com", 200, 260 );
         ctx.setFill ( Color.RED );
         ctx.fillText ( " MUITO OBRIGADO", 250, 320 );
     }
     // /////////////////////////////////////////////////////////////////////////
     public static void limitTextField ( TextField textField, int limit ) {
         UnaryOperator < Change > textLimitFilter = change -> {
              if ( change.isContentChange ( ) ) {
                   int newLength = change.getControlNewText ( ).length ( );
                   if ( newLength > limit ) {
                        String trimmedText = change.getControlNewText ( )
                                 .substring ( 0, limit );
                        change.setText ( trimmedText );
                        int oldLength = change.getControlText ( ).length ( );
                        change.setRange ( 0, oldLength );
                   }
              }
              return change;
         };
         textField.setTextFormatter ( new TextFormatter < Object > (
                   textLimitFilter ) );
     }
     // /////////////////////////////////////////////////////////////////////////
     public static int Array_2 ( int vet [ ] ) {
         //Esconde o textfield
         textField_1 .setVisible ( false );
         t_0.setVisible ( false );
         hbox_2.setPadding ( new Insets ( 60, 0, 10, 340 ) );
         // Abaixo ajustamos o total de colunas do TextField
         textField_2.setPrefColumnCount ( 2 );
         textField_2.setPromptText ( "fim" );
         ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 15 ) );
         ctx.setFill ( Color.BLUE );
         ctx.fillText ( "Insira o ", 190, 80 );
         ctx.setFill ( Color.RED );
         ctx.fillText (  " 1º", 240, 80 );
         ctx.setFill ( Color.BLUE );
         ctx.fillText ( "número", 270, 80 );
         textField_2.setOnKeyPressed ( new EventHandler < KeyEvent > ( ) {
              @Override
              public void handle ( KeyEvent event ) {
                   limitTextField ( textField_2, 2 );
                   if ( event.getCode ( ) == KeyCode.ENTER ) {
                        ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 13 ) );
                        ctx.setFill ( Color.BLUE );
                        // convertendo string para inteiro
                        fim = Integer.parseInt ( textField_2.getText ( ) );
                        if ( Integer.parseInt ( textField_2.getText ( ) ) >= 0
                                 && Integer.parseInt ( textField_2.getText ( ) ) < 100 ) {
                            Inf_quant ( vet );
                        }
                   }
              }
         } );
         ro_ot.getChildren ( ).addAll ( hbox_2 );
         return 0;
     }
     // /////////////////////////////////////////////////////////////////////////
     public static int Inf_quant ( int vet [ ] ) {
         ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 15 ) );
         int a = 0, i;
         StringBuilder st = new StringBuilder ( );
         k++;
         int [ ] Vetor = new int [ inicio + 2 ];     
         for ( i = 1; i < inicio + 1 ; i++ ) {
              Vetor [ i ] = ' ';        
         }
         ctx.clearRect ( 180, 70, 200, 20 );
         ctx.clearRect ( 240, 70, 20, 20 );
         ctx.clearRect ( 260, 70, 200, 20 );
         //limpa o campo do textfiel;
         textField_2.clear ( );
         ctx.setFill ( Color.BLUE );
         ctx.fillText ( "Insira o ", 190, 80 );
         ctx.setFill ( Color.RED );
         ctx.fillText ( " " + ( k + 1 ) + " º", 240, 80 );
         ctx.setFill ( Color.BLUE );
         ctx.fillText ( "número", 275, 80 );
         vet [ k ] = fim;
         st.append ( vet [ k ]  );   
         for ( i = 0; i < inicio + 1; i++ ) {
              Vetor [ a ] = vet [ i  ];
              a++;
         }
         if ( k == inicio ){
              st = new StringBuilder ( );
              //Esconde o textfield
              textField_2 .setVisible ( false );
              ctx.clearRect ( 180, 70, 400, 20 );
              for ( i = 1; i < inicio + 1; i++ ){
                   if ( Vetor [ i ] != ' ' )
                        if ( Vetor [ i ] % 10 == 0 )
                            st.append ("\n");
                   if ( Vetor [ i ] >= 0 && Vetor [ i ] <= 9 )
                        st.append ( "0" );
                   System.out.printf ( "%3d", Vetor [ i ] );
                   st.append ( Vetor [ i ] + "\t");
              }
              ctx.setFill ( Color.BLUE );
              ctx.fillText ( "Veja abaixo o array preenchido", 220, 80 );
              ctx.setFill ( Color.RED );
              ctx.fillText ( " " + st, 160, 110 );
              Informe ( );
         }
         return 0;
     }
     // /////////////////////////////////////////////////////////////////////////
     public static int Array_1 ( int vet [ ] ) {
         hbox_1.setPadding ( new Insets ( 60, 0, 10, 420 ) );
         // Abaixo ajustamos o total de colunas do TextField
         textField_1.setPrefColumnCount ( 2 );
         textField_1.setPromptText ( "ini" );
         ctx.setFont ( Font.font ( "Tahoma", FontWeight.NORMAL, 12 ) );
         ctx.setFill ( Color.BLACK );
         t_0.setFont( Font.font ( "Arial", FontWeight.NORMAL, 15 ) );
         t_0.setFill ( Color.BLACK );
         t_0.setX ( 180 );// coluna
         t_0.setY ( 80 );// linha
         t_0.setText ( "Quantos números deseja inserir ?" );  
         textField_1.setOnKeyPressed ( new EventHandler < KeyEvent > ( ) {
              @Override
              public void handle ( KeyEvent event ) {
                   limitTextField ( textField_1, 2 );
                   if ( event.getCode ( ) == KeyCode.ENTER ) {
                        ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 13 ) );
                        ctx.setFill ( Color.BLUE );
                        // convertendo string para inteiro
                        inicio = Integer.parseInt ( textField_1.getText ( ) );
                        if ( Integer.parseInt ( textField_1.getText ( ) ) >= 0
                                 && Integer.parseInt ( textField_1.getText ( ) ) < 100 ) {
                            Array_2 ( vet );
                        }
                   }
              }
         } );
         return 0;
     }
     // ////////////////////////////////////////////////////////////////////////
     public void start ( Stage stage ) {
         ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 15 ) );
         stage.setTitle ( "JAVAFX - PREENCHENDO UM ARRAY COM TEXTFIELD" );
         //Criando moldura e dando efeitos
         ro_ot.setStyle ( "-fx-padding: 5;" + "-fx-border-style: solid inside;"
                   + "-fx-border-width: 12;" + "-fx-border-insets: 5;"
                   + "-fx-border-radius: 5;" + "-fx-border-color: black;" );
         ctx.setFill ( Color.RED );
         ctx.fillText ( "PREENCHENDO UM ARRAY COM TEXTFIELD", 150, 50 );
         ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 15 ) );
         int [ ] vet = new int [ 100 ];
         Array_1 ( vet );
         ro_ot.getChildren ( ).addAll ( canvas, hbox_1, t_0 );
         stage.setScene ( sce_ne );
         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.