entanglement 11 de nov. de 2010
Veja se isso pisca do jeito que você quer.
package guj ;
import java.awt.Color ;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListener ;
import javax.swing.Icon ;
import javax.swing.JLabel ;
public class BlinkingLabel extends JLabel {
private void initialize () {
hide = false ;
count = 0 ;
timer = new javax . swing . Timer ( 100 , new ActionListener () {
@Override
public void actionPerformed ( ActionEvent e ) {
if ( hide ) {
foregroundColor = getForeground ();
setForeground ( getBackground ());
} else {
setForeground ( foregroundColor );
}
hide = ! hide ;
count ++ ;
if ( count == times * 2 + 1 ) {
timer . stop ();
count = 0 ;
}
}
});
}
public BlinkingLabel () {
super ();
initialize ();
}
public BlinkingLabel ( String text ) {
super ( text );
initialize ();
}
public BlinkingLabel ( Icon image ) {
super ( image );
initialize ();
}
public BlinkingLabel ( String text , int horizontalAlignment ) {
super ( text , horizontalAlignment );
initialize ();
}
public BlinkingLabel ( Icon image , int horizontalAlignment ) {
super ( image , horizontalAlignment );
initialize ();
}
public BlinkingLabel ( String text , Icon icon , int horizontalAlignment ) {
super ( text , icon , horizontalAlignment );
initialize ();
}
public void blink ( int times ) {
hide = false ;
count = 0 ;
this . times = times ;
timer . start ();
}
private javax . swing . Timer timer ;
private boolean hide ;
private int count ;
private int times ;
private Color foregroundColor ;
}
package guj ;
import javax.swing.SwingUtilities ;
import java.awt.BorderLayout ;
import javax.swing.JPanel ;
import javax.swing.JFrame ;
import javax.swing.JLabel ;
import javax.swing.JButton ;
import java.awt.GridBagLayout ;
import java.awt.GridBagConstraints ;
import java.awt.FlowLayout ;
public class ExemploPiscar extends JFrame {
private static final long serialVersionUID = 1L ;
private JPanel jContentPane = null ;
private BlinkingLabel blkTeste = null ;
private JPanel pnlBotoes = null ;
private JButton btnTestar = null ;
public BlinkingLabel getBlkTeste () {
if ( blkTeste == null ) {
blkTeste = new BlinkingLabel ();
blkTeste . setText ( "Exemplo" );
}
return blkTeste ;
}
/**
* This method initializes pnlBotoes
*
* @return javax.swing.JPanel
*/
private JPanel getPnlBotoes () {
if ( pnlBotoes == null ) {
pnlBotoes = new JPanel ();
pnlBotoes . setLayout ( new FlowLayout ());
pnlBotoes . add ( getBtnTestar (), null );
}
return pnlBotoes ;
}
/**
* This method initializes btnTestar
*
* @return javax.swing.JButton
*/
private JButton getBtnTestar () {
if ( btnTestar == null ) {
btnTestar = new JButton ();
btnTestar . setText ( "Testar" );
btnTestar . addActionListener ( new java . awt . event . ActionListener () {
public void actionPerformed ( java . awt . event . ActionEvent e ) {
blkTeste . setText ( String . format ( "Teste %d" , i ++ ));
blkTeste . blink ( 3 );
}
private int i = 0 ;
});
}
return btnTestar ;
}
/**
* @param args
*/
public static void main ( String [] args ) {
// TODO Auto-generated method stub
SwingUtilities . invokeLater ( new Runnable () {
public void run () {
ExemploPiscar thisClass = new ExemploPiscar ();
thisClass . setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE );
thisClass . setVisible ( true );
}
});
}
/**
* This is the default constructor
*/
public ExemploPiscar () {
super ();
initialize ();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize () {
this . setSize ( 300 , 200 );
this . setContentPane ( getJContentPane ());
this . setTitle ( "Exemplo Piscar" );
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane () {
if ( jContentPane == null ) {
jContentPane = new JPanel ();
jContentPane . setLayout ( new BorderLayout ());
jContentPane . add ( getBlkTeste (), BorderLayout . NORTH );
jContentPane . add ( getPnlBotoes (), BorderLayout . SOUTH );
}
return jContentPane ;
}
}
Sucrylhus 11 de nov. de 2010
Funcionou perfeitamente…
Mas eu não consigo aplicar isso no meu projeto, acho que é um pouco de mais pro meu parco conhecimento hehehe.
Se você puder me explicar um pouco sobre o código, eu não consegui entender muito bem algumas partes, tais como:
public BlinkingLabel getBlkTeste() {
if (blkTeste == null) {
blkTeste = new BlinkingLabel();
blkTeste.setText("Exemplo");
}
return blkTeste;
}
public BlinkingLabel () {
super ();
initialize ();
}
public BlinkingLabel ( String text ) {
super ( text );
initialize ();
}
public BlinkingLabel ( Icon image ) {
super ( image );
initialize ();
}
public BlinkingLabel ( String text , int horizontalAlignment ) {
super ( text , horizontalAlignment );
initialize ();
}
public BlinkingLabel ( Icon image , int horizontalAlignment ) {
super ( image , horizontalAlignment );
initialize ();
}
public BlinkingLabel ( String text , Icon icon , int horizontalAlignment ) {
super ( text , icon , horizontalAlignment );
initialize ();
}
public void blink ( int times ) {
hide = false ;
count = 0 ;
this . times = times ;
timer . start ();
}
private javax . swing . Timer timer ;
private boolean hide ;
private int count ;
private int times ;
private Color foregroundColor ;
}
Ficaria agradecido se você pudesse me explicar isso melhor para que eu pudesse entender e aplicar às minhas necessidades.
Grato.