2013-10-10 2 views
0

일부 게임에서 매 초마다 JLabel을 편집하는 방법 (시간 남음 또는 점수). 이 내 코드 여기Jlabel을 매 초 편집하는 방법?

static int l = 1; 
static int s = 5000; 
static int t = 90; 
public static void main(String[] args) { 

    //Frame 
    final JFrame f = new JFrame(); 
    f.setTitle("Picture Puzzle"); 
    f.setSize(500,500); 
    f.setLocationRelativeTo(null); 
    f.setResizable(false); 
    f.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    f.setVisible(true); 

// 몇 가지 추가 거즈 여기

JLabel blevel00 = new JLabel("Level:" + l); 
    JLabel bscore00 = new JLabel("Score:" + s); 
    JLabel btime00 = new JLabel("Time:" + t); 

    p2.add(blevel00); 
    p2.add(bscore00); 
    p2.add(btime00); 

// 몇 가지 추가 거즈

start.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent arg0) { 
      // TODO Auto-generated method stub 
      while(t != 0) { //the t is the static int t = 90; 
      f.add(p2); 
      f.remove(p1); 
      f.setVisible(true); 
      f.revalidate(); 
      f.repaint(); 
      } 
      t--; 
     } 
    }); 

} }

이다 나는이 아무것도 시도 일어난다. 어떤 도움을 주시면 감사하겠습니다.

답변

2

스윙은 단일 스레드 환경입니다. 즉, UI에 대한 모든 변경 및 수정은 이벤트 발송 스레드 컨텍스트 내에서 발생할 것으로 예상됩니다.

결코 끝나지 않는 루프 나 I/O를 차단하는 것처럼이 스레드를 차단하는 것은이 스레드가 페인트 이벤트를 포함하여 새 이벤트를 처리하지 못하도록합니다.

스윙은이 문제에 대한 여러 가지 해결책을 제공합니다. 가장 좋은 해결책은 아마도 javax.swing.Timer을 사용하는 것입니다. 이렇게하면 EDT 컨텍스트 내에서 호출되는 일반 콜백을 예약 할 수 있으므로 일반 기반에서 UI를 수정할 수 있습니다.

은 자세한 내용

enter image description here

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.Font; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.text.DateFormat; 
import java.util.Date; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class SimpleClock { 

    public static void main(String[] args) { 
     new SimpleClock(); 
    } 

    public SimpleClock() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 
     private JLabel time; 
     public TestPane() { 
      setLayout(new GridBagLayout()); 
      time = new JLabel(); 
      time.setFont(time.getFont().deriveFont(Font.BOLD, 48)); 
      add(time); 
      updateTime(); 
      Timer timer = new Timer(500, new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        updateTime(); 
       } 
      }); 
      timer.start(); 
     } 

     protected void updateTime() { 
      time.setText(DateFormat.getTimeInstance().format(new Date())); 
     } 
    } 

} 
+0

그래서 난 내 코드에서 while 루프를 사용하지 못할 간단한 예제와

업데이트

에 대한 Concurrency in SwingHow to use Swing Timers에서보세요? –

+0

'actionPerformed' 메소드가 실행되는 이벤트 디스패치 스레드의 컨텍스트 내에 있지 않습니다 ... – MadProgrammer

+0

샘플 코드를 보내 주셔서 감사합니다. 그것은 내 프로젝트에서 많이 도움이 될 것입니다. –

관련 문제