javax.swing.Timer类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(10.9k)|赞(0)|评价(0)|浏览(103)

本文整理了Java中javax.swing.Timer类的一些代码示例,展示了Timer类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Timer类的具体详情如下:
包路径:javax.swing.Timer
类名称:Timer

Timer介绍

暂无

代码示例

代码示例来源:origin: stackoverflow.com

private static final int RADIUS = 25;
private static final int FRAMES = 24;
private final Timer timer = new Timer(20, this);
private final Rectangle rect = new Rectangle();
private BufferedImage background;
  EventQueue.invokeLater(new Runnable() {
  JFrame f = new JFrame("AnimationTest");
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  f.add(this);
  f.pack();
  f.setLocationRelativeTo(null);
  f.setVisible(true);
  timer.start();
  this.setOpaque(false);
  this.setPreferredSize(new Dimension(WIDE, HIGH));
  this.addMouseListener(new MouseHandler());
  this.addComponentListener(new ComponentHandler());
  this.repaint();
    Dimension d = field.getPreferredSize();
    field.setBounds(e.getX(), e.getY(), d.width, d.height);
    add(field);

代码示例来源:origin: stackoverflow.com

JFrame f = new JFrame("MarqueeTest");
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  String s = "Tomorrow, and tomorrow, and tomorrow, "
  + "creeps in this petty pace from day to day, "
  + "sound and fury signifying nothing.";
  MarqueePanel mp = new MarqueePanel(s, 32);
  f.add(mp);
  f.pack();
  f.setLocationRelativeTo(null);
  f.setVisible(true);
  mp.start();
  EventQueue.invokeLater(new Runnable() {
private final Timer timer = new Timer(1000 / RATE, this);
private final JLabel label = new JLabel();
private final String s;
  label.setFont(new Font("Serif", Font.ITALIC, 36));
  label.setText(sb.toString());
  this.add(label);
  timer.start();
  timer.stop();

代码示例来源:origin: org.netbeans.api/org-openide-util

/** Impl of HierarchyListener, starts init job with delay when component shown,
 * stops listening to asociated component it isn't showing anymore,
 * calls cancel if desirable.
 * @param evt hierarchy event
 */
@Override
public void hierarchyChanged(HierarchyEvent evt) {
  final boolean hierachyChanged = (evt.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0;
  LOG.log(Level.FINE, "Hierarchy Changed {0}", hierachyChanged);
  if (hierachyChanged) {
    boolean isShowing = comp4Init.isShowing();
    if (timer == null && isShowing) {
      timer = new Timer(20, this);
      timer.setRepeats(false);
      timer.start();
      LOG.log(Level.FINE, "Timer started for {0}", comp4Init);
    } else if (!isShowing) {
      comp4Init.removeHierarchyListener(this);
      LOG.log(Level.FINE, "Not showing, cancling for {0}", comp4Init);
      cancel();
    }
  }
}

代码示例来源:origin: skylot/jadx

@Override
  public void setVisible(boolean aFlag) {
    super.setVisible(aFlag);
    if (aFlag) {
      timer.start();
    } else {
      timer.stop();
    }
  }
}

代码示例来源:origin: JetBrains/ideavim

public void startMappingTimer(@NotNull ActionListener actionListener) {
 final NumberOption timeoutLength = Options.getInstance().getNumberOption("timeoutlen");
 if (timeoutLength != null) {
  myMappingTimer.setInitialDelay(timeoutLength.value());
 }
 for (ActionListener listener : myMappingTimer.getActionListeners()) {
  myMappingTimer.removeActionListener(listener);
 }
 myMappingTimer.addActionListener(actionListener);
 myMappingTimer.start();
}

代码示例来源:origin: plantuml/plantuml

private void startTimer() {
  Log.info("Init done");
  final Timer timer = new Timer(period, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      tick();
    }
  });
  timer.setInitialDelay(0);
  timer.start();
  Log.info("Timer started");
}

代码示例来源:origin: stackoverflow.com

g.setColor(Color.blue);
g.fillRect(xPos, 400, 100, 100);
repaint();
Timer timer = new Timer(1000, new ActionListener() {
  private boolean state = false;
  @Override
    repaint();
timer.start();
EventQueue.invokeLater(new Runnable() {
  @Override
  public void run() {
    JFrame frame = new JFrame("Testing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new GameRunner());
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

代码示例来源:origin: stackoverflow.com

private final JPanel control = new JPanel();
private final List<CabPanel> fleet = new ArrayList<CabPanel>();
private final Timer timer = new Timer(200, null);
  fleet.add(new CabPanel("Cab #2", Hue.Magenta));
  fleet.add(new CabPanel("Cab #3", Hue.Yellow));
  control.setLayout(new GridLayout(0, 1));
  for (CabPanel cp : fleet) {
    control.add(cp);
    timer.addActionListener(cp.listener);
  this.add(map, BorderLayout.CENTER);
  timer.start();
    super(new GridLayout(1, 0));
    name.setText(s);
    this.setBackground(hue.getColor());
    this.setPreferredSize(new Dimension(32 * SIZE, 32 * SIZE));
  JFrame f = new JFrame("Dispatch");
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  EventQueue.invokeLater(new Runnable() {

代码示例来源:origin: stackoverflow.com

private RotatingIcon( Icon icon, final JComponent component ) {
 delegateIcon = icon;
 rotatingTimer = new Timer( 100, new ActionListener() {
  @Override
  public void actionPerformed( ActionEvent e ) {
    angleInDegrees = 0;
   component.repaint();
 rotatingTimer.setRepeats( false );
 rotatingTimer.start();
 rotatingTimer.stop();
 Graphics2D g2 = (Graphics2D )g.create();
 int cWidth = delegateIcon.getIconWidth() / 2;
 delegateIcon.paintIcon(c, g2, x, y);
 g2.setTransform(original);
 rotatingTimer.start();

代码示例来源:origin: stackoverflow.com

timer.restart();
  startStopButton.setText("Stop");
  isTimerRunning = true;
backgroundColour = Color.white;
foregroundColour = clut.peek();
timer = new Timer(10, timerAction);
drawingArea.addComponentListener(componentAdapter);
buttonPanel.setBorder(BorderFactory.createLineBorder(Color.darkGray, 5));
startStopButton.setOpaque(true);
startStopButton.setForeground(Color.white);
    if (!isTimerRunning) {
      startStopButton.setText("Stop");
      timer.start();
      isTimerRunning = true;
    } else if (isTimerRunning) {
      startStopButton.setText("Start");
      timer.stop();
      isTimerRunning = false;
buttonPanel.add(startStopButton);
    timer.stop();
    System.exit(0);

代码示例来源:origin: stackoverflow.com

lines = new ArrayList<>();
timer = new Timer(75, new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    if (rotate < -50) {
      ((Timer) e.getSource()).stop();
    } else {
      lines.add(new Line(x1, rotate));
      repaint();
      x1 += 5;
      rotate--;
start.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e) {
    timer.start();
add(start);
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
for (Line line : lines) {
  line.drawLine(g);
  int Radius = (int) (Math.min(getWidth(), getHeight()) * 0.4);

代码示例来源:origin: stackoverflow.com

private final Timer timer = new Timer(1000, this);
  this.setLayout(new GridLayout(N, N));
  BufferedImage bi = null;
  try {
  JFrame f = new JFrame();
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  f.add(this);
  f.pack();
  f.setVisible(true);
  timer.start();
  this.removeAll();
  for (JLabel label : list) add(label);
  this.validate();
  EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {

代码示例来源:origin: stackoverflow.com

private final Timer timer = new Timer(1000, this);
private final List<ButtonPanel> panels = new ArrayList<ButtonPanel>();
  this.setLayout(new GridLayout(N, N, N, N));
  for (int i = 0; i < N * N; i++) {
    ButtonPanel bp = new ButtonPanel(i);
    panels.add(bp);
    this.add(bp);
public void actionPerformed(ActionEvent e) {
  for (JPanel p : panels) {
    p.setBackground(new Color(rnd.nextInt()));
    this.setBackground(new Color(rnd.nextInt()));
    this.add(new JButton("Button " + String.valueOf(i)));
  EventQueue.invokeLater(new Runnable() {
      JFrame f = new JFrame("ButtonTest");
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      ButtonTest bt = new ButtonTest();
      f.add(bt);
      f.pack();
      f.setLocationRelativeTo(null);
      f.setVisible(true);
      bt.timer.start();

代码示例来源:origin: stackoverflow.com

final JFrame frame = new JFrame("table adjust example");
    frame.add(createUI());
    frame.pack();
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    javax.swing.Timer timer = new javax.swing.Timer(3000, new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
    timer.setRepeats(true);
    timer.setCoalesce(true);
    timer.start();
    Object valueAt = jTable.getValueAt(row, column);
    Component tableCellRendererComponent = cellRenderer.getTableCellRendererComponent(jTable, valueAt, false, false, row, column);
    int heightPreferable = tableCellRendererComponent.getPreferredSize().height;
    maxHeight = Math.max(heightPreferable, maxHeight);
width = comp.getPreferredSize().width;
  comp = renderer.getTableCellRendererComponent(
      table, table.getValueAt(r, column), false, false, r, column);
  int currentWidth = comp.getPreferredSize().width;
  width = Math.max(width, currentWidth);

代码示例来源:origin: stackoverflow.com

private final Timer timer = new Timer(1000, this);
private JDialog dialog = new JDialog();
private final JOptionPane optPane = new JOptionPane();
  EventQueue.invokeLater(new Runnable() {
  JFrame frame = new JFrame("Title");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setLocationByPlatform(true);
  timer.setCoalesce(false);
  optPane.setMessage(message());
  optPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
  optPane.setOptionType(JOptionPane.DEFAULT_OPTION);
  optPane.addPropertyChangeListener(this);
  dialog.add(optPane);
  dialog.pack();
  frame.add(new JLabel(frame.getTitle(), JLabel.CENTER));
  frame.setVisible(true);
  dialog.setLocationRelativeTo(frame);
  dialog.setVisible(true);
  timer.start();
    thatsAllFolks();
  timer.restart();
  dialog.dispatchEvent(new WindowEvent(
    dialog, WindowEvent.WINDOW_CLOSING));

代码示例来源:origin: stackoverflow.com

rectangle = new Rectangle(50, 50, 250, 250);
lastTimeChanged = System.currentTimeMillis();
setBackground(Color.WHITE);
g.drawImage(buffImg, 0, 0, getWidth(), getHeight(), this);
rectangle.y = rectangle.y + dirY * 10;;
repaint();
  public void run() {
    final MyCanvas canvas = new MyCanvas();
    JFrame frame = new JFrame();
   frame.setSize(new Dimension(500, 500));
   frame.add(canvas);
   frame.setVisible(true);
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   Timer timer = new Timer(200, canvas);
   timer.start();
   new Thread()

代码示例来源:origin: stackoverflow.com

setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel contentPane = (JPanel) getContentPane();
contentPane.add(label, BorderLayout.CENTER);
contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
pack();
      timer.stop();
      label.setText("The time is up!");
timer = new Timer(delay, action);
timer.setInitialDelay(0);
timer.start();
setVisible(true);

代码示例来源:origin: stackoverflow.com

String cmd = e.getActionCommand();
    if (STOP.equals(cmd)) {
      timer.stop();
      run.setText(START);
    } else {
      timer.start();
      run.setText(STOP);
  public void actionPerformed(ActionEvent e) {
    if ("Fast".equals(combo.getSelectedItem())) {
      timer.setDelay(FAST);
    } else {
      timer.setDelay(SLOW);
btnPanel.add(run);
btnPanel.add(combo);
this.add(btnPanel, BorderLayout.SOUTH);
timer = new Timer(FAST, new ActionListener() {
timer.start();
EventQueue.invokeLater(new Runnable() {

代码示例来源:origin: magefree/mage

public void gainLifeDisplay() {
  if (faderGainLife == null && doGainFade) {
    doGainFade = false;
    faderGainLife = new Timer(50, new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        gainX++;
        int alpha = Math.max(250 - gainX, 200);
        setCenterColor(new Color(2 * gainX, 210, 255, alpha));
        repaint();
        if (gainX >= 100) {
          setCenterColor(new Color(200, 210, 0, 200));
          gainX = 100;
          if (faderGainLife != null) {
            faderGainLife.stop();
            faderGainLife.setRepeats(false);
            faderGainLife.setDelay(50000);
          }
        }
      }
    });
    gainX = 0;
    faderGainLife.setInitialDelay(25);
    faderGainLife.setRepeats(true);
    faderGainLife.start();
  }
}

代码示例来源:origin: RaiMan/SikuliX2

public void setTimeout(int timeout){
 Timer timer = new Timer(timeout, new ActionListener(){
   @Override
   public void actionPerformed(ActionEvent arg0) {
    command = null;
    dismiss();
   }
 });
 timer.setRepeats(false);
 timer.start();
}

相关文章