Ir ao conteúdo

Posts recomendados

Postado

Olá a todos. Criei esse código e consegui executar a primeira tela. Mas preciso VALIDAR o Nome e a Matrícula e dá erro. Desde já agradeço. Segue o código:

package br.com.ead.matricula;



import javax.swing.JOptionPane;

import javax.swing.JTextField;



public class AlunoTeste2 {

  

                public static void main(String[] args) {

                                                              

                               JTextField nome = new JTextField();

                               JTextField matricula = new JTextField();



                              

                               Object[] escola = {

                                    "Nome:", nome,

                                    "Matrícula:", matricula,                                                         

                               };                                                  

                              

                               JOptionPane.showConfirmDialog(null, escola,"Validação de Alunos- PROJEÇÃO", JOptionPane.OK_CANCEL_OPTION);



------------------------------------------------------------



A seguir criei esse código para tentar validar os campos mas não consigo "amarrar" os códigos, segue:



if(escola == null) {

                                               JOptionPane.showMessageDialog(null, "Você pressionou CANCEL");

                               }

                               if (escola!=null) {

                                  try {

                                                  int matricula =  Integer.parseInt(escola);

                                                 

                                                       if (matricula>=200010000 && matricula<202000000) {

                                                               JOptionPane.showMessageDialog(null, "Matrícula Confere "+"\n"+matricula);                                                             

                                                       }

                                                       else {

                                                               JOptionPane.showMessageDialog(null, "Matrícula errada!");

                                                       }

                                  }catch (NumberFormatException e) {

                                         JOptionPane.showMessageDialog(null, "Entrada Errada");

                }

}

mensagem.png

Postado

Segue um exemplo feito em JavaFX, é só adaptar para suas necessidades:

package code;

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class App extends Application {
    private Label labelNome;
    private Label labelMatricula;

    private TextField fieldNome;
    private TextField fieldMatricula;

    private Button buttonCancelar;
    private Button buttonAcessar;

    private GridPane pane;
    private Scene scene;

    private Alert alert;

    private String nome;
    private int matricula;

    public App() {
        this.labelNome = new Label("Nome");
        this.labelMatricula = new Label("Matrícula");

        this.fieldNome = new TextField("Aluno");
        this.fieldMatricula = new TextField("200010000");

        this.buttonCancelar = new Button("Cancelar");
        this.buttonAcessar = new Button("Acessar");

        this.pane = new GridPane();
        this.scene = new Scene(pane);

        this.alert = new Alert(Alert.AlertType.NONE);

        this.nome = "";
        this.matricula = 0;
    }

    @Override
    public void start(Stage stage) {
        buttonCancelar.setOnAction(event -> {
            alert.setAlertType(Alert.AlertType.INFORMATION);
            alert.setHeaderText("Saindo");
            alert.setContentText("");
            alert.showAndWait();
            System.exit(0);
        });

        buttonAcessar.setOnAction(event -> {
            nome = fieldNome.getText();
            matricula = Integer.parseInt(fieldMatricula.getText());

            if (nome.length() < 3) {
                alert.setAlertType(Alert.AlertType.WARNING);
                alert.setHeaderText("Nome inválido");
                alert.setContentText("O nome não é válido");
                alert.showAndWait();

                fieldNome.clear();
                fieldNome.requestFocus();
            } else if (matricula < 200010000 || matricula >= 202000000) {
                alert.setAlertType(Alert.AlertType.WARNING);
                alert.setHeaderText("Matrícula inválida");
                alert.setContentText("A matrícula não confere");
                alert.showAndWait();

                fieldMatricula.clear();
                fieldMatricula.requestFocus();
            } else {
                alert.setAlertType(Alert.AlertType.INFORMATION);
                alert.setHeaderText("Acesso permitido");
                alert.setContentText("Tudo certo");
                alert.showAndWait();

                fieldNome.requestFocus();
                fieldNome.clear();
                fieldMatricula.clear();
            }
        });

        GridPane.setConstraints(labelNome, 0, 0, 1, 1, HPos.RIGHT, VPos.CENTER);
        GridPane.setConstraints(fieldNome, 1, 0, 1, 1, HPos.LEFT, VPos.CENTER);
        GridPane.setConstraints(labelMatricula, 0, 1, 1, 1, HPos.RIGHT, VPos.CENTER);
        GridPane.setConstraints(fieldMatricula, 1, 1, 1, 1, HPos.LEFT, VPos.CENTER);
        GridPane.setConstraints(buttonCancelar, 0, 2, 1, 1, HPos.LEFT, VPos.CENTER);
        GridPane.setConstraints(buttonAcessar, 1, 2, 1, 1, HPos.RIGHT, VPos.CENTER);

        pane.setAlignment(Pos.CENTER);
        pane.setHgap(15);
        pane.setVgap(15);
        pane.setPadding(new Insets(20));
        pane.getChildren().addAll(labelNome, fieldNome,
                labelMatricula, fieldMatricula,
                buttonCancelar, buttonAcessar);

        stage.setTitle("Programa de Acesso");
        stage.setScene(scene);
        stage.show();
    }
}

 

Postado

@AdrianoSiqueira Olá. Não está faltando o método main no seu código?

public static void main(String[] args) {
    launch(args);
}

(!?)

Postado

@AdrianoSiqueira Olá. O método main é obrigatório, e só recebe essa chamada no JavaFX:

launch(args);

No Eclipse não roda de jeito nenhum sem o método main :)

 

Crie uma conta ou entre para comentar

Você precisa ser um usuário para fazer um comentário

Criar uma conta

Crie uma nova conta em nossa comunidade. É fácil!

Crie uma nova conta

Entrar

Já tem uma conta? Faça o login.

Entrar agora

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

LANÇAMENTO!

eletronica2025-popup.jpg


CLIQUE AQUI E BAIXE AGORA MESMO!