Create a window using the JFrame class in Java - ProgrammerTech
Language:

Search

Create a window using the JFrame class in Java

  • Share this:
Create a window using the JFrame class in Java

Programming interfaces in Java using the swing . library

Swing library in Java, in the next lessons we will deal with programming interfaces in Java using the swing library. We will learn how to program interfaces and add elements such as buttons, text and tables. The user interface is called Graphical User Interface and is abbreviated as GUI.

In this lesson, we will explain a very simple example, which is how to create an interface only. And we will follow the rest of the elements in the rest of the lessons and we will build an interface only so that the introduction of the next lessons will be easy for you. When we program interfaces in Java with the swing library, we include the library with the command.

import javax.swing.*;

The asterisk * means import all the special tools into the swing library and then we include the interface class via the JFrame class. Then we derive an object, as we learned in the previous lessons, which is the interface class, also called a container. Because it contains all the buttons, texts and tables that we will discuss in the upcoming lessons.

 

swing library classes in java

  1. The JFrame class is used to add a window.
  2. The JButton class is used to add a button.
  3. The JLabel class is used to add a title or an image.
  4. The JTextArea class is used to add a paragraph.
  5. The JTextField class is used to add a text input box.
  6. JList Class This class is used to add a list.
  7. The JTable class is used to add a table.

Example

package swin;
import javax.swing.*;
public class Swin{
     public static void main(String []args){
        JFrame F=new JFrame();
        F.setSize(400, 400);
        F.setResizable(true);
        F.setVisible(true);
     }
}

At first we called the swing library with all its tools, then we entered the main program and included the JFrame interface class. And then by setSize we set the width of the interface and its length 400 * 400 means square. In the next line via setResizable its task for the zoom button in the interface we set it true ie. It is enabled, and if its value is false, this button will be disabled. And the last line is for the interface's appearance property, and its value is true, meaning that the appearance property is enabled.

create interface from JFrame java

 

We also note that the zoom button is activated in it, and it is square in length and width of equal length. If we want to give a name to this interface, the code form is as follows.

package swin;
import javax.swing.*;
public class Swin{
     public static void main(String []args){
        JFrame F=new JFrame("Ejemplo JFrame");
        F.setSize(400, 400);
        F.setResizable(true);
        F.setVisible(true);
     }
}

And when executing, it will show it the name we set for it.

title gui JFrame java

 

Do not try to learn the Swing library unless you have learned Java before, because you will face great difficulties in understanding the code even if the code is explained in detail.


Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com