java - actionPerformed using two same buttons variable -


  जेपीएनएल बीटीएन = नया जेपीनल (); Btns.add (नया जेबटन ("बीटीएन 1")); Btns.add (नया जेबटन ("बीटीएन 2")); Btns.setLayout ((नया फ्लो लेआउट (फ्लो लेआउट ट्रैफ़िक))); southPanel.add (btns);   

हैलो, ऊपर, मैंने कोड प्रदान किया है। मैं कुछ बटन के लिए क्रियात्मक विधि कैसे बनाऊं? उदाहरण के लिए, जब मैं बीटीएन 1 पर क्लिक करता हूं, तो इसे प्रिंट किया जाएगा बटन 1 को क्लिक किया गया है। जब मैं बीटीएन 2 पर क्लिक करता हूं, तो इसे प्रिंट किया जाएगा बटन 2 को क्लिक किया गया है।

धन्यवाद।

< P> बटन को ऑब्जेक्ट का एक उदाहरण बनाएं ..
  जेबटन बटन 1 = नया जेबटन ("बीटीएन 1"); जेबटन बटन 1 = नया जेबटन ("बीटीएन 1");   

फिर उन्हें पैनल में जोड़ें

  btns.add (button1); Btns.add (बटन 2);   

बटन को एक एक्शन कमांड जोड़ें:

  button1.setActionCommand ("button1"); button2.setActionCommand ("button2"); \   

एक ऐसा क्लास बनाएं जो ActionListener

       सार्वजनिक शून्य क्रियाप्रवर्तित (एक्शन एवेन्ट evt) {स्ट्रिंग एक्शन कॉमांड = ईवैट.एटएक्शन कॉमांड (); // कार्रवाई प्राप्त करें कमांड और स्ट्रिंग कार्रवाई करने के लिए इसे पास करें कॉमांड अगर (actionCommand.equals ("button1") // बटन 1 और क्लिक किया जाता है अगर (actionCommand.equals ("button2")) // बटन 1 क्लिक किया}  < / प्री>  

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -