2013-04-17 2 views
0

편집 할 수없는 텍스트 필드가있는 패널이 있습니다. 패널에 textField.setText(timer); 가지를 추가하고 싶습니다. 기본적으로 JTextArea에 0 : 0 : 0과 같이 표시하기를 원합니다.Java 타이머 및 텍스트 필드

Timer, TimerTasks, ActionListeners 및 정렬을 만들려고했습니다. 나는 이것에 매달릴 수 없다. 어쩌면 내가 타이머를 추가하기 때문에? 지금은 public void guiComponents() throws Exception{...}에 추가하려고했습니다. 이 메서드는 패널, 프레임 및 기타 구성 요소의 모든 속성을 유지합니다.

어쩌면 나는 타이머의 전체 개념을 과장하지 않을 것입니다. Origanlly이 작업을 수행하기 위해 double-nested for 루프를 사용해 보았습니다. 그러나 나머지 프로그램이 실행되는 동안 계속해야하기 때문에 그렇게 할 수 없었습니다.

답변

3

이 경우 JTextField를 사용하지 마십시오. 텍스트를 표시하려면 JLabel을 사용하십시오. 다음과 같이 :

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

public class TimerTime extends JFrame implements ActionListener 
{ 
    JLabel timeLabel; 

    public TimerTime() 
    { 
     timeLabel = new JLabel(new Date().toString()); 
     getContentPane().add(timeLabel, BorderLayout.NORTH); 
    } 

    public void actionPerformed(ActionEvent e) 
    { 
     timeLabel.setText(new Date().toString()); 
    } 

    public static void main(String[] args) 
    { 
     TimerTime frame = new TimerTime(); 
     frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setVisible(true); 

     int time = 1000; 
     javax.swing.Timer timer = new javax.swing.Timer(time, frame); 
     timer.setInitialDelay(1); 
     timer.start(); 
    } 
} 
+0

'날짜'는 무엇을 해야할지 모르겠다. 그러나 나는 실시간을 원하지 않는다. 앱 실행이 시작된 후 위쪽으로 세는 것과 비슷하며 끝날 때까지 계속 증가합니다. – SelfDeceit

+1

@SelfDeceit 그 개념은 동일합니다. 'actionPerformed' 메소드에서 카운터를 증가시키고 출력을 포맷해야합니다. 나는 네가 적어도 그렇게 많이 할 수 있다고 확신한다. – MadProgrammer