Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.border.*;
- import java.util.List;
- import java.util.ArrayList;
- public class TelaGrafica extends JFrame implements ActionListener{
- JButton btnInsere, btnExibe;
- JTextField txtNome, txtDia, txtMes, txtAno;
- JLabel entranome, entradata;
- List <Pessoa> lista = new ArrayList <Pessoa>(); //erro aq
- public TelaGrafica(){//desenha a tela, botoes tem eventos
- super ("Lista de Pessoas");
- setLayout (new BorderLayout());
- JPanel norte = new JPanel(new FlowLayout());
- entranome = new JLabel("Entre com o nome: ", SwingConstants.CENTER);
- txtNome = new JTextField(20);
- norte.add(entranome);
- norte.add(txtNome);
- add(norte, BorderLayout.NORTH);
- JPanel sul = new JPanel (new FlowLayout());
- btnInsere = new JButton("Insere");
- btnExibe = new JButton ("Exibe");
- sul.add(btnInsere);
- sul.add(btnExibe);
- add(sul, BorderLayout.SOUTH);
- JPanel centro = new JPanel (new FlowLayout());
- entradata = new JLabel ("Insira a data de nascimento:", SwingConstants.CENTER);
- txtDia = new JTextField(3);
- txtMes = new JTextField(3);
- txtAno = new JTextField(4);
- centro.add(entradata);
- centro.add(txtDia);
- centro.add(txtMes);
- centro.add(txtAno);
- Border b1 = BorderFactory.createEtchedBorder();
- Border b2 = BorderFactory.createEtchedBorder();
- centro.setBorder(b2);
- add(centro, BorderLayout.CENTER);
- btnInsere.addActionListener(this);
- btnExibe.addActionListener(this);
- }
- @Override
- public void actionPerformed (ActionEvent e){
- String nome; int dia, mes, ano;
- if (e.getSource() == btnInsere){
- nome = txtNome.getText();
- try{
- dia = Integer.parseInt(txtDia.getText());
- mes = Integer.parseInt(txtMes.getText());
- ano = Integer.parseInt(txtAno.getText());
- }catch(NumberFormatException erro){
- dia = 1;
- mes = 1;
- ano = 1990;
- }
- Pessoa p = new Pessoa(nome, dia, mes, ano);
- p.verificaSigno();
- lista.add(p);
- JOptionPane.showMessageDialog(null, "Cadastrado com sucesso.");
- txtNome.setText("");
- txtDia.setText("");
- txtMes.setText("");
- txtAno.setText("");
- }
- if (e.getSource() == btnExibe){
- String texto = "";
- for (Pessoa p:lista){
- texto += String.format("\n %s", p.toString());
- }
- JOptionPane.showMessageDialog(null, texto);
- }
- }
- public static void main (String args[]){
- TelaGrafica tela = new TelaGrafica();
- tela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- tela.setSize(360,180);
- tela.setLocation(500,300);
- tela.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement