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
1 2 3 |
Syntax: <strong> </strong><em><strong>xxx.setBounds(int x_axis, int y_axis, int width, int height);</strong> </em> |
We can arrange the containers with the help of this method easily.
A example of this method is shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
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:
Good Example
Oh ! Thanks ‘anurag’