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.
julianolandim
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 */publicclasstrabalho02extendsjavax.swing.JFrame{/** Creates new form trabalho02 */publictrabalho02(){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">privatevoidinitComponents(){botao=newjavax.swing.JButton();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);botao.setText("jButton1");botao.addMouseListener(newjava.awt.event.MouseAdapter(){publicvoidmousePressed(java.awt.event.MouseEventevt){botaoMousePressed(evt);}publicvoidmouseClicked(java.awt.event.MouseEventevt){botaoMouseClicked(evt);}publicvoidmouseEntered(java.awt.event.MouseEventevt){botaoMouseEntered(evt);}});org.jdesktop.layout.GroupLayoutlayout=neworg.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>privatevoidbotaoMouseEntered(java.awt.event.MouseEventevt){}privatevoidbotaoMousePressed(java.awt.event.MouseEventevt){System.out.println("botao precionado");botao.setBackground(java.awt.Color.red);}privatevoidbotaoMouseClicked(java.awt.event.MouseEventevt){System.out.println("botao solto");botao.setBackground(java.awt.Color.blue);}/** * @param args the command line arguments */publicstaticvoidmain(Stringargs[]){/* 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.LookAndFeelInfoinfo:javax.swing.UIManager.getInstalledLookAndFeels()){if("Nimbus".equals(info.getName())){javax.swing.UIManager.setLookAndFeel(info.getClassName());break;}}}catch(ClassNotFoundExceptionex){java.util.logging.Logger.getLogger(trabalho02.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);}catch(InstantiationExceptionex){java.util.logging.Logger.getLogger(trabalho02.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);}catch(IllegalAccessExceptionex){java.util.logging.Logger.getLogger(trabalho02.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);}catch(javax.swing.UnsupportedLookAndFeelExceptionex){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(newRunnable(){publicvoidrun(){newtrabalho02().setVisible(true);}});}// Variables declaration - do not modifyprivatejavax.swing.JButtonbotao;// End of variables declaration}
Espero ter ajudado
:thumbup:
F
Fabio010
Já percebi o seu exemplo
não sabia que o mouseListener podia funcionar com os botões!.
muito obrigado pela ajuda!
julianolandim
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: