Our first window; login example

In this tutorial we will develop our first window from scratch. Until now we have been using predefined windows but now we are going to make a customized window and its components. We will use the example of the typical window we use when we login in or when we register in an application.

The window of our example will have "Demo application" as title, a label called "user", another label called "password", two input boxes to insert the corresponding data and two buttons; "login" and "register"

In the main method in the code below, we create a window and we add the components created through the "placeComponents".

In this "placeComponents" method, we create all the components one by one (JLabels, JTextField, JPasswordField, JButton). With the setBounds method we give each component the coordinates for their localization on the window as well as the width and height, adding them after to the window through the add method of the Panel class.

package com.edu4java.swing.tutrial3;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class LoginView {

	public static void main(String[] args) {
		JFrame frame = new JFrame("Demo application");
		frame.setSize(300, 150);

		JPanel panel = new JPanel();


	private static void placeComponents(JPanel panel) {


		JLabel userLabel = new JLabel("User");
		userLabel.setBounds(10, 10, 80, 25);

		JTextField userText = new JTextField(20);
		userText.setBounds(100, 10, 160, 25);

		JLabel passwordLabel = new JLabel("Password");
		passwordLabel.setBounds(10, 40, 80, 25);

		JPasswordField passwordText = new JPasswordField(20);
		passwordText.setBounds(100, 40, 160, 25);

		JButton loginButton = new JButton("login");
		loginButton.setBounds(10, 80, 80, 25);
		JButton registerButton = new JButton("register");
		registerButton.setBounds(180, 80, 80, 25);


<< Previous Next >>