Java GUI :  JFrame , JLabel , ImageIcon and day 1
Tags: java
Published: 2022-07-09 19:05:23



## What is java ?

Java is a general🌎 purpose programming language that is class👨‍🏫 based ,object-oriented and software platform📡 that runs 🏃on millions of devices. Java is based on C and C++.java is also using to build GUI (graphic user interface ) .

## Java GUI

java swing is a GUI widget📟 toolkit 🧰for java. swing is a set of program that provide ability 🦸🏼‍♀️to create GUI components like buttons🖲️, textfeild⌨, scrollbars🎚 and so on…..Java swing components are platform independent and the components are lightweight📦.

 

_This is what i learn today (09-07-2022)_


 **JFrame** class is a type of a container 🚛 which inherits the  javax.swing.JFrame class. JFrame works like the main window 🪟 where components like buttons, labels, textfield etc ….are added to create GUI.
In simple terms it is a container that provide a window on the screen.

 

```
import javax.swing.JFrame; // to insert frame we need this

public class frame
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame(); //creating instance of a jframe called frame

        frame.setSize(500,500);  //seting size of the frame width and height
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //it is used to specify one of the several options for the close button. exit the application when the user closes the window.
        frame.setVisible(true); // it makes sceen appear in the screen
    }
}
```

**JLabel** can display 🖥️ either text, an image or both. Labels 🏷️ are align's by setting 🧑🏾‍🔧 the verticle and horizontal alignment in its display area

```
import javax.swing.*;  // if we want to add all the componets we can use * symbol to

import java.awt.*;

public class label
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        JLabel label = new JLabel(); //create JLabel instance with no image and with empty string for the title

        label.setText("click me"); //returns the text string that label displays
        label.setForeground(new Color(0xDC0E0E));

        frame.setTitle("java swing");
        frame.setBounds(500, 150, 1000, 800);      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
        frame.setVisible(true);
        frame.add(label);
    }
}

```
**imageicon** is an implementation of the icon⭐ interface that paints icon from image.

**Setimageicon** method of frame🔲 class is used too change the icon of frame

```

import javax.swing.JFrame; // to insert frame we need this

import javax.swing.ImageIcon;  // importing imageicon

import javax.swing.JLabel;
import java.awt.*;  // this is using for changing the color of the font of the label

public class imageicon
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        ImageIcon image =new ImageIcon("image/polygon.jpg"); //C:\Users\ASUS\Downloads\imagejpg  // location of the image
        JLabel label = new JLabel();
        label.setText("image "); // text of the label
        label.setIcon(image); //setting the image of the icon of frame or window
        //setting position of the image and label
        label.setHorizontalTextPosition(JLabel.LEFT); // setting the text position or where  the text display, LEFT of the image or RIGHT of the image or CENTER of the imaege
        label.setVerticalTextPosition(JLabel.BOTTOM);// setting the text position or where  the text display, BOTTOM of the image or TOP of the image

        // setting position of the image+label in the frame
        label.setHorizontalAlignment(JLabel.SOUTH_EAST); // setting image+label LEFT or RIGHT or CENTER and more ......
        label.setVerticalAlignment(JLabel.TOP); //setting image+label TOP or CENTER and more .......

        label.setForeground(Color.RED); //seting font color
        label.setFont(new Font("Courier new",Font.BOLD,24));  // setting font for text, font style and font size

        frame.setSize(500,500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.add(label);
        frame.setIconImage(image.getImage()); // adding icon for the frame or window
    }
}

```