Timer 如何从函数中使用Swing计时器

Timer 如何从函数中使用Swing计时器,timer,repaint,Timer,Repaint,我想停止执行一个函数来打印我在paintComponent中拥有的内容 main(){ f(){ change(list) Timer to stop 1 minute and repaint } } 我想已经包括在内了 摆动计时器 public class exampe extends JPanel implements ActionListener { int x; int y; int temp = 0; public void paintComp

我想停止执行一个函数来打印我在paintComponent中拥有的内容

main(){
   f(){
      change(list)
      Timer to stop 1 minute and repaint
   }
}


我想已经包括在内了

摆动计时器

public class exampe extends JPanel implements ActionListener {

int x;
int y;
int temp = 0;

public void paintComponent(Graphics g) {

    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.fillOval(x - 2, y - 2, 4, 4);
}

public void set(int X, int Y) {

    x = X;
    y = Y;
}

public static void main(String args[]) {

    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            new exampe().createAndDisplayGUI();
        }
    });


 }


 public void createAndDisplayGUI(){


    JFrame frame = new JFrame("TEST");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    exampe ex = new exampe();
    JScrollPane scroll = new JScrollPane(ex);
    frame.getContentPane().add(scroll);
    frame.setSize(400, 300);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    Timer PointTimer = new Timer(1000, ex);
    PointTimer.setInitialDelay(1000);
    PointTimer.start();
    System.out.println("started");
}

@Override
public void actionPerformed(ActionEvent e) {

   // set(rand.nextInt(350), rand.nextInt(350));
      set(temp+10,temp+10);
      temp=temp+2;
      repaint();
}
}
public class exampe extends JPanel implements ActionListener {

int x;
int y;
int temp = 0;

public void paintComponent(Graphics g) {

    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.fillOval(x - 2, y - 2, 4, 4);
}

public void set(int X, int Y) {

    x = X;
    y = Y;
}

public static void main(String args[]) {

    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            new exampe().createAndDisplayGUI();
        }
    });


 }


 public void createAndDisplayGUI(){


    JFrame frame = new JFrame("TEST");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    exampe ex = new exampe();
    JScrollPane scroll = new JScrollPane(ex);
    frame.getContentPane().add(scroll);
    frame.setSize(400, 300);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    Timer PointTimer = new Timer(1000, ex);
    PointTimer.setInitialDelay(1000);
    PointTimer.start();
    System.out.println("started");
}

@Override
public void actionPerformed(ActionEvent e) {

   // set(rand.nextInt(350), rand.nextInt(350));
      set(temp+10,temp+10);
      temp=temp+2;
      repaint();
}
}