Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Dimension;
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import javax.swing.BorderFactory;
- import javax.swing.JButton;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.border.Border;
- public class FormPanel extends JPanel{
- private JLabel nameLabel;
- private JLabel occupationLabel;
- private JTextField nameField;
- private JTextField occupationField;
- private JButton okBtn;
- // Constructor
- FormPanel(){
- Dimension dim = getPreferredSize();
- dim.width = 250;
- setPreferredSize(dim);
- // Initialize
- nameLabel = new JLabel("Name: ");
- occupationLabel = new JLabel("Occupation: ");
- nameField = new JTextField(10);
- occupationField = new JTextField(10);
- okBtn = new JButton("OK");
- // Create borders
- Border innerBorder = BorderFactory.createTitledBorder("Add Person");
- Border outerBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
- setBorder(BorderFactory.createCompoundBorder(outerBorder, innerBorder));
- // Create a GridBagLayout
- setLayout(new GridBagLayout());
- GridBagConstraints gc = new GridBagConstraints();
- // 1. Create title "name" in left (gridx, gridy: like matrix' elements)
- gc.gridx = 0;
- gc.gridy = 0;
- gc.weightx = 1;
- gc.weighty = 1;
- gc.fill = GridBagConstraints.NONE;
- gc.anchor = GridBagConstraints.LINE_END;
- add(nameLabel, gc);
- // 2. Create a field (right to the "name") to write on
- gc.gridx = 1;
- gc.gridy = 0;
- gc.anchor = GridBagConstraints.LINE_START;
- add(nameField, gc);
- // 3. Create a title "occupation" in left and under the "name"
- gc.gridx = 0;
- gc.gridy = 1;
- gc.anchor = GridBagConstraints.LINE_END;
- add(occupationLabel, gc);
- // 4. Create a field (under the "nameField" and right to the "occupation") as the 4th element of my matrix
- gc.gridx = 1;
- gc.gridy = 1;
- gc.anchor = GridBagConstraints.LINE_START;
- add(occupationField, gc);
- // 5. Add the okBtn
- gc.gridy = 2; // Katevainw ston y 2 theseis
- gc.gridx = 1;
- gc.anchor = GridBagConstraints.LINE_START;// Proxwraw ston x 1 thesi
- add(okBtn, gc);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement