0

I am playing with Swing. Following is my first try with Swing, a simple GUI for Quadratic Equations Solver. The program worked fine when I use FlowLayout but the visualization was not so nice when I resized the frame. So I wanted to put all the labels and texfields in myFrame in centered column form and decided to try the BoxLayout. But when I chose BoxLayout to have a better display then the code could not be compiled and Eclipse threw a lot of warnings at me.

I am aware that there are some questions related to the problem I have here. And the common reason in those questions was that the first argument of the constructor of BoxLayout (myFrame in this case) had not been initialized when It was passed to the constructor.

It seems to me that I did not make that mistake in my case since myFrame was created before I invoke the constructor of BoxLayout. So could you please kindly point out to me what wrong with the code?

Here is the code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Math;

class QuadraticEqSolver implements ActionListener {

    JTextField aCoef, bCoef, cCoef;
    JButton jbtnSolve; 
    JLabel jlabPrompt, jlabA, jlabB, jlabC, jlabEquation, jlabSolution;

    QuadraticEqSolver() {

        // Create a new JFrame container. 
        JFrame myFrame = new JFrame("Solve Quadratic Equations"); 

        // Specify BoxLayout for the layout manager. 
        myFrame.setLayout(new BoxLayout(myFrame, BoxLayout.PAGE_AXIS)); 

        // Give the frame an initial size. 
        myFrame.setSize(240, 120); 

        // Terminate the program when the user closes the application. 
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create text fields for entering coefficients 
        aCoef = new JTextField(10); 
        bCoef = new JTextField(10);
        cCoef = new JTextField(10);

        // Set the action commands for the text field. 
        aCoef.setActionCommand("aCoefficients"); 
        bCoef.setActionCommand("bCoefficients"); 
        cCoef.setActionCommand("cCoefficients"); 

        // Create the Solve button. 
        JButton jbtnSolve = new JButton("Solve the Eq."); 

        // Add action listeners. 
        jbtnSolve.addActionListener(this); 

        // Create the labels. 
        jlabPrompt = new JLabel("Enter coefficients of the equation: "); 
        jlabA = new JLabel("Enter a:");
        jlabB = new JLabel("Enter b:");
        jlabC = new JLabel("Enter c:");
        jlabEquation = new JLabel(""); 
        jlabSolution = new JLabel("");

        // Add the components to the content pane. 
        myFrame.add(jlabPrompt); 
        myFrame.add(jlabA);
        myFrame.add(aCoef);
        myFrame.add(jlabB);
        myFrame.add(bCoef);
        myFrame.add(jlabC);
        myFrame.add(cCoef);

        myFrame.add(jbtnSolve);  
        myFrame.add(jlabEquation);
        myFrame.add(jlabSolution);

        // Display the frame. 
        myFrame.setVisible(true); 

   }

    public void actionPerformed(ActionEvent ae){
        if(ae.getActionCommand().equals("Solve the Eq.")){
            //The Solve the Eq. button was pressed.
            double a = Double.parseDouble(aCoef.getText());
            double b = Double.parseDouble(bCoef.getText());
            double c = Double.parseDouble(cCoef.getText());

            double Delta = Math.pow(b,2) - 4*a*c;
            jlabEquation.setText("Your equation is: " + a + "x^2 + " + b + "x + " + c + " = 0.");

            if(Delta < 0){
                jlabSolution.setText("Your equation does not have real solution!");
            } else if(Delta == 0) {
                double sol = ( -b + Math.sqrt(Delta) )/ ( 2*a );
                jlabSolution.setText("Your equation has one solution x = " + sol);
            } else {
                double sol1 = ( -b - Math.sqrt(Delta) )/ ( 2*a );
                double sol2 = ( -b + Math.sqrt(Delta) )/ ( 2*a );
                jlabSolution.setText("Your equation has two solutions x1 = " + sol1 + "; x2 = " + sol2);
            }
        } else {

        }
    }

    public static void main(String args[]){
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new QuadraticEqSolver();
            }
        });
    }
}

and there are the warnings

Exception in thread "AWT-EventQueue-0" java.awt.AWTError: BoxLayout can't be shared
    at javax.swing.BoxLayout.checkContainer(Unknown Source)
    at javax.swing.BoxLayout.invalidateLayout(Unknown Source)
    at javax.swing.BoxLayout.addLayoutComponent(Unknown Source)
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at javax.swing.JFrame.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at QuadraticEqSolver.<init>(QuadraticEqSolver.java:54)
    at QuadraticEqSolver$1.run(QuadraticEqSolver.java:99)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Kratos1808
  • 43
  • 6

0 Answers0