fubarable

GridBagLayout Example

Sep 5th, 2016
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.73 KB | None | 0 0
  1. import java.awt.GridBagConstraints;
  2. import java.awt.GridBagLayout;
  3. import java.awt.Insets;
  4. import java.awt.event.ActionEvent;
  5. import javax.swing.*;
  6.  
  7. @SuppressWarnings("serial")
  8. public class QuadEq2 extends JPanel {
  9.     private static final String TITLE_TEXT = "Enter coefficients of the equation:";
  10.     private static final int COL_COUNT = 10;
  11.     private static final int GAP = 4;
  12.     private static final Insets INSETS = new Insets(GAP, GAP, GAP, GAP);
  13.     private JTextField aCoefTextField = new JTextField(COL_COUNT);
  14.     private JTextField bCoefTextField = new JTextField(COL_COUNT);
  15.     private JTextField cCoefTextField = new JTextField(COL_COUNT);
  16.     private JTextField solutnTextField = new JTextField(COL_COUNT);
  17.     private JButton jbtnSolve = new JButton(new QuadSolutionAction("Solve Equation"));
  18.    
  19.     public QuadEq2() {
  20.         // make the solutnTextField non-editable
  21.         solutnTextField.setFocusable(false);
  22.         solutnTextField.setEditable(false);
  23.        
  24.         setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
  25.         setLayout(new GridBagLayout());
  26.        
  27.         // constraints for the title
  28.         GridBagConstraints gbc = new GridBagConstraints();
  29.         gbc.gridx = 0;
  30.         gbc.gridy = 0;
  31.         gbc.gridwidth = 2;
  32.         gbc.fill = GridBagConstraints.BOTH;
  33.         gbc.weightx = 1.0;
  34.         gbc.weighty = 1.0;
  35.         gbc.anchor = GridBagConstraints.CENTER;
  36.         gbc.insets = INSETS;
  37.         JLabel titleLabel = new JLabel(TITLE_TEXT, SwingConstants.CENTER);
  38.         add(titleLabel, gbc);
  39.        
  40.         // add coefficient JTextFields with JLabel to
  41.         // each row in the grid
  42.         addCoeffField(1, "A", aCoefTextField);
  43.         addCoeffField(2, "B", bCoefTextField);
  44.         addCoeffField(3, "C", cCoefTextField);
  45.        
  46.         // add solution button and text field to the last row
  47.         addTwoComps(4, jbtnSolve, solutnTextField);
  48.     }
  49.  
  50.     private void addCoeffField(int row, String text, JTextField coefTextField) {
  51.         JLabel label = new JLabel("Coefficient " + text + ":");
  52.         addTwoComps(row, label, coefTextField);
  53.     }
  54.  
  55.     private void addTwoComps(int row, JComponent comp1, JComponent comp2) {
  56.         GridBagConstraints gbc = new GridBagConstraints();
  57.         gbc.gridx = 0;
  58.         gbc.gridy = row;
  59.         gbc.fill = GridBagConstraints.BOTH;
  60.         gbc.weightx = 1.0;
  61.         gbc.weighty = 1.0;
  62.         gbc.anchor = GridBagConstraints.WEST;
  63.         Insets insets = INSETS;
  64.         insets.right = 2 * GAP;
  65.         gbc.insets = insets;
  66.         add(comp1, gbc);
  67.        
  68.         gbc.gridx = 1;
  69.         gbc.insets = INSETS;
  70.         gbc.fill = GridBagConstraints.HORIZONTAL;
  71.         gbc.anchor = GridBagConstraints.EAST;
  72.         add(comp2, gbc);
  73.     }
  74.    
  75.     private class QuadSolutionAction extends AbstractAction {
  76.         public QuadSolutionAction(String name) {
  77.             super(name);
  78.             // give the button a mnemonic alt-key
  79.             int mnemonic = (int) name.charAt(0);
  80.             putValue(MNEMONIC_KEY, mnemonic);
  81.         }
  82.  
  83.         @Override
  84.         public void actionPerformed(ActionEvent e) {
  85.             // TODO put code to do calculation here
  86.             // output to the solutnTextField
  87.         }
  88.     }
  89.  
  90.     private static void createAndShowGui() {
  91.         QuadEq2 mainPanel = new QuadEq2();
  92.  
  93.         JFrame frame = new JFrame("Solve Equation");
  94.         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  95.         frame.getContentPane().add(mainPanel);
  96.         frame.pack();
  97.         frame.setLocationByPlatform(true);
  98.         frame.setVisible(true);
  99.     }
  100.  
  101.     public static void main(String[] args) {
  102.         SwingUtilities.invokeLater(() -> createAndShowGui());
  103.     }
  104. }
Add Comment
Please, Sign In to add comment