ठीक है, इसलिए मैंने एक साधारण प्रोग्राम बनाया है जो बटन को क्लिक करने पर हर बार काउंटर करने के लिए मूल्य जोड़ता है। अब, "ऑटो" बटन क्लिक होने पर काउंटर के मान को बढ़ाने के लिए मैं "ऑटो" बटन सुविधा जोड़ना चाहता हूं।बटन एक्शनलिस्टर
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
क्या * बटन * ऑटो बटन क्या करना चाहिए? –
मुझे लगता है [स्विंग टाइमर] (http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) इसे हल करेंगे। – Azad