ठीक है, निम्न कोड JTextame को JTextArea और JButton के साथ दिखाता है। जब बटन क्लिक किया जाता है, तो टाइमर घटना को समय-समय पर (उनके बीच दूसरी देरी के साथ) बटन से संबंधित बटन भेजता है जो वर्तमान समय के साथ एक लाइन जोड़ता है।
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.Timer;
public class TimerTest extends JFrame implements ActionListener{
private static final long serialVersionUID = 7416567620110237028L;
JTextArea area;
Timer timer;
int count; // Counts the number of sendings done by the timer
boolean running; // Indicates if the timer is started (true) or stopped (false)
public TimerTest() {
super("Test");
setBounds(30,30,500,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null);
area = new JTextArea();
area.setBounds(0, 0, 500, 400);
add(area);
JButton button = new JButton("Click Me!");
button.addActionListener(this);
button.setBounds(200, 400, 100, 40);
add(button);
// Initialization of the timer. 1 second delay and this class as ActionListener
timer = new Timer(1000, this);
timer.setRepeats(true); // Send events until someone stops it
count = 0; // in the beginning, 0 events sended by timer
running = false;
System.out.println(timer.isRepeats());
setVisible(true); // Shows the frame
}
public void actionPerformed(ActionEvent e) {
if (! running) {
timer.start();
running = true;
}
// Writing the current time and increasing the cont times
area.append(Calendar.getInstance().getTime().toString()+"\n");
count++;
if (count == 10) {
timer.stop();
count = 0;
running = false;
}
}
public static void main(String[] args) {
// Executing the frame with its Timer
new TimerTest();
}
}
अच्छा, यह कोड javax.swig.Timer ऑब्जेक्ट्स का उपयोग करने का नमूना है। प्रश्न के विशेष मामले के संबंध में। टाइमर को रोकने के लिए अगर कथन बदलना चाहिए, और जाहिर है, कार्रवाई के कार्यों को लागू किया गया है।निम्नलिखित टुकड़ा समाधान की एक कंकाल actionPerformed है:
public void actionPerformed(ActionEvent e) {
if (e.getComponent() == myDealerComponent()) {
// I do this if statement because the actionPerformed can treat more components
if (! running) {
timer.start();
runnig = true;
}
// Hit a card if it must be hitted
switch (getJBTable(JB.total, JB.aces > 0)) {
case 0:
JB.hit();
break;
case 1:
break done;
case 2:
JB.hit();
JB.bet *= 2;
break done;
}
if (JB.total >= 21) { // In this case we don't need count the number of times, only check the JB.total 21 reached
timer.stop()
running = false;
}
}
}
IMHO इससे समस्या का समाधान, अब @ user920769 लगता है जहां ActionListener और प्रारंभिक/रोक की स्थिति में डाल दिया जाना चाहिए ...
@kleopatra: धन्यवाद मुझे इस टाइमर क्लास का अस्तित्व दिखाने के लिए, मुझे इसके बारे में कुछ नहीं पता है और यह आश्चर्यजनक है, स्विंग एप्लिकेशन में बहुत सी कार्यरत चीजें संभव बनाएं :)
उदाहरण के लिए आपको बहुत बहुत धन्यवाद, लेकिन मुझे इन पंक्तियों पर एक त्रुटि मिल रही है: टाइमर = नया टाइमर (1000, यह); timer.setRepeats (सही); यह कहकर क्रमशः उपयुक्त कन्स्ट्रक्टर या विधि नहीं मिल रहा है। क्या वे बहिष्कृत थे? – Fractaly
क्या आप टाइमर क्लास आयात करते हैं? आखिरी रिलीज में विधियों को भी बहिष्कृत नहीं किया गया है, इसलिए यह आपकी गलती है। [यहां जावा 7 एपीडोक] (http://download.oracle.com/javase/7/docs/api/javax/swing/Timer.html) – Charliemops