Adding components to a JFrame in Java

Leave a Comment
There so many  components in java that can be added to a frame.Components include Labels ,textfields ,buttons menus panels,etc.
Today am going to show  you how ta add these components on your frame.
Having gone through the previous tutorial then you are set to continue with this and if you are having any  prooblems concerning the  previous post just leave a comment on the comment box and it will get solved for you.

By  now i assume that you  have already  fired your IDE and created a  new project. I will use the previous project to add components to the  frame that we  created.
The components that i am going to add to  the will be textfields ,labels buttons and a panel
so lets get started

Directly  above the constructor create objects of JTextField,JLabel,JButton and Jpanel as follows
 Step one

public JLabel name,password;

private  JTextField nameField;

private JPasswordField passwordField;

private JButton loginButton,cancelButton;

private JPanel  panel;

Step two

Create an instance of these objects inside the construct

panel=new JPanel();

name=new JLabel("Name");

password=new JLabel("Password");

nameField=new JTextField(15);

passwordField=new JPasswordField(15);

loginButton=new JButton("Login");

cancelButton=new JButton("Cancel");

 Step 3

add all these components to the frame as follows.(this is done inside the constructor)

add(panel);

add(name);

add(nameFied);

add(password);

add(passwordFied);

add(cancelButton);

add(loginButton);

 Step 4

Run your progarm and you will see the added components on the frame.



0 comments:

Post a Comment