좋아, 버튼을 클릭 할 때마다 카운터에 값을 추가하는 간단한 프로그램을 만들었습니다. 이제 '자동'버튼을 클릭하면 카운터의 값을 높이기 위해 '자동'버튼 기능을 추가하고 싶습니다.Button ActionListener
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.TimeUnit;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Gui extends JFrame{
private static final long serialVersionUID = 1L;
private JButton uselesButton;
private JButton autoButton;
private FlowLayout layout;
private long counter = 0;
public Gui() {
super("Button");
layout = new FlowLayout(FlowLayout.CENTER);
this.setLayout(layout);
uselesButton = new JButton(String.format("Pressed %d times", counter));
add(uselesButton);
uselesButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
counter++;
uselesButton.setText(String.format("Pressed %d times", counter));
}
});
autoButton = new JButton("Auto");
add(autoButton);
autoButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for(long i =0; i < 99999999;i++) {
try {
TimeUnit.MILLISECONDS.sleep(10);
} catch (InterruptedException e1) {
System.out.println("ERROR");
}
counter = i;
uselesButton.setText(String.format("Pressed %d times", counter));
}
}
});
}
}
난 점을 명심 : 그것은 화면에 루프가 완료 값 대신 업데이트를 각 카운터 값을 렌더링하지 않습니다 때문에이 문제가 있어요 .. 여기 내 코드입니다 초보자 ... 모든 도움을 주셨습니다 :)
문제는 무엇입니까? – Sello
자동 버튼은 무엇을하고 있을까요? –
[Swing Timer] (http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html)가이 문제를 해결할 것으로 생각합니다. – Azad