Page 216 - CITS - CSA - TP (Volume 2) - Hindi
P. 216

कं  ूटर सॉ वेयर ए ीके शन - CITS




           4   े म आरंभीकरण:
              JFrame frame = new JFrame(“RadioButton Example”);

               “RadioButton Example” टाइटल के  साथ एक JFrame (िवंडो) बनाएं

           5  RadioButton बनाना :

              JRadioButton radioButton1 = new JRadioButton(“Male”); JRadioButton radioButton2 = new JRadioButton(“Female”);
              “Male” and “Female” लेबल के  साथ दो JRadioButton इं  स बनाएँ ।

           6  ButtonGroup सेटअप :

              ButtonGroup group = new ButtonGroup();
               group.add(radioButton1);

              group.add(radioButton2);

              एक बटन समूह बनाएं  और उसम  रेिडयो बटन जोड़ । इससे यह सुिनि त होता है िक समूह म  एक समय म  के वल एक ही रेिडयो बटन का चयन
              िकया जा सकता है।

           7  सबिमट बटन ि एशन :
              JButton submitButton = new JButton(“Submit”);

              Create a JButton with the label “Submit”.

           8  सबिमट बटन के  िलए ActionListener:
              submitButton.addActionListener(new ActionListener()

              { @Override public void actionPerformed(ActionEvent e)

               { String selectedOption = radioButton1.isSelected() ? “Male” : “Female”; JOptionPane.showMessageDialog(frame, “Selected
              Option: “ + selectedOption); } });
              सबिमट बटन पर एक ए न िल र संल  कर । जब बटन पर   क िकया जाता है, तो यह जाँचता है िक कौन सा रेिडयो बटन चुना गया है और
              उसके  अनुसार एक मैसेज डायलॉग  दिश त करता है।

           9   े म लेआउट और घटक जोड़ना:

              frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS)); frame.getContentPane().add(radioButton1);
              frame.getContentPane().add(radioButton2); frame.getContentPane().add(submitButton);

               े म के  लेआउट को ऊ ा धर बॉ  लेआउट पर सेट कर , और  े म के  कं ट ट पैन म  रेिडयो बटन और सबिमट बटन जोड़ ।
           10   े म कॉ  फ़गरेशन और  दश न:

              frame.setSize(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true);

              आकार, िडफ़ॉ  बंद ऑपरेशन सेट कर , और  े म को िविजबल बनाएं ।

              यह  ो ाम दो रेिडयो बटन और एक सबिमट बटन के  साथ एक सरल GUI बनाता है। सबिमट बटन पर   क करने पर, एक मैसेज  डायलॉग
              िदखाया जाता है, जो चयिनत िलंग को दशा ता है। बटन ुप का उपयोग िविश  चयन सुिनि त करता है।









                                                           202

                                  CITS : IT & ITES - कं  ूटर सॉ वेयर ए ीके शन - अ ास 117
   211   212   213   214   215   216   217   218   219   220   221