Eu gostava de saber se existe algum tipo de método ou evento em que é possível saber se o botão está sendo pressionado.
Procurei no google e só encontrei o mouse pressed.
Eu gostava mesmo de carregar no botão e ele ficar com um fundo preto por exemplo (enquanto pressionado) , e quando largava o botão ele ficava com o seu fundo normal.
Ok então voçê está me a dizer que não existe nada para reconhecer que o botao está pressionado.
Eu gostava mesmo de quando o botão estivesse a ser pressionado ele muda-se de cor.
Tem sim Fabio,
Quando o botão estiver precionado o evento é MousePressed e quando soltar o evendo é MouseClicked.
Fiz um exemplo pra voce ver e ele funciona assim, MousePressed o botao fica vermelho e MouseCliked o botao fica azul.
De uma olhada pra ver se você consegue entender.
/**
*
* @author juliano
*/
public class trabalho02 extends javax.swing.JFrame {
/** Creates new form trabalho02 */
public trabalho02() {
initComponents();
botao.setBackground(java.awt.Color.blue);
}
/** 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">
private void initComponents() {
botao = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
botao.setText("jButton1");
botao.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
botaoMousePressed(evt);
}
public void mouseClicked(java.awt.event.MouseEvent evt) {
botaoMouseClicked(evt);
}
public void mouseEntered(java.awt.event.MouseEvent evt) {
botaoMouseEntered(evt);
}
});
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(botao)
.addContainerGap(286, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(40, 40, 40)
.add(botao)
.addContainerGap(231, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void botaoMouseEntered(java.awt.event.MouseEvent evt) {
}
private void botaoMousePressed(java.awt.event.MouseEvent evt) {
System.out.println("botao precionado");
botao.setBackground(java.awt.Color.red);
}
private void botaoMouseClicked(java.awt.event.MouseEvent evt) {
System.out.println("botao solto");
botao.setBackground(java.awt.Color.blue);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(trabalho02.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(trabalho02.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(trabalho02.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(trabalho02.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new trabalho02().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton botao;
// End of variables declaration
}
Espero ter ajudado
:thumbup:
Já percebi o seu exemplo
não sabia que o mouseListener podia funcionar com os botões!.
muito obrigado pela ajuda!
sim funciona sim, so não esqueça de usar também o MouseExited caso o usuário clique e sem soltar o botao do mouse arraste .
entao ficaria assim:
seuBotao.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
//fica vermelho
}
public void mouseClicked(java.awt.event.MouseEvent evt) {
//fica azul
}
public void mouseExited(java.awt.event.MouseEvent evt) {
//fica azul
}
});
Por favor informe o topico como [Resolvido] caso seja isso mesmo que você procurava. :thumbup:
Ok muito obrigado pela ajuda.
estamos por aqui se precisar. :thumbup: