Advertisement
hercioneto

Projeto nomes

Nov 28th, 2023
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.85 KB | None | 0 0
  1. import java.awt.Dimension;
  2. import java.awt.GridLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import javax.swing.JButton;
  6. import javax.swing.JComboBox;
  7. import javax.swing.JDialog;
  8. import javax.swing.JFrame;
  9. import javax.swing.JLabel;
  10. import javax.swing.JPanel;
  11. import javax.swing.JTextArea;
  12. import javax.swing.JTextField;
  13.  
  14. /**
  15.  *
  16.  * @author Professor
  17.  */
  18. public class Nomes {
  19.  
  20.     static void janela() {
  21.         String[] nomes = new String[7];
  22.  
  23.         JFrame j = new JFrame("LISTA NOMES");
  24.         j.setSize(400, 400);
  25.         j.setLocationRelativeTo(j);
  26.         j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27.  
  28.         j.setResizable(false);
  29.         JPanel p = new JPanel();
  30.  
  31.         GridLayout layout = new GridLayout(0, 2, 30, 30);
  32.         p.setLayout(layout);
  33.  
  34.         JLabel jlbNome = new JLabel("Digite o nome:");
  35.         jlbNome.setHorizontalAlignment(JLabel.RIGHT);
  36.         jlbNome.setHorizontalTextPosition(JLabel.RIGHT);
  37.  
  38.         JTextField jtxNome = new JTextField();
  39.         jtxNome.setPreferredSize(new Dimension(50, 24));
  40.  
  41.        
  42.  
  43.         JButton jbtAdicionar = new JButton("Adicionar nomes");
  44.         JButton jBtMostrar = new JButton("Mostrar nomes");
  45.         jBtMostrar.setBounds(50, 100, 95, 30);
  46.         jbtAdicionar.setBounds(50, 100, 95, 30);
  47.        
  48.         jbtAdicionar.addActionListener(new ActionListener() {
  49.         Integer i = 0;    
  50.             public void actionPerformed(ActionEvent ae) {
  51.                
  52.                 nomes[i]= jtxNome.getText();
  53.                 jtxNome.setText("");
  54.                
  55.                 i= i+1;
  56.                 if (i==7) { jtxNome.setEnabled(false);
  57.                 } else { jtxNome.grabFocus(); }
  58.             }
  59.         });
  60.  
  61.         jBtMostrar.addActionListener(new ActionListener() {
  62.             public void actionPerformed(ActionEvent ae) {
  63.                
  64.         JDialog d = new JDialog(j, "NOMES");
  65.         JTextArea jtArea = new JTextArea("NOMES");
  66.         jtArea.setSize(new Dimension(50, 150));
  67.        
  68.        
  69.                 String nomesT="";
  70.                 for (int i = nomes.length-1; i >=0; i--) {
  71.                     //System.out.println(nomes[i]);
  72.                     nomesT = nomesT + nomes[i] +"\n";
  73.                 }
  74.                 jtArea.setText(nomesT);
  75.            
  76.         d.add(jtArea);
  77.         d.setSize(200, 150);
  78.         d.setResizable(false);
  79.         d.setLocationRelativeTo(j);
  80.         d.setVisible(true);
  81.             }
  82.         });
  83.  
  84.         p.add(jlbNome);
  85.         p.add(jtxNome);
  86.        
  87.  
  88.         p.add(jbtAdicionar);
  89.         p.add(jBtMostrar);
  90.  
  91.         p.setAutoscrolls(true);
  92.         p.setPreferredSize(new Dimension(300, 200));
  93.  
  94.         j.add(p);
  95.         j.pack();
  96.  
  97.         j.setVisible(true);
  98.     }
  99.  
  100.     public static void main(String[] args) {
  101.         janela();
  102.     }
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement