简体   繁体   中英

Constructor trouble shooting

Here is my project:

package myProjects;

import java.awt.GridBagConstraints;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.*;

public class GeometryEquationSolver extends JFrame{

JPanel mainPanel;
JTextField eq1Text, eq2Text;
JButton enterButton, clearTextButton;

public static void main(String[] args) {
    new GeometryEquationSolver();
}
public GeometryEquationSolver(){
    this.setSize(340, 140);
    this.setTitle("Equation Solver");
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);

    mainPanel = new JPanel();
    mainPanel.setLayout(new GridBagLayout());

    addItem(new JLabel("Equation 1"), 0, 0, 1, 1);
    addItem(new JLabel("Equation 2"), 2, 0, 1, 1);

    eq1Text = new JTextField(10);
    addItem(eq1Text, 0, 1, 1, 1);
    eq2Text = new JTextField(10);
    addItem(eq2Text, 2, 1, 1, 1);

    addItem(new JLabel("="), 1, 1, 1, 1); // Just the "=" for looks.

    enterButton = new JButton("Enter");
    enterButton.addActionListener(e->{
        Equation eq1 = new Equation(eq1Text.getText());
    });
    addItem(enterButton, 0, 2, 1, 1);

    clearTextButton = new JButton("Clear");
    clearTextButton.addActionListener(e->{
        eq1Text.setText(null);
        eq2Text.setText(null);
    });
    addItem(clearTextButton, 2, 2, 1, 1);

    this.add(mainPanel);
    this.setVisible(true);
}
private String[] rawData;
private String[] splitEq(String eq){
    rawData = eq.split("");
    return rawData;
}
private void addItem(JComponent c, int x, int y, int width, int height){
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = x;
    gbc.gridy = y;
    gbc.gridwidth = width;
    gbc.gridheight = height;
    gbc.weightx = 100.0;
    gbc.weighty = 100.0;
    gbc.fill = GridBagConstraints.NONE;

    mainPanel.add(c, gbc);
}
}
class Equation{

public enum opperation {add, sub, mult, div};
public opperation opper;
public String eq;

public Equation(String eq){
    this.eq = eq;
    opper = opperation.add;
    this.Equation(this.eq, opper);
}
 public Equation(String eq, opperation opper){

    }
}

My problem occurs in the class Equation. In the first constructor I have to following line:

this.Equation(this.eq, opper);

And I get this error:

The method Equation(String, Equation.opperation) is undefined for the type Equation

I'm trying to call the constructor after the first one. Does anyone know how to fix this?

The correct syntax would be this(this.eq, opper); . However, as this line must be the first line in your constructor's code, it would become this(eq, opperation.add); .

So your constructor would finally look like this:

public Equation(String eq){
    this(eq, opperation.add);
    this.eq = eq;
    opper = opperation.add;
    //this.Equation(this.eq, opper);
}

If it's absolutely necessary to have some code executed before calling the other constructor, perhaps this answer can help.

If you would like to call a constructor from a constructor, pls refer https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

Here is the code that can work.

public class Equation {

    public static enum opperation {add, sub, mult, div};
    public opperation opper;
    public String eq;

    public Equation(String eq){
        this(eq, opperation.add);
        this.eq = eq;
        opper = opperation.add;
    }
    public Equation(String eq, opperation opper){

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM