无法启动挥杆计时器

时间:2012-11-21 09:48:17

标签: java swing timer

我正在尝试制作一个Swing Timer,当我按下按钮但从未启动时执行。 我的代码是:

public class prueba extends JFrame {
    java.util.Date date= new java.util.Date();
    private JPanel contentPane;
    Timer timero;
        ........

public prueba() {
         ..........
        timero  = new Timer(1000, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              System.out.println("otra vez....uauuauauaua");
            }
          });
        btnIniciar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                  timero.start();
                                  ...............
你能帮帮我吗? 谢谢,抱歉我的英文!

当我这样做时,我有一个错误:

public class prueba extends JFrame {
    java.util.Date date= new java.util.Date();
    private JPanel contentPane;
    Timer timero;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                prueba frame = new prueba();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public prueba() {




    final WebManager web = new WebManager("user","pass","http://www.spanishtracker.com/login.php");
    //web.EnableDebug();
    web.doLogin();
    web.getCookies;
        .....
    contentPane.add(txtSalida, gbc_txtSalida);

    timero.start();

它给了我一个错误:

java.lang.NullPointerException
    at prueba.<init>(prueba.java:216)
    at prueba$1.run(prueba.java:56)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$400(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)

These lines are:
216         timero.start();
56      prueba frame = new prueba();

1 个答案:

答案 0 :(得分:3)

您可能有另一个名为Timer timero .start() start(),{{1}}您呼叫{{1}}的{​​{1}}。作为参考,此shadows显示了如何启动和停止计时器,此example显示了如何将转发的{{1}}方法公开给封闭的计时器。后者有助于避免在构造函数结束之前过早地启动计时器。