Advertisement
Alfoli

TelaGrafica

Feb 27th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.08 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.border.*;
  5. import java.util.List;
  6. import java.util.ArrayList;
  7.  
  8. public class TelaGrafica extends JFrame implements ActionListener{
  9.     JButton btnInsere, btnExibe;
  10.     JTextField txtNome, txtDia, txtMes, txtAno;
  11.     JLabel entranome, entradata;
  12.     List <Pessoa> lista = new ArrayList <Pessoa>(); //erro aq
  13.    
  14.     public TelaGrafica(){//desenha a tela, botoes tem eventos
  15.         super ("Lista de Pessoas");
  16.         setLayout (new BorderLayout());
  17.        
  18.         JPanel norte = new JPanel(new FlowLayout());
  19.         entranome = new JLabel("Entre com o nome: ", SwingConstants.CENTER);
  20.  
  21.         txtNome = new JTextField(20);
  22.         norte.add(entranome);
  23.         norte.add(txtNome);
  24.         add(norte, BorderLayout.NORTH);
  25.        
  26.         JPanel sul = new JPanel (new FlowLayout());
  27.         btnInsere = new JButton("Insere");
  28.         btnExibe = new JButton ("Exibe");
  29.         sul.add(btnInsere);
  30.         sul.add(btnExibe);
  31.         add(sul, BorderLayout.SOUTH);
  32.        
  33.         JPanel centro = new JPanel (new FlowLayout());
  34.         entradata = new JLabel ("Insira a data de nascimento:", SwingConstants.CENTER);
  35.         txtDia = new JTextField(3);
  36.         txtMes = new JTextField(3);
  37.         txtAno = new JTextField(4);
  38.         centro.add(entradata);
  39.         centro.add(txtDia);
  40.         centro.add(txtMes);
  41.         centro.add(txtAno);
  42.         Border b1 = BorderFactory.createEtchedBorder();
  43.         Border b2 = BorderFactory.createEtchedBorder();
  44.         centro.setBorder(b2);
  45.         add(centro, BorderLayout.CENTER);
  46.         btnInsere.addActionListener(this);
  47.         btnExibe.addActionListener(this);
  48.        
  49.     }
  50.    
  51.     @Override
  52.    
  53.     public void actionPerformed (ActionEvent e){
  54.         String nome; int dia, mes, ano;
  55.         if (e.getSource() == btnInsere){
  56.             nome = txtNome.getText();
  57.             try{
  58.                 dia = Integer.parseInt(txtDia.getText());
  59.                 mes = Integer.parseInt(txtMes.getText());
  60.                 ano = Integer.parseInt(txtAno.getText());
  61.             }catch(NumberFormatException erro){
  62.                 dia = 1;
  63.                 mes = 1;
  64.                 ano = 1990;
  65.             }
  66.             Pessoa p = new Pessoa(nome, dia, mes, ano);
  67.             p.verificaSigno();
  68.             lista.add(p);
  69.             JOptionPane.showMessageDialog(null, "Cadastrado com sucesso.");
  70.             txtNome.setText("");
  71.             txtDia.setText("");
  72.             txtMes.setText("");
  73.             txtAno.setText("");
  74.         }
  75.         if (e.getSource() == btnExibe){
  76.             String texto = "";
  77.             for (Pessoa p:lista){
  78.                 texto += String.format("\n %s", p.toString());
  79.             }
  80.             JOptionPane.showMessageDialog(null, texto);
  81.         }
  82.     }
  83.     public static void main (String args[]){
  84.         TelaGrafica tela = new TelaGrafica();
  85.         tela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  86.         tela.setSize(360,180);
  87.         tela.setLocation(500,300);
  88.         tela.setVisible(true);
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement