2013-10-26 29 views
0

我仍然有更新JLabel使用计时器的问题。我似乎无法弄清楚我错过了什么。全局第二个变量保持为零,所以我猜测定时器正在工作但不更新GUI窗口?没有运气与javax计时器Jlabel

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

class Globals 
{ 
    public static int seconds = 0; 
} 

class main 
{ 
    public static void main(String Args[]) 
    { 

     //text timeline = new text(); 
     JFrame testing = new JFrame(); 
     frame textdes = new frame(); 
     testing.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     testing.setSize(1000,1000); 
     testing.setVisible(true); 

     Countdown timer = new Countdown(); 
     Timer countdown = new Timer(5000, timer); 
     countdown.start(); 

     JLabel countdowntext = new JLabel(); 
     countdowntext.setText("Now its :" + Globals.seconds); 
     testing.add(countdowntext);  
     testing.add(textdes); 

    } 
} 

class frame extends JFrame 
{ 
    class Countdown implements ActionListener 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      Globals.seconds++; 
     } 
    } 
} 

回答

2

您只在程序中设置标签的文本一次。然后计时器更改变量的值,但不对标签执行任何操作。它应该做的:

Globals.seconds++; 
countdowntext.setText("Now its :" + Globals.seconds); 

这里有一个完整的例子:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

class Globals { 
    public static int seconds = 0; 
} 

class Main { 
    public static void main(String Args[]) { 

     //text timeline = new text(); 
     JFrame testing = new JFrame(); 
     testing.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     testing.setSize(1000,1000); 
     testing.setVisible(true); 

     JLabel countDownLabel = new JLabel(); 
     countDownLabel.setText("Now it's : " + Globals.seconds); 
     testing.add(countDownLabel); 

     CountDown countdown = new CountDown(countDownLabel); 
     Timer timer = new Timer(5000, countDown); 
     timer.start(); 
    } 
} 

class CountDown implements ActionListener { 
    private JLabel countDownLabel; 

    public CountDown(JLabel countDownLabel) { 
     this.countDownLabel = countDownLabel; 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     Globals.seconds++; 
     this.countDownLabel.setText("Now it's : " + Globals.seconds); 
    } 
} 
+0

我试图做的是较早的,但它有错误出现,说明它无法找到符号 – lecardo

+0

您必须通过JLabel更新到您的CountDown实例,作为构造函数参数,以便倒计时可以保留对标签的引用并在每次调用时更新它。您还应该学习Java命名约定并避免创建无用的类。看到我编辑的答案。 –

1

试试这个 -

public class MainFrame{ 
    JFrame frame = new JFrame("Main frame"); 
    JPanel panel = new JPanel(); 
    JLabel countdownText = new JLabel(); 

    int seconds = 0; 

    public MainFrame() { 
     Timer timer = new Timer(5000, new Countdown()); 
     timer.start(); 

     panel.add(countdownText); 

     frame.getContentPane().add(panel); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.setPreferredSize(new Dimension(320,240)); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 

     countdownText.setText("Now its :" + seconds); 
    } 

    class Countdown implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 
      seconds++; 
      countdownText.setText("Now its :" + seconds); 
     } 
    } 

    public static void main(String [] args){ 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new MainFrame(); 
      } 
     }); 
    } 
}