2015-10-10 69 views
0

對於幾天來,我一直努力在Java中的Timer程序。 它的工作原理是,你打開程序並且JLabel加起來。出於某種原因,它不會重複,只能使用一次。搖擺定時器只能使用一次

這是我的代碼。

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 

import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.Timer; 
import javax.swing.SwingConstants; 
import java.awt.Font; 

public class TimerC extends JFrame implements ActionListener{ 

Timer t = new Timer(5, this); 
public int intClicked; 
public String stringClicked; 
public JLabel clicked; 
public TimerC() { 
    t.start(); 
    JPanel p1 = new JPanel(); 
    getContentPane().add(p1); 
    p1.setLayout(null); 

    JLabel clicked = new JLabel(); 
    clicked.setFont(new Font("Tahoma", Font.PLAIN, 64)); 
    clicked.setHorizontalAlignment(SwingConstants.CENTER); 
    clicked.setText("0"); 

    int intClicked = 1; 
    String stringClicked = String.valueOf(intClicked); 

    clicked.setText(stringClicked); 

p1.add(clicked); 

clicked.setSize(42, 100); 
clicked.setLocation(191, 97); 
    } 

@Override 
public void actionPerformed(ActionEvent e) { 
} 
} 
+0

1)的源代碼中的白色空間中的單個空行的全部就是*永遠*需要。 '{'之後或'}'之前的空行通常也是多餘的。 2)使用合乎邏輯的一致形式縮進代碼行和塊。縮進旨在使代碼的流程更易於遵循! –

+0

'計時器t =新定時器(5,這一點);'這裏的基本問題是,'this'的'actionPerformed'方法不..nothing!變化,首先,要打印的東西'System.out' .. –

+0

'p1.setLayout(NULL);'Java的圖形用戶界面有不同的OS」,屏幕大小,屏幕分辨率等方面的工作在不同的地區使用不同的PLAFs。因此,它們不利於像素的完美佈局。請使用佈局管理器或[它們的組合](http://stackoverflow.com/a/5630271/418556)以及[white space]的佈局填充和邊框(http://stackoverflow.com/a/17874718/ 418556)。 –

回答

1

該行內部TimerC()JLabel clicked = new JLabel();刪除JLabelclicked,因爲你已經宣佈點擊外,與這樣您作爲一個局部變量聲明再次。閱讀關於變量範圍。

我不知道你有沒有注意到,但您使用的字體大小大,和你的JLabel的尺寸不夠大,試圖改變它的大小:

clicked.setSize(200, 100);

定義一些行爲actionPerformed

@Override 
    public void actionPerformed(ActionEvent e) { 
     clicked.setText(intClicked++ + " "); 
     // this.repaint(); it's not necessary 
    } 
+0

仍似乎工作?它只是停留在1 – RegularMoments

+1

@AndrewThompson確實 –

+0

@RegularMoments該行內部的'定時器C()','的JLabel點擊=新的JLabel();''刪除盈JLabel''clicked',因爲你已經宣佈點擊之外,和用這種方式,您將可以作爲一個局部變量聲明再次 –

1

作爲一個良好的編程習慣,它總是建議到自我參照this傳遞給其他對象,只有對象的建設完成之後,在c頌歌流。在代碼示例中,

public class TimerC extends JFrame implements ActionListener{ 

     Timer t = null; 
     public JLabel clicked; 

     public TimerC() { 
      ... 
      clicked = new JLabel(); 
      ... 
      intClicked = 1; 
      ... 
      clicked.setSize(42, 100); 
      clicked.setLocation(191, 97); 

      t = new Timer(5, this); 
      t.start(); 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      intClicked = intClicked + 1; 
      clicked.setText(String.valueOf(intClicked)); 
      clicked.repaint(); 
     } 
    } 
+0

我嘗試使用的代碼,但仍doesent工作... – RegularMoments

0

對於每個第二更新,則需要Timer t = new Timer(1000, this);並使用actionPeformed如下:

public void actionPerformed(ActionEvent e) { 
     System.out.println(e); 
     clicked.setText(String.valueOf(intClicked++)); 
}