Advertisement
TermSpar

Object Oriented, GUIs, and ArrayLists

Jul 15th, 2016
1,767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.18 KB | None | 0 0
  1. package objects;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.ArrayList;
  6.  
  7. import javax.swing.JButton;
  8. import javax.swing.JFrame;
  9. import javax.swing.JOptionPane;
  10.  
  11. public class Main {
  12.  
  13.     private JFrame frame = new JFrame();
  14.     private JButton btnAddUser = new JButton();
  15.     private JButton btnDisplay = new JButton();
  16.    
  17.     //User Variables:
  18.     private ArrayList<User> userArray = new ArrayList<User>();
  19.     private String name;
  20.     private String strId;
  21.     private int id;
  22.    
  23.     public Main() {
  24.        
  25.         //Frame Attributes:
  26.         frame.setLayout(null);
  27.         frame.setTitle("User Data");
  28.         frame.setSize(400, 600);
  29.         frame.setVisible(true);
  30.         frame.setResizable(false);
  31.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  32.        
  33.         //btnAddUser Attributes:
  34.         btnAddUser.setText("Add User");
  35.         btnAddUser.setSize(200, 50);
  36.         btnAddUser.setLocation(90, 100);
  37.        
  38.         //btnAddUser Click Event:
  39.         btnAddUser.addActionListener(new ActionListener(){
  40.             public void actionPerformed(ActionEvent arg0) {
  41.                 name = JOptionPane.showInputDialog("Enter Name: ");
  42.                 strId = JOptionPane.showInputDialog("Enter ID: ");
  43.                 id = Integer.parseInt(strId);
  44.                
  45.                 //Add User To Array:
  46.                 User newUser = new User(name, id);
  47.                 userArray.add(newUser);
  48.             }
  49.         });
  50.        
  51.         //btnDisplay Attributes:
  52.         btnDisplay.setText("Display");
  53.         btnDisplay.setSize(200, 50);
  54.         btnDisplay.setLocation(90, 200);
  55.                
  56.         //btnDisplay Click Event:
  57.         btnDisplay.addActionListener(new ActionListener(){
  58.             public void actionPerformed(ActionEvent arg0) {
  59.                 for(int i = 0; i < userArray.size(); i++){
  60.                     JOptionPane.showMessageDialog(null, "Name: " + userArray.get(i).getName());
  61.                     JOptionPane.showMessageDialog(null, "ID: " + userArray.get(i).getID());
  62.                 }
  63.             }
  64.         });
  65.        
  66.         //Add Frame Objects:
  67.         frame.add(btnAddUser);
  68.         frame.add(btnDisplay);
  69.     }
  70.    
  71.     public static void main(String[] args){
  72.         new Main();
  73.     }
  74.  
  75. }
  76.  
  77.  
  78. package objects;
  79.  
  80. public class User {
  81.  
  82.     private String name;
  83.     private int id;
  84.    
  85.     public User(String newName, int newID) {
  86.         name = newName;
  87.         id = newID;
  88.     }
  89.    
  90.     public String getName(){
  91.         return name;
  92.     }
  93.    
  94.     public int getID(){
  95.         return id;
  96.     }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement