Ankit Arora
Ankit Arora

Reputation: 13

Border Layout program not done as expected

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class BLayout
{
JFrame f;
JButton b[];
BLayout(String s)
{
f=new JFrame(s);
b=new JButton[5];
String b1[]={"NORTH","South","Center","East","WEst"};
String x[]=

{BorderLayout.NORTH,BorderLayout.SOUTH,BorderLayout.CENTER,BorderLayout.EAST,BorderLayout.WEST};
for(int i=0;i<b1.length();i++){
b[i]=new JButton(b1[i]);
f.add(b[i],x[i]);
}
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(400,400);
    f.setVisible(true);
    }
    public static void main(String... s)
    {
    new BLayout("Border LAyout");
    }
    }

Error show that 'variable not found'.

What should I do?

Why b1.length does not considered as 5 and when I give 5 then there is no error.

Upvotes: 0

Views: 44

Answers (1)

camickr
camickr

Reputation: 324108

for(int i=0;i<b1.length();i++)

When using Arrays, the length is not a method of the array. It is a variable.

You should be using:

for(int i = 0; i < b1.length; i++)

Also don't be afraid to use "whitespace" when coding to make the code more readable.

Upvotes: 1

Related Questions