Many developers want their own Layout Manager with user-defined so JAVA has provided a separate method for the every container of JFrame Component and that method is setBounds(). This method helps the developers to position the container as per their need .
About SetBounds() method:
This method consists four parameter
Syntax:
xxx.setBounds(int x_axis, int y_axis, int width, int height);
We can arrange the containers with the help of this method easily.
A example of this method is shown below:
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.Dimension;
public class LoginPanel {
public LoginPanel() {
}
public static void main (String[] args) {
JFrame frame = new JFrame();
frame.setLayout(null);
//For Positioning the Containers
//Labeling
JLabel label = new JLabel("WELCOME TO"+
"LOGIN PANEL", JLabel.CENTER);
JLabel user = new JLabel("UserName:\t");
JLabel pass = new JLabel("Password:\t");
//Some features of SETBOUNDS() Methods
//setBounds(x-axis, y-axis, width, height);
label.setBounds(150,10,220,20);
user.setBounds(150,50,100,50);
pass.setBounds(150,90,100,50);
//TextField
JTextField userField = new JTextField();
JPasswordField passField = new JPasswordField();
//Some of the features of SETBOUNDS() Methods
//setBounds(x-axis, y-axis, width, height);
userField.setBounds(250,65,80,20);
passField.setBounds(250,105,80,20);
//Button
JButton but1 = new JButton("Submit");
//setBounds(x-axis, y-axis, width, height);
but1.setBounds(200,150,100,20);
//adding containers to JFRAME
frame.getContentPane().add(label);
frame.getContentPane().add(user);
frame.getContentPane().add(userField);
frame.getContentPane().add(pass);
frame.getContentPane().add(passField);
frame.getContentPane().add(but1);
//features of JFrame
//Although these can be defined
//before the containers.
//We defined at last
frame.setVisible(true);
frame.setSize(500,300);
}
}
And the output must be like this:

Login Panel Output
Filed under: Applet | Tagged: containers, height, java, JFrame, positon, programmer, setBounds, trick, width, x, y | Leave a Comment »

