I'm trying to create a simple JFrame with some scomponents like JTextField and JButton The below code is not showing any error,
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class Commform
{
static final JFrame common = new JFrame();
JTextField Farmername = new JTextField();
Commform()
{
common.getContentPane().setLayout(null);
Farmername.setBounds(100, 100, 285, 100);
}
}
But, after declaring the object for JFrame (common) and JTextField (Farmername), if I try to use setBounds method before the constructor like the bolded line,
public class Commform
{
static final JFrame common = new JFrame();
JTextField Farmername = new JTextField();
Farmername.setBounds(100, 100, 285, 100);
Commform()
{
common.getContentPane().setLayout(null);
}
}
then netbeans underlines in red color and shows the tooltip as "package Farmername does not exist, illegal start or expression" When I am able to use object inside constructor, why can't I use just after creating the object ?
And even using the Farmername inside static, as shown below doesn't give any error.
static
{
System.out.println("Called from main function");
common.getContentPane().setLayout(null);
Farmername.setBounds(100, 100, 285, 100);
}
I'm creating this form in the class without main function because I would like to call this frame and enter credentials to insert them to database, later to display particular person's credentials in their respective text field, I want to call this same frame. But at any level I don't want to use main function to this class.
Please help me.