एक GUI इंटरफ़ेस बनाने में Im जहां उपयोगकर्ता एक टेक्स्ट फ़ील्ड में एक निंबर में प्रवेश करता है, फिर नंबर को CurrentAccount के ऑब्जेक्ट के पैरामीटर के रूप में जोड़ा जाता है। इस संख्या को फिर यादृच्छिक मान में जोड़ा या घटाया जाता है। मैं चाहता हूं कि यह हर 5 सेकंड में हो, समीकरण पूरा होने के बाद मूल्य लेना और मूल्य को जोड़ना या घटाना जब तक उपयोगकर्ता इसे बंद करने के लिए नहीं कहता। अब तक मैंने यह कोड यह तय करने के लिए बनाया है कि क्या किसी रैंडम नंबर को घटाया जाना चाहिए, रैंडम नंबर जेनरेट करना चाहिए, उसे अकाउंट बैलेंस में जोड़ना या घटाना चाहिए फिर अकाउंट बैलेंस को टेक्स्ट फील्ड में प्रिंट करें।
//method generates a random number to determine if a value should be added or subtracted from the myBalance variable within theAccount classes and completes the equation
int month = 0;
private void simulate()
{
try
{
if(month<12)
{ //Creates instance of Random to decide if the random number should be added or subtracted to the balance
Random decider = new Random();
for (int i = 0; i < 1; i++)
{
int ans = decider.nextInt(2)+1;
//if the decider value == 1, subtract or "withdraw" random number from the balance
if (ans == 1)
{
//creates instance of Random to create random number
Random bal = new Random();
int withdrawAmount = bal.nextInt((500 - 100) + 1) + 100;
//sets account balance to the balance subtract the random number
theAccount.myBalance = theAccount.myBalance - withdrawAmount;
//increments the month timer
month++;
//prints the new balance to the transaction text field
jTextArea2.setText("The new balance is " + theAccount.myBalance );
} else
{
//if decider value == 2, add or "deposit" random number to the balance
//creates instance of Random to create random number
Random bal = new Random();
int depositAmount = bal.nextInt((500 - 100) +1) + 100;
//sets account balance to the balance plus the random number
theAccount.myBalance= theAccount.myBalance + depositAmount;
//increments the month timer
month++;
//prints the new balance to the transaction text field
jTextArea2.setText("The new balance is " + theAccount.myBalance );
}
//if the account has a balance of -£200, generate an error and reset the balance to the user inputted value
if (theAccount.myBalance < -200)
{
jTextArea1.setText("Overdraft of £200 only");
theAccount.myBalance = value;
}
//if the account has a balance of 0, generate an error as the account must always have at least £1 before entering the overdraft
if(theAccount.myBalance == 0)
{
jTextArea1.setText("An account must always have £1");
}
}
}
} catch (NullPointerException i)
{
jTextArea2.setText("Create an Account");
}
}
Ive ने एक थ्रेड। स्लीप (5000) विधि का उपयोग करने की कोशिश की, लेकिन इसे गलत जगह पर डाल दिया होगा क्योंकि यह 5 सेकंड के लिए क्रिएट बटन को अटका देता है और शेष राशि को स्वयं प्रिंट करता है। इस मामले में कोई मदद महान होगी। इसके अलावा मेरे पास उपयोगकर्ता इनपुट का पता लगाने के लिए एक विधि है और जाहिर तौर पर इसे और इस पद्धति को कॉल करने के लिए बटन पर एक एक्शन श्रोता है।
[संशोधित टिप्पणी] मैंने कोड को लूप करने के लिए एक टाइमर का उपयोग करने की भी कोशिश की, लेकिन मुझे लगता है कि इसे सही कोड पर दोहराने के लिए इंगित किया जा सकता है क्योंकि यह मेमोरी को भरने के अलावा और कुछ नहीं करता है।
ActionListener timerListener = new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
ControlPanel.this.simulate();
}
};
Timer theTimer = new Timer(5000,timerListener);
2 जवाब
यहाँ आप java.util.Timer
के लिए एक उदाहरण हैं:
हमें टाइमर: MyTimerTask द्वारा कॉल किए जाने वाले कार्य की आवश्यकता है
import java.util.Timer;
import java.util.TimerTask;
public class TimerTaskExample extends TimerTask {
@Override
public void run() {
// Implement your Code here!
}
}
फिर हमें इस टास्क को निष्पादित करने के लिए एक टाइमर शेड्यूल करना होगा:
Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTimerTask(), 0, 10 * 1000);
पहला तर्क टास्क को निष्पादित करना है।
दूसरा पैरामीटर पहले निष्पादन से पहले एक देरी बताता है।
तीसरा पैरामीटर मिलीसेकंड में अवधि है। (यहाँ: हर 10 सेकंड में निष्पादित करें!)
कृपया ध्यान दें कि यदि आप अपने GUI में इस तंत्र के साथ काम करना चाहते हैं तो आपको SwingUtilities.invokeLater()
का उपयोग करना होगा
पूरक के बाद संपादित करें:
javax.swing.Timer
का उपयोग करने के लिए आपको टाइमर रन बनाने के लिए start()
कॉल करना होगा।
समय-समय पर कार्य चलाने का एक और तरीका है ScheduledExecutorService <का उपयोग करना। / a>
तब आपका कोड इस तरह दिख सकता है:
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
Runnable yourRunnable = new Runnable() {
@Override
public void run() {
// Implement your Code here!
}
};
और फिर अपना कार्य चलाएं:
int initialDelay = 0;
int delay = 5;
scheduler.scheduleWithFixedDelay(yourRunnable, initialDelay, delay, TimeUnit.SECONDS);
आपका कार्य आह्वान विधि shutdown()
से रद्द किया जा सकता है
scheduler.shutdown();
संबंधित सवाल
नए सवाल
java
जावा एक उच्च स्तरीय प्रोग्रामिंग भाषा है। इस टैग का उपयोग तब करें जब आपको भाषा का उपयोग करने या समझने में समस्या हो। इस टैग का उपयोग शायद ही कभी किया जाता है और इसका उपयोग अक्सर [वसंत], [वसंत-बूट], [जकार्ता-ई], [Android], [javafx], [हडूप], [श्रेणी] और [मावेन] के साथ किया जाता है।