regarding JFrame

hi friends,

i have used JFrame in my application. application runs perfectly but everytime when we run it, we have to maximize it then only controls on it are appeared.Also i want to set position of JFrame window at center. but it takes top-left position all the time it loaded.

my code is :

public class user extends JFrame

{

JFrame f ;

JPanel pn;

JLabel lblLoingId;

JLabel lblPwd;

JTextField txtLoginId;

JTextField txtPasswordField;

JButton btnLogin;

public user()

{

pn= new JPanel();

f.getContentPane().add(pn);

lblLoginid= new JLabel("Login Name:");

lblPwd = new JLabel("Password:");

txtLoginId = new JTextField(10);

txtPwd = new JTextField(10);

btnLogin = new JButton("Login");

pn.add(lblLoginId);

pn.add(txtLoginId);

pn.add(lblPwd);

pn.add(txtPwd);

pn.add(btnLogin);

}

public static void main(String args[])

{

f = new JFrame("Login Screen");

f.setVisible(true);

f.setSize(200,150);

user obj = new user();

}

}

}

what should i do for above things?

[1175 byte] By [deepalee] at [2007-9-30 20:27:21]
# 1

Hi Deepalee

The reason why your controls don't show is because you have done this

1.Make new frame

2. Set it to visible

3. Add the panel

The panel is there, but is showing only after a re-paint. It will show if do this

1.make new frame

2.add panel

3.set visible

The to make the frame appear where you want it, look at setBounds in the JFrame class to show you how to do this.

Cheers,

Rachel

RachelSwailes at 2007-7-7 1:11:46 > top of Java-index,Administration Tools,Sun Connection...
# 2

hi!! since u r already extending JFrame, u don't need to declare it again. The following code may work as you expect. I have just commented ur lines and added mine.

public class user extends JFrame

{

//JFrame f ;

JPanel pn;

JLabel lblLoingId;

JLabel lblPwd;

JTextField txtLoginId;

JTextField txtPasswordField;

JButton btnLogin;

public user()

{

super("Login Screen");// Calls the constructor of JFrame

setSize(200,150);// Sets the size of the frame

setVisible(true);

// This method can be used to place the window anywhere

setLocation(350,150);

pn= new JPanel();

//f.getContentPane().add(pn);

getContentPane().add(pn);

lblLoginid= new JLabel("Login Name:");

lblPwd = new JLabel("Password:");

txtLoginId = new JTextField(10);

txtPwd = new JTextField(10);

btnLogin = new JButton("Login");

pn.add(lblLoginId);

pn.add(txtLoginId);

pn.add(lblPwd);

pn.add(txtPwd);

pn.add(btnLogin);

}

public static void main(String args[])

{

//f = new JFrame("Login Screen");

//f.setVisible(true);

//f.setSize(200,150);

user obj = new user();

obj.setVisible(true);

}

}

}

Now u need not maximise everytime. Enjoy ;)

Aswin at 2007-7-7 1:11:46 > top of Java-index,Administration Tools,Sun Connection...
# 3
thank u very much.your suggestions help me and i found the solution.byedeepalee
deepalee at 2007-7-7 1:11:46 > top of Java-index,Administration Tools,Sun Connection...