Need help in adding no of buttons in java.

Morningdot Hablu

Morningdot Hablu

@morningdot-6Xuj4M Oct 22, 2024
Hello friends,
I have to add no's of buttons in applet.
But i did not able to add this.Check this code.
import java.awt.*;
import javax.swing.*;
/*<APPLET CODE="kk.java" HEIGHT=300 WIDTH=400></APPLET>*/
public class kk extends JApplet
{
int i;
public void meth()
{
JButton b[]=new JButton[20];
}
public void init()
{
meth();
setLayout(new FlowLayout());
for(i=0;i<5;i++)
{
add(b);
}
}
}

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Manish Goyal

    Manish Goyal

    @manish-r2Hoep Sep 4, 2010

    Try this program,You will get what you want
    import java.applet.*;
    import java.awt.*;
    import javax.swing.*;
    /*<APPLET CODE="kk.java" HEIGHT=300 WIDTH=400></APPLET>*/
    public class kk extends JApplet
    { 
       public void init ()
       { 
        for(int i=0;i<20;i++)
            {
            JButton b;
            setLayout(new FlowLayout());
            b=new JButton("click");
                 add(b);
            }
       }
    }
  • Morningdot Hablu

    Morningdot Hablu

    @morningdot-6Xuj4M Sep 4, 2010

    check this code goyal.
    .
    import java.applet.*;
    import java.awt.*;
    import javax.swing.*;
    /*<APPLET CODE="ss.java" HEIGHT=300 WIDTH=400></APPLET>*/
    public class ss extends JApplet
    {
    JButton b;
    public void init ()
    {
    for(int i=0;i<20;i++)
    {
    setLayout(new FlowLayout());
    //b.setSize(4,90);//applet not initialised.
    b=new JButton("click");
    //b.setSize(4,90);//not works on button.
    add(b);
    }
    }
    public void paint(Graphics g)
    {
    for(int j=0;j<=20;j++)
    {
    b.setSize(4,60);//if you want to use setSize() only option to use there.
    }
    }
    }
    actually i want to apply setSize function on every button.
    here setSize work only on the last one.
    That's why i want to take the array of Button.But i dont know why it will display errors.
  • Manish Goyal

    Manish Goyal

    @manish-r2Hoep Sep 4, 2010

    Try this code and say cheese 😀

    import java.applet.*;
    import java.awt.*;
    import javax.swing.*;
    /*<APPLET CODE="kk.java" HEIGHT=300 WIDTH=400></APPLET>*/
    public class kk extends JApplet
    {
        JButton b[]=new JButton[20];
        public void init ()
        {
        for(int i=0;i<20;i++)
        {
            setLayout(new FlowLayout());
            b[i]=new JButton("click");
            add(b[i]);
            
        }
        }
        
    public void paint(Graphics g)
    {
        for(int j=0;j<20;j++)
    {
    b[j].setSize(4,90);
    }
    }    
    }
  • Morningdot Hablu

    Morningdot Hablu

    @morningdot-6Xuj4M Sep 4, 2010

    Oh set..!!
    what a silly mistake i done.
    Well nice solution goyal..!!
  • Manish Goyal

    Manish Goyal

    @manish-r2Hoep Sep 4, 2010

    Actually the problem was that we were just creating array ,not button objects.
  • Morningdot Hablu

    Morningdot Hablu

    @morningdot-6Xuj4M Sep 4, 2010

    check this code goyal.
    import java.applet.*;
    import java.awt.*;
    import javax.swing.*;
    /*<APPLET CODE="ss.java" HEIGHT=300 WIDTH=400></APPLET>*/
    public class ss extends JApplet
    {
    JButton b[]=new JButton[90];
    public void init ()
    {
    setLayout(new GridLayout(1,0));
    for(int i=0;i<90;i++)
    {
    b=new JButton();
    add(b);

    }
    }

    public void paint(Graphics g)
    {
    for(int j=0;j<90;j++)
    {
    b[j].setSize(5,5);
    }
    }
    }

    actually i don't know the function to create the event line(you must seen event line in any music player like vlc).
    so i decided to create my own event line function.
    If you goone clicked it on any particular point of event line you song will forwarded to that point(i want to do this with the help of time of the song at that point).
    .
    I must posted here on CE when i completed this.
    Thanks for the help goyal...!!