0
我想要做一個擺動計時器是執行時,我按下按鈕,但從未開始。 我的代碼是:無法啓動擺動計時器
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();
您發佈的代碼看起來不錯。你確定'btnIniciar'是你的UI中添加的按鈕嗎?你可以使用調試器來驗證,或者在該按鈕的「ActionListener」中添加一個'System.out'調用 – Robin
謝謝。是的,我添加了一個system.out,我確認它進入了按鈕,但沒有進入定時器的監聽器:-( – user650034
你可以發佈你的整個代碼嗎? – Pratik