2013-10-26 2 views
0

타이머를 사용하여 JLabel을 업데이트하는 데 계속 문제가 있습니다. 나는 내가 빠진 것을 알아낼 수 없다. 글로벌 두 번째 변수는 0으로 유지되므로 타이머는 작동하지만 GUI 창은 업데이트하지 않는 것으로 추측하고 있습니까?Jlabel이있는 javax 타이머가 없다면

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(); 
      } 
     }); 
    } 
}