The fact is that I am doing a calculator for my programming classes, nothing complicated but this is stuck in the end. I have a VentanaPrincipal class that is the graphical interface created with swing. And I have a PanelOperations class, where I'm doing the operations with the "Add" and "Subtract" buttons. In the book we used, I read that in order to use methods of the "world" class of the problem (Calculator with add and subtract methods) I had to create a relation of the class PanelOperations to the Main Window, and as this had a relation with the class world, you could use your methods without problem. This works for me but it says to modify the constructor of the PanelOperations class so it looks like this:
private VentanaPrincipal principal; //Esto está antes del constructor, solo lo pongo aquí para ahorrar espacio.
public PanelOperaciones( VentanaPrincipal v ) {
principal = v;
setLayout(new GridLayout(1, 2));
JButton btSumar = new JButton("Sumar");
JButton btRestar = new JButton("Restar");
btRestar.setActionCommand(RESTAR);
btRestar.addActionListener(this);
btSumar.setActionCommand(SUMAR);
btSumar.addActionListener(this);
add(btSumar);
add(btRestar);
}
In the book that I use, I have to leave the instance of the relationship like this: principal = v; The problem comes in that the Main Window class also has a relation with the class panelOperations. So, when the instancio asks me for the value "v" which is a reference to its same class, how can I solve this?