** Hello good day, in classes we had to see the MVC design pattern to implement it in a future project.
Testing how to separate the code from the views and that I made one using JFrame -> 2 botones + JdesktopPane
; and each button opens a JInternalFrame
, up there all right, the problem I have is when I want to hear an event within those JInternalFrame
, I do not know how to "listen" to them since I put a button inside each JInternalFram
e to that at the time of pressing them I return in console a sout
to know that it was pressed.
Variable names:
Button "Window 1" = btnVentana1
"Window 2" button = btnVentana2
JDesktopPane = dpDesktop
"Click me!" button = btnVentana1Frame
Button "Click me 2!" = btnVentana2Frame
Try with:
if (e.getSource() == v1f.btnVentana1Frame) {
System.out.println("Botón click me presionado");
}
expecting it to work like it did to open the JInternalFrames but it does not dial anything.
PrincipalController.java
package controller;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import view.PrincipalView;
import view.Ventana1Frame;
import view.Ventana2Frame;
public class PrincipalController implements ActionListener{
private PrincipalView pv;
private Ventana1Frame v1f;
private Ventana2Frame v2f;
public PrincipalController(PrincipalView pv, Ventana1Frame v1f, Ventana2Frame v2f) {
this.pv = pv;
this.v1f = v1f;
this.v2f = v2f;
this.pv.btnVentana1.addActionListener(this);
this.pv.btnVentana2.addActionListener(this);
this.v1f.btnVentana1Frame.addActionListener(this);
this.v2f.btnVentana2Frame.addActionListener(this);
}
public void iniciar(){
pv.setTitle("Prueba");
pv.setLocationRelativeTo(null);
pv.setVisible(true);
v1f = null;
v2f = null;
}
@Override
public void actionPerformed(ActionEvent e) {
//Abrir Internal Frame 1
if (e.getSource() == pv.btnVentana1) {
if (v1f == null) {
System.out.println("Ventana 1");
v1f = new Ventana1Frame();
pv.dpEscritorio.add(v1f);
v1f.setVisible(true);
} else {
pv.dpEscritorio.getDesktopManager().activateFrame(v1f);
}
}
//Abrir Internal Frame 2
if (e.getSource() == pv.btnVentana2) {
if (v2f == null) {
System.out.println("Ventana 2");
v2f = new Ventana2Frame();
pv.dpEscritorio.add(v2f);
v2f.setVisible(true);
} else {
pv.dpEscritorio.getDesktopManager().activateFrame(v2f);
}
}
}
}
Main.java
package mvc;
import controller.PrincipalController;
import view.PrincipalView;
import view.Ventana1Frame;
import view.Ventana2Frame;
public class Main {
public static void main(String[] args) {
PrincipalView pv = new PrincipalView();
Ventana1Frame v1f = new Ventana1Frame();
Ventana2Frame v2f = new Ventana2Frame();
PrincipalController pc = new PrincipalController(pv, v1f, v2f);
pc.iniciar();
}
}