Olá pessoal!
Tudo certo??
Pessoal, eu gostaria de fazer o seguinte:
Quero que a minha classe que possui a interface não trate os eventos dos botões. Quero que estes sejam tratados em outra classe. O problema é que eu estou fazendo em Netbeans e o sistema é Desktop. Se eu criar um botão na interface e dar duplo clique para programar o que será feito caso este seja apertado, o código ficará no mesmo arquivo que está a minha interface.
O meu professor resolveu isso pra mim de duas maneiras diferente, a 1ª é menos "elegante" a 2ª é mais elegante só que não estou conseguindo fazer aqui!!
Bom, vou enviar o código da 1ª forma:
Pacote testecontroller
Arquivo Tela.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Tela.java
*
* Created on 10/03/2010, 17:42:23
*/
package testecontroller;
/**
*
* @author 58407
*/
public class Tela extends javax.swing.JFrame {
/** Creates new form Tela */
public Tela() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("OK");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(150, 150, 150)
.addComponent(jButton1)
.addContainerGap(201, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(132, 132, 132)
.addComponent(jButton1)
.addContainerGap(145, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
}//GEN-LAST:event_jButton1ActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
Tela t = new Tela();
t.setVisible(true);
t.setControlador();
}
public void setControlador() {
jButton1.addActionListener(new Controller());
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
// End of variables declaration//GEN-END:variables
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testecontroller;
import java.awt.event.ActionListener;
/**
*
* @author 58407
*/
public class Controller implements ActionListener {
public void actionPerformed(java.awt.event.ActionEvent evt) {
System.out.print("OK");
}
}
Essa é a primeira forma, a segunda forma era algo mais ou menos assim: No botão na inteface em suas propriedades tem uma propriedade chamada "action" ali vai em "código personalizado" e coloca no textField new Controller()
Algo assim, bom, não lembro direito o que faço depois... Enfim, gostaria de saber se alguém sabe alguma forma de resolver isso desta forma, ou de alguma outra forma.
Muito obrigada pela ajuda!
Dina

