sexta-feira, 22 de dezembro de 2017

JavaFx - Imprimindo valores em TextArea

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


Veja abaixo o código do programa:



import javafx.scene.Scene;

import javafx.scene.control.TextArea;
import javafx.stage.Stage;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.Pane;
import javafx.event.ActionEvent;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.event.EventHandler;
import javafx.scene.paint.Color;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.concurrent.Task;

public class PROJETO extends Application {
     static String str_2 = "MUITO OBRIGADO";
     static String str_1 = "Por: Samuel Lima";
     static String str = new String ( );
     static String str_3 = new String ( );
     static String str_4 = "";
     static String str_5 = "";
     static Button btn_1 = new Button ( "Inf" );
     static Button btn_2 = new Button ( "Cls" );
    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 Group g = new Group ( );
     static Canvas canvas = new Canvas (  );
     static GraphicsContext ctx = canvas.getGraphicsContext2D ( );
     static TextArea    text          = new TextArea ( );   
     static VBox vbox = new VBox ( );
     static GridPane grid = new GridPane ( ); 
     static MenuBar menuBar = new MenuBar ( );
     static Pane ro_ot = new Pane ( );
     static BorderPane root = new BorderPane ( );
     static Scene scene = new Scene ( vbox, 500, 500 );
///////////////////////////////////////////////////////////////////////////
     public static void TextString ( ) {
         t_0.setFont ( Font.font ( "Arial", FontWeight.BOLD, 25 ) );
         t_1.setFont ( Font.font ( null, FontWeight.BOLD, 15 ) );
         t_2.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 25 ) );
     }
     // /////////////////////////////////////////////////////////////////////////
     public static Node But_ton_1 ( ) {
         btn_1.setPrefWidth ( 50 );// Largura do botão
         btn_1.setLayoutX ( 29 );// Posição do botão coluna
         btn_1.setPrefHeight ( 20 );// altura do botão
         btn_1.setLayoutY ( 40 );// //Posição do botão linha
         btn_1.setOnAction ( new EventHandler < ActionEvent > ( ) {
              @Override
              public void handle ( ActionEvent event ) {
                   int b = 0;
                   b = 1;
                   Sleep ( 1800, b );
                   b = 2;
                   Sleep ( 2800, b );
              }
         } );
         ro_ot.getChildren ( ).add ( btn_1 );
         return btn_1;
     }
