Develop an applet to display the message "Happy New Year" within a text field

Experiment : 6
આપણે આમાં એક applet બનાવવાનું છે જે "happy new year" નો મેસેજ print કરતું હોય એને તે button નો ઉપયોગ થી થાય.
yearbtn.java

import java.awt.*;
import java.awt.event.*;

public class yearbtn extends Frame implements ActionListener{
Button b1;
Label l1;
    yearbtn()
    {
        b1=new Button("year");
        l1=new Label(" ");
        setLayout(new FlowLayout());
        setVisible(true);
        setSize(300,500);
        add(b1);
        add(l1);
        b1.addActionListener(this);
    }
    public static void main(String args[])
    {
        yearbtn s=new yearbtn();        
    }
    
public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==b1)
        {
            l1.setText("happy new year");
        }
}
}

  • Output :
  •  
     



આ ઉદાહરણમાં, જ્યારે તમે લેબલ પર ક્લિક કરો છો, ત્યારે લેબલનો ટેક્સ્ટ લેબલ "Label clicked!" પર બદલાવામાં આવશે. તો એપ્લિકેશનમાં બટનનો છે પરંતુ પ્રતિક્રિયાઓને દર્શાવવામાં આવે છે.આવું માટે, એપ્લિકેશનનો કોડ વધુ વિસ્તૃત કરવામાં આવે છે કારણ કે તમારી એપ્લિકેશનમાં વધુમાં વધુ GUI એલીમેન્ટ્સ અને ક્રિયાઓ હોવી જરૂરી છે. પરંતુ, આ ઉદાહરણ તમને બટન અને લેબલની બસીક પ્રિંસિપલ્સ સમજાવે છે.
બટન (Button) અને લેબલ (Label) પ્રોગ્રામિંગ માં GUI એલીમેન્ટ્સ સંબંધિત છે.
બટન (Button): બટન, સાધારણ રીતે, તેમના સાથે એક ક્લિક કરવાથી કોઈ પ્રોસેસિંગ યોજના શરૂ કરે છે. Java માં, બટનનો ઉપયોગ વપરીને એપ્લિકેશનમાં બટન દૃષ્ટિકોના ફોર્મમાંથી વિવિધ ક્રિયાઓનું પ્રાથમિક રૂપે સ્થાન રખવામાં આવે છે.
લેબલ (Label): લેબલ એપ્લિકેશનમાં માહિતીનો પ્રદર્શન કરવાનું એક સાધારણ રીતે ઉપયોગ થાય છે. Java માં, લેબલનો ઉપયોગ પ્રદર્શિત કરવામાં માહિતીનાં ટેક્સ્ટને સંકલિત કરવા માટે કરવામાં આવે છે.

આપણે આમાં એક બટન ને એક text field લેસુ અને button ના click પર text field માં message બતાવ શું.

import java.awt.*;
import java.awt.event.*;

આ import દ્વારા આપણે લાઇબ્રેરી add કરી awt.

Button b1;
Label l1;
Button પર clicked અને label ને change કરવાનું.
આ દ્વારા b1 નામનું બટન અને l1 લેબલ જેમાં happy new year print કરવા.



public static void main(String args[])
    {
        yearbtn s=new yearbtn();        
    }

Program ની શરૂઆત થાય main function થી.
yearbtn નામનાં class નો object બનાવી ને call કર યો છે. yearbtn નો s નામ નો object. બનાવ્યો.

  yearbtn()
    {
        b1=new Button("year");
        l1=new Label(" ");
        setLayout(new FlowLayout());
        setVisible(true);
        setSize(300,500);
        add(b1);
        add(l1);
        b1.addActionListener(this);
    }

yearbtn() જે એક constructer છે. જે yearbtn નો s નામનો object બનતા ની સાથે call થાય છે.
 ત્યાર બાદ ક્રમશઃ button create size set કરવી અને છેલ્લે ActionListner.

actionPerformed(ActionEvent) એક Java એન્ટરફેસ છે અને java.awt.event.ActionListener ઇન્ટરફેસ દ્વારા પ્રદાન કરવામાં આવે છે. આ ઇન્ટરફેસમાં એક મેથડ છે, જેનો નામ actionPerformed છે.આપેલ ઉદાહરણોમાં, તમે ક્લાસ બનાવ્યો છે અને એપ્લિકેશનમાં બટનને ક્લિક કરવાથી કઈ ક્રિયા કરવી તે નિર્ધારવા માટે actionPerformed(ActionEvent) મેથડનો ઉપયોગ કરવામાં આવે છે.જ્યારે તમે બટન પર ક્લિક કરો છો, તે એક ActionEvent ઉત્પન્ન કરે છે અને તમારી actionPerformed(ActionEvent) મેથડ કોલ કરે છે.

actionPerformed(ActionEvent e)

જેવુ કંઇ clicked થસે action થસે તેવું આ function call થસે.

setText() એક મેથડ છે, જેનો ઉપયોગ ટેક્સ્ટ બૉક્સ, લેબલ, બટન માં વિવિધ એપ્લિકેશનમાં ટેક્સ્ટને સેટ કરવા માટે કરવામાં આવે છે.

l1.setText("happy new year");

setText(""); દ્વારા happy new year set થયું.

આમ simple button ના action પર actionPerformed call 
થાય એના આધારે lable update થાય.

ટિપ્પણીઓ

આ બ્લૉગ પરની લોકપ્રિય પોસ્ટ્સ

Write an applet that display the position of the mouse at the upper left corner of the applet when it is dragged or moved. draw a 10x10 pixel rectangle filed with black at the current mouse position

Write an applet that uses the mouse listener, which overrides only two methods which are mousePressed and mouseReleased

Write an applet that draws a circle. The dimension of the applet should be 500 x 300 pixels. The circle should be centered in the applet and have a radius of 100 pixels. Display your name centered in a circle.( using drawOval() method ).