返回列表 发帖

无意中发现的好玩代码——摇动的Java对话框

无意中发现的好玩代码——摇动的Java对话框 由Linux系统中文网(Linux521.com)编辑收集整理,除Linux521注明原创文章外,其版权归原作者所有。如果您在学习中遇到问题欢迎在下面的评论中留言,我们会尽全力解答您的问题。
  import java.awt.Point;

  import java.awt.event.ActionEvent;

  import java.awt.event.ActionListener;

  import javax.swing.JDialog;

  import javax.swing.JOptionPane;

  import javax.swing.Timer;

  public class Main {

  JDialog dialog;

  Point naturalLocation;

  Timer shakeTimer;

  public Main(JDialog d) {

  dialog = d;

  }

  public void startShake() {

  final long startTime;

  naturalLocation = dialog.getLocation();

  startTime = System.currentTimeMillis();

  shakeTimer = new Timer(5, new ActionListener() {

  public void actionPerformed(ActionEvent e) {

  double TWO_PI = Math.PI * 2.0;

  double SHAKE_CYCLE = 50;

  long elapsed = System.currentTimeMillis() - startTime;

  double waveOffset = (elapsed % SHAKE_CYCLE) / SHAKE_CYCLE;

  double angle = waveOffset * TWO_PI;

  int SHAKE_DISTANCE = 10;

  int shakenX = (int) ((Math.sin(angle) * SHAKE_DISTANCE) naturalLocation.x);

  dialog.setLocation(shakenX, naturalLocation.y);

  dialog.repaint();

  int SHAKE_DURATION = 1000;

  if (elapsed >= SHAKE_DURATION)

  stopShake();

  }

  });

  shakeTimer.start();

  }

  public void stopShake() {

  shakeTimer.stop();

  dialog.setLocation(naturalLocation);

  dialog.repaint();

  }

  public static void main(String[] args) {

  JOptionPane pane = new JOptionPane("your message",JOptionPane.ERROR_MESSAGE, JOptionPane.OK_OPTION);

  JDialog d = pane.createDialog(null, "title");

  Main dec = new Main(d);

  d.pack();

  d.setModal(false);

  d.setVisible(true);

  dec.startShake();

  }

  }

沙发
偶的
呵呵

TOP

谢了,顶~~~~~~~~~~~

TOP

返回列表