////////////////////////////////////////////////////////////////////////////
     private static Node But_ton_2 ( ) {
         btn_2 .setPrefWidth ( 50 );// Largura do botão
         btn_2.setLayoutX ( 357 );// Posição do botão coluna
         btn_2 .setPrefHeight ( 20 );// altura do botão
         btn_2 .setLayoutY ( 40 );// //Posição do botão linha
         btn_2.setOnAction ( new EventHandler < ActionEvent > ( ) {
              @Override
              public void handle ( ActionEvent event ) {
                   // Coluna, linha, comprimento, altura
                   // Apagando uma linha ou partes
                   ctx.clearRect ( 80, 120, 180, 80 );
                   t_2.setVisible( false );
                   t_3.setVisible( false );
                   btn_1.setVisible ( false );
                   btn_2.setVisible ( false );
              }
         } );
         ro_ot.getChildren ( ).add ( btn_2 );
         return btn_2;
     }
     // /////////////////////////////////////////////////////////////////////////
     // Chamando uma função depois de um certo tempo
     public static void Tempo_2 ( ) {
         t_3.setFont ( Font.font ( null, FontWeight.BOLD, 15 ) );
         t_3.setX ( 110 );
         t_3.setY ( 130 );
         t_3.setFill ( Color.BLUE );
         t_3.setText ( str_2 );
         root.getChildren ( ).add ( t_3 );
     }
     public static void Tempo_1 ( ) {
         t_2.setFont ( Font.font ( null, FontWeight.BOLD, 15 ) );
         t_2.setX ( 110 );
         t_2.setY ( 110 );
         t_2.setFill ( Color.RED );
         t_2.setText ( str_1 );
         root.getChildren ( ).add ( t_2 );
     }
     public static int Sleep ( int x, int k ) {
         Task < Integer > close = new Task < Integer > ( ) {
              @Override
              protected Integer call ( ) throws Exception {
                   Thread.sleep ( x );
                   return null;
              }
         };
         close.setOnSucceeded ( c -> {
              if ( k == 1 )
                   Tempo_1 ( );
              if ( k == 2 )
                   Tempo_2 ( );
         } );
         new Thread ( close ).start ( );
         return 0;
     }
     // /////////////////////////////////////////////////////////////////////////
     public static int Menu_Bar ( ) {
         EventHandler < ActionEvent > action = changeTabPlacement ( );
         Menu menu = new Menu ( "Arq" );
         MenuItem top = new MenuItem ( "Exit" );
         top.setOnAction ( action );
         menu.getItems ( ).add ( top );
         MenuItem bottom = new MenuItem ( "Inf" );
         bottom.setOnAction ( action );
         menu.getItems ( ).add ( bottom );
         menuBar.getMenus ( ).add ( menu );
         root.setTop ( menuBar );
         return 0;
     }
     // /////////////////////////////////////////////////////////////////////////
     private static EventHandler < ActionEvent > changeTabPlacement ( ) {
         return new EventHandler < ActionEvent > ( ) {
              public void handle ( ActionEvent event ) {
                   MenuItem mItem = ( MenuItem ) event.getSource ( );
                   String side = mItem.getText ( );
                   if ( "Exit".equalsIgnoreCase ( side ) ) {
                        System.exit ( 0 );
                   } else if ( "Inf".equalsIgnoreCase ( side ) ) {
                        But_ton_1 ( );
                        But_ton_2 ( );
                   }
              }
         };
     }
     // //////////////////////////////////////////////////////////////////////////
     static float Inte_iro ( double x ) {
         int inteiro = 0;
         inteiro = ( int ) x;
         if ( x == inteiro ) {
              return 1;
         } else {
              return 0;
         }
     }
     // /////////////////////////////////////////////////////////////////////////
     public static int Text_Area ( ){
         TextString ( );
         t_1.setX ( 60 );
         t_1.setY ( 50 );
         t_1.setFill ( Color.RED );
         t_1.setText ( "IMPRIMINDO VALORES EM TEXTAREA JAVAFX" );
         double x = 12.5;
         double y = 12;
         // Para números decimais
         if ( Inte_iro ( x ) == 0 ) {
              //System.out.println ( Math.sqrt ( x ) );
              str += Math.sqrt ( x );
         }        
         // Para números inteiros
         if ( Inte_iro ( y ) == 1 ) {
              //System.out.println ( Math.sqrt ( y ) );
              str_3 += Math.sqrt ( y );
         }
         text.setFont ( Font.font("Amble Cn", FontWeight.BOLD, 25 ) );
         text.appendText ( "Abaixo a raiz quadrada de => " );
         text.appendText ( "" + x + "\n" );
         text.appendText ( str );
         text.appendText ( "\nAbaixo a raiz quadrada de => " );
         text.appendText ( "" + y + "\n" );
         text.appendText ( str_3 );
         grid.setVgap ( 4 );
         grid.setHgap ( 2 );   
         root.setPadding ( new Insets ( 1, 1, 1, 1 ) );
         //Posiciona a Margem do TextArea
         grid.add ( text, 1, 20, 2, 10 );
         root.setMinWidth ( 150 );
         text.setStyle ( "-fx-font-style: italic;"
                   + " -fx-font-weight: bolder;"
                   +  " -fx-font-size: 20px;"
                   +  "-fx-alignment: center;"
                   + "-fx-text-fill: blue" );
         root.getChildren ( ).addAll ( grid, t_1, t_4 );
         return 0;
     }
////////////////////////////////////////////////////////////////////////////
     @Override
     public void start ( Stage stage ) throws Exception {
         stage.setTitle ( "IMPRIMINDO VALORES EM TEXTAREA JAVAFX" );
         Menu_Bar ( );
         // Dando uns efeitos na moldura
         vbox.setStyle ( "-fx-padding: 5;" + "-fx-border-style: solid inside;"
                   + "-fx-border-width: 18;" + "-fx-border-insets: 5;"
                   + "-fx-border-radius: 5;" + "-fx-border-color: blue;" );
         Text_Area ( );
         vbox.getChildren ( ).addAll ( canvas, root, ro_ot, grid );
         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.