Advertisement
makispaiktis

FormPanel (Swing)

Feb 19th, 2019 (edited)
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.15 KB | None | 0 0
  1. import java.awt.Dimension;
  2. import java.awt.GridBagConstraints;
  3. import java.awt.GridBagLayout;
  4.  
  5. import javax.swing.BorderFactory;
  6. import javax.swing.JButton;
  7. import javax.swing.JLabel;
  8. import javax.swing.JPanel;
  9. import javax.swing.JTextField;
  10. import javax.swing.border.Border;
  11.  
  12. public class FormPanel extends JPanel{
  13.  
  14.     private JLabel nameLabel;
  15.     private JLabel occupationLabel;
  16.     private JTextField nameField;
  17.     private JTextField occupationField;
  18.     private JButton okBtn;
  19.    
  20.     // Constructor
  21.     FormPanel(){
  22.         Dimension dim = getPreferredSize();
  23.         dim.width = 250;
  24.         setPreferredSize(dim);
  25.        
  26.         // Initialize
  27.         nameLabel = new JLabel("Name: ");
  28.         occupationLabel = new JLabel("Occupation: ");
  29.         nameField = new JTextField(10);
  30.         occupationField = new JTextField(10);
  31.         okBtn = new JButton("OK");
  32.        
  33.        
  34.         // Create borders
  35.         Border innerBorder = BorderFactory.createTitledBorder("Add Person");
  36.         Border outerBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
  37.         setBorder(BorderFactory.createCompoundBorder(outerBorder, innerBorder));
  38.        
  39.         // Create a GridBagLayout
  40.         setLayout(new GridBagLayout());
  41.         GridBagConstraints gc = new GridBagConstraints();
  42.        
  43.         // 1. Create title "name" in left (gridx, gridy: like matrix' elements)
  44.         gc.gridx = 0;
  45.         gc.gridy = 0;
  46.         gc.weightx = 1;
  47.         gc.weighty = 1;
  48.         gc.fill = GridBagConstraints.NONE;
  49.         gc.anchor = GridBagConstraints.LINE_END;
  50.         add(nameLabel, gc);
  51.        
  52.         // 2. Create a field (right to the "name") to write on
  53.         gc.gridx = 1;
  54.         gc.gridy = 0;
  55.         gc.anchor = GridBagConstraints.LINE_START;
  56.         add(nameField, gc);
  57.        
  58.         // 3. Create a title "occupation" in left and under the "name"
  59.         gc.gridx = 0;
  60.         gc.gridy = 1;
  61.         gc.anchor = GridBagConstraints.LINE_END;
  62.         add(occupationLabel, gc);
  63.        
  64.         // 4. Create a field (under the "nameField" and right to the "occupation") as the 4th element of my matrix
  65.         gc.gridx = 1;
  66.         gc.gridy = 1;
  67.         gc.anchor = GridBagConstraints.LINE_START;
  68.         add(occupationField, gc);
  69.        
  70.         // 5. Add the okBtn
  71.         gc.gridy = 2;               // Katevainw ston y 2 theseis
  72.         gc.gridx = 1;
  73.         gc.anchor = GridBagConstraints.LINE_START;// Proxwraw ston x 1 thesi
  74.         add(okBtn, gc);
  75.        
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement