Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import java.awt.Insets;
- import java.awt.event.ActionEvent;
- import javax.swing.*;
- @SuppressWarnings("serial")
- public class QuadEq2 extends JPanel {
- private static final String TITLE_TEXT = "Enter coefficients of the equation:";
- private static final int COL_COUNT = 10;
- private static final int GAP = 4;
- private static final Insets INSETS = new Insets(GAP, GAP, GAP, GAP);
- private JTextField aCoefTextField = new JTextField(COL_COUNT);
- private JTextField bCoefTextField = new JTextField(COL_COUNT);
- private JTextField cCoefTextField = new JTextField(COL_COUNT);
- private JTextField solutnTextField = new JTextField(COL_COUNT);
- private JButton jbtnSolve = new JButton(new QuadSolutionAction("Solve Equation"));
- public QuadEq2() {
- // make the solutnTextField non-editable
- solutnTextField.setFocusable(false);
- solutnTextField.setEditable(false);
- setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
- setLayout(new GridBagLayout());
- // constraints for the title
- GridBagConstraints gbc = new GridBagConstraints();
- gbc.gridx = 0;
- gbc.gridy = 0;
- gbc.gridwidth = 2;
- gbc.fill = GridBagConstraints.BOTH;
- gbc.weightx = 1.0;
- gbc.weighty = 1.0;
- gbc.anchor = GridBagConstraints.CENTER;
- gbc.insets = INSETS;
- JLabel titleLabel = new JLabel(TITLE_TEXT, SwingConstants.CENTER);
- add(titleLabel, gbc);
- // add coefficient JTextFields with JLabel to
- // each row in the grid
- addCoeffField(1, "A", aCoefTextField);
- addCoeffField(2, "B", bCoefTextField);
- addCoeffField(3, "C", cCoefTextField);
- // add solution button and text field to the last row
- addTwoComps(4, jbtnSolve, solutnTextField);
- }
- private void addCoeffField(int row, String text, JTextField coefTextField) {
- JLabel label = new JLabel("Coefficient " + text + ":");
- addTwoComps(row, label, coefTextField);
- }
- private void addTwoComps(int row, JComponent comp1, JComponent comp2) {
- GridBagConstraints gbc = new GridBagConstraints();
- gbc.gridx = 0;
- gbc.gridy = row;
- gbc.fill = GridBagConstraints.BOTH;
- gbc.weightx = 1.0;
- gbc.weighty = 1.0;
- gbc.anchor = GridBagConstraints.WEST;
- Insets insets = INSETS;
- insets.right = 2 * GAP;
- gbc.insets = insets;
- add(comp1, gbc);
- gbc.gridx = 1;
- gbc.insets = INSETS;
- gbc.fill = GridBagConstraints.HORIZONTAL;
- gbc.anchor = GridBagConstraints.EAST;
- add(comp2, gbc);
- }
- private class QuadSolutionAction extends AbstractAction {
- public QuadSolutionAction(String name) {
- super(name);
- // give the button a mnemonic alt-key
- int mnemonic = (int) name.charAt(0);
- putValue(MNEMONIC_KEY, mnemonic);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO put code to do calculation here
- // output to the solutnTextField
- }
- }
- private static void createAndShowGui() {
- QuadEq2 mainPanel = new QuadEq2();
- JFrame frame = new JFrame("Solve Equation");
- frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- frame.getContentPane().add(mainPanel);
- frame.pack();
- frame.setLocationByPlatform(true);
- frame.setVisible(true);
- }
- public static void main(String[] args) {
- SwingUtilities.invokeLater(() -> createAndShowGui());
- }
- }
Add Comment
Please, Sign In to add comment