1
Giving continuity to a project of the college that was stopped, I intend to do it in Java already I did and I remade from scratch about 2 times however, it was horrible and to each class I realized that I could do better, so I decided to try to organize the code using MVC.
My question is, why is this code not working? It’s been about three days that I’m reading the stackoverflow and so far I haven’t found the problem.
I’m using this tutorial as a basis: http://www.gqferreira.com.br/artigos/ver/mvc-com-java-desktop-parte3
Model
package duvidamvc;
import javax.swing.JOptionPane;
public class DuvidaModel {
// Construtor
public DuvidaModel() {
iniciar();
}
public void iniciar() {
JOptionPane.showMessageDialog(null, "Funcionou.");
}
} //aqui tinha uma chave sobrando
Controller
package duvidamvc;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DuvidaController implements ActionListener {
private DuvidaView view;
private DuvidaModel modelo;
// Construtor
public DuvidaController(DuvidaView view) {
this.view = view;
this.view.getBtUm().addActionListener(this);
this.view.getBtDois().addActionListener(this);
this.view.getBtTres().addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == this.view.getBtUm()) {
chamandoModel();
}
if (e.getSource() == this.view.getBtDois()) {
chamandoModel();
}
if (e.getSource() == this.view.getBtTres()) {
chamandoModel();
}
}
private void chamandoModel() {
modelo = new DuvidaModel();
modelo.iniciar();
}
}
View
package duvidamvc;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DuvidaView extends JFrame {
// Encapsulamento
private JFrame tela;
private JPanel painel;
private JButton btUm;
private JButton btDois;
private JButton btTres;
// Construtor
public DuvidaView() {
janela();
}
public JButton getBtUm() {
return btUm;
}
public JButton getBtDois() {
return btDois;
}
public JButton getBtTres() {
return btTres;
}
public void janela() {
JFrame tela = new JFrame();
painel = new JPanel();
// painel.setLayout(null);
tela.setTitle("Duvida padrão MVC");
tela.setDefaultCloseOperation(EXIT_ON_CLOSE);
tela.add(painel);
tela.setSize(400, 80);
btUm = new JButton("Exemplo 1");
painel.add(btUm);
btDois = new JButton("Exemplo 2");
painel.add(btDois);
btTres = new JButton("Exemplo 3");
painel.add(btTres);
tela.setVisible(true);
pack();
}
public static void main(String[] args) {
new DuvidaView();
}
}
Place four spaces before each line of code to format it as code. Or select your code and click the button with the {}.
– Victor Stafusa
James, what is the
error
that is being displayed in yourIDE
?– user148754
The IDE doesn’t make any mistakes at all, that’s the problem
– James