Publicado por: Psico.Indie | Setembro 21, 2008

Alerta do MSN em java

Essa dica mostra como implementar um alerta igual aquele usado para chamar atenção no MSN.
Confira o código abaixo.

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.lang.Thread;

public class Main extends JFrame{

public Main() {

setVisible(true);
setTitle(“Alerta de Janela”);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 300);
setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
JButton vibrateButton;
vibrateButton = new JButton(“Alerta!”);
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(vibrateButton);
add(buttonPanel, BorderLayout.SOUTH);

vibrateButton.addActionListener( new ActionListener() {

public void actionPerformed(ActionEvent e) {
vibrar();
}
});

}

private void vibrar() {

final int originalX = this.getLocationOnScreen().x;
final int originalY = this.getLocationOnScreen().y;

try{

for(int i = 0; i < 20; i++) {
Thread.sleep(10);
setLocation(this.getLocationOnScreen().x, this.getLocationOnScreen().y + 10);
Thread.sleep(10);
setLocation(this.getLocationOnScreen().x, this.getLocationOnScreen().y – 10);
Thread.sleep(10);
setLocation(this.getLocationOnScreen().x – 10, this.getLocationOnScreen().y);
Thread.sleep(10);
this.setLocation(originalX, originalY);
}

} catch (Exception error) {
error.printStackTrace();
}

}

public static void main (String args []) {

Main m = new Main();

}
}


Deixe uma resposta

Sua resposta:

Categorias