|
| 1 | +package View; |
| 2 | + |
| 3 | +import javax.swing.*; |
| 4 | +import java.awt.*; |
| 5 | +import java.awt.event.ActionListener; |
| 6 | + |
| 7 | +public class Form extends JPanel { |
| 8 | + |
| 9 | + private JTextField firstnameField; |
| 10 | + private JTextField lastNameField; |
| 11 | + |
| 12 | + private JButton addButton; |
| 13 | + private JButton viewButton; |
| 14 | + |
| 15 | + public Form() { |
| 16 | + |
| 17 | + JLabel firstnameLabel = new JLabel("First Name: "); |
| 18 | + JLabel lastnameLabel = new JLabel("Last Name: "); |
| 19 | + |
| 20 | + firstnameField = new JTextField(25); |
| 21 | + lastNameField = new JTextField(25); |
| 22 | + |
| 23 | + addButton = new JButton("Add User"); |
| 24 | + addButton.setPreferredSize(new Dimension(278, 40)); |
| 25 | + viewButton = new JButton("View All Users"); |
| 26 | + viewButton.setPreferredSize(new Dimension(278, 40)); |
| 27 | + |
| 28 | + // space between fields |
| 29 | + Insets fieldsInset = new Insets(0, 0, 10, 0); |
| 30 | + // space between buttons |
| 31 | + Insets buttonInset = new Insets(20,0,0,0); |
| 32 | + |
| 33 | + // uses Grid Bag Layout |
| 34 | + setLayout(new GridBagLayout()); |
| 35 | + GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
| 36 | + gridBagConstraints.insets = fieldsInset; |
| 37 | + gridBagConstraints.fill = GridBagConstraints.NONE; |
| 38 | + |
| 39 | + gridBagConstraints.gridx = 0; |
| 40 | + gridBagConstraints.gridy = 0; |
| 41 | + gridBagConstraints.anchor = GridBagConstraints.WEST; |
| 42 | + |
| 43 | + add(firstnameLabel, gridBagConstraints); |
| 44 | + |
| 45 | + gridBagConstraints.gridx = 0; |
| 46 | + gridBagConstraints.gridy = 1; |
| 47 | + |
| 48 | + add(firstnameField, gridBagConstraints); |
| 49 | + |
| 50 | + gridBagConstraints.gridx = 0; |
| 51 | + gridBagConstraints.gridy = 2; |
| 52 | + gridBagConstraints.anchor = GridBagConstraints.WEST; |
| 53 | + |
| 54 | + add(lastnameLabel, gridBagConstraints); |
| 55 | + |
| 56 | + gridBagConstraints.gridx = 0; |
| 57 | + gridBagConstraints.gridy = 3; |
| 58 | + |
| 59 | + add(lastNameField, gridBagConstraints); |
| 60 | + |
| 61 | + gridBagConstraints.gridx = 0; |
| 62 | + gridBagConstraints.gridy = 4; |
| 63 | + gridBagConstraints.insets = buttonInset; |
| 64 | + |
| 65 | + add(addButton, gridBagConstraints); |
| 66 | + |
| 67 | + gridBagConstraints.gridx = 0; |
| 68 | + gridBagConstraints.gridy = 5; |
| 69 | + gridBagConstraints.insets = buttonInset; |
| 70 | + |
| 71 | + add(viewButton, gridBagConstraints); |
| 72 | + } |
| 73 | + |
| 74 | + // getters |
| 75 | + public String getFirstname() { |
| 76 | + return firstnameField.getText(); |
| 77 | + } |
| 78 | + |
| 79 | + public String getLastname() { |
| 80 | + return lastNameField.getText(); |
| 81 | + } |
| 82 | + |
| 83 | + public void submitUsers(ActionListener actionListener) { |
| 84 | + addButton.addActionListener(actionListener); |
| 85 | + } |
| 86 | + |
| 87 | + public void viewUsers(ActionListener actionListener) { |
| 88 | + viewButton.addActionListener(actionListener); |
| 89 | + } |
| 90 | + |
| 91 | + // reset fields |
| 92 | + public void reset(boolean bln) { |
| 93 | + if(bln) { |
| 94 | + firstnameField.setText(""); |
| 95 | + lastNameField.setText(""); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments