Need help in adding no of buttons in java.

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.*;
/**/
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

  • Manish Goyal
    Manish Goyal
    Try this program,You will get what you want
    import java.applet.*;
    import java.awt.*;
    import javax.swing.*;
    /**/
    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
    check this code goyal.
    .
    import java.applet.*;
    import java.awt.*;
    import javax.swing.*;
    /**/
    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
    Try this code and say cheese 😀

    import java.applet.*;
    import java.awt.*;
    import javax.swing.*;
    /**/
    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
    Oh set..!!
    what a silly mistake i done.
    Well nice solution goyal..!!
  • Manish Goyal
    Manish Goyal
    Actually the problem was that we were just creating array ,not button objects.
  • Morningdot Hablu
    Morningdot Hablu
    check this code goyal.
    import java.applet.*;
    import java.awt.*;
    import javax.swing.*;
    /**/
    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...!!

You are reading an archived discussion.

Related Posts

This is what we received few hours ago. - I wish to work with CrazyEngineers Team because: to create a revolution in technology Article: PETE Process Promises Successful Technology Fusion...
Hello CEans, Many of the CEans might be the great testing professionals, I want advice from such experienced testing professionals,, I want to know how to wirte the best test...
Is there any Short way to reach particular path in regedit??Rather than expanding each and every path......
hey Friends, In life many times we get shock......Me talking about Electrical Shock..... Share your experience here.................. Such that other will be aware of it........... Tell others precautions too..... Regards,...
Hey Friends, Here is the way, you can suggest some ways to reduce the capital cost of Transmission Line....... Give all your suggestions here regarding everything....means from construction of Poles...