当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
无意中发现的好玩代码:摇动的Java对话框
发布时间:2010/5/28 11:18:04 来源:城市学习网 编辑:ziteng
  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; [NextPage]   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();
  }
  }
  编辑特别推荐:
  Java中的输入/输出(I/O)
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved