Sunday, February 9, 2020

Buku Tips dan Trik Pemrograman Java - Mengubah Icon di JFrame

Sinopsis

Untuk mengubah Icon di JFrame berbasis GUI di java yang akan menjadi ciri khas tertentu dalam sebuah aplikasi, biasa nya developer menggunakan icon seperti dalam tampilan berikut

Buku Tips dan Trik Pemrograman Java - Mengubah Icon di JFrame


Serta otomatis menampilkan JFrame di Center Monitori, yang perlu kalian lakukan buatlah Icon dengan format gambar *.png seperti berikut dibawah ini



Kalian bisa membuat class \(Center.java\) seperti berikut

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;

/**
 *
 * @author mulkan.ms@gmail.com
 */
public class Center {
    public static void center(JFrame frame){
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int X = (screen.width / 2) - (frame.getWidth() / 2); // Center horizontally.
        int Y = (screen.height / 2) - (frame.getHeight() / 2); // Center vertically.

        frame.setBounds(X,Y , frame.getWidth(),frame.getHeight());
    }

    public static void center(JDialog frame){
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int X = (screen.width / 2) - (frame.getWidth() / 2); // Center horizontally.
        int Y = (screen.height / 2) - (frame.getHeight() / 2); // Center vertically.

        frame.setBounds(X,Y , frame.getWidth(),frame.getHeight());
    }
    public static void center(JInternalFrame frame){
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int X = (screen.width / 2) - (frame.getWidth() / 2); // Center horizontally.
        int Y = (screen.height / 2) - (frame.getHeight() / 2); // Center vertically.

        frame.setBounds(X,Y , frame.getWidth(),frame.getHeight());
    }
    public static void set(JInternalFrame frame){
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int X = (screen.width / 2) - (frame.getWidth() / 2); // Center horizontally.
        int Y = (screen.height / 2) - (frame.getHeight() / 2); // Center vertically.

        frame.setBounds(X,Y , frame.getWidth(),frame.getHeight());
    }
    public static void set(JFrame frame){
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int X = (screen.width / 2) - (frame.getWidth() / 2); // Center horizontally.
        int Y = (screen.height / 2) - (frame.getHeight() / 2); // Center vertically.

        frame.setBounds(X,Y , frame.getWidth(),frame.getHeight());
    }
    public static void set(JDialog frame){
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int X = (screen.width / 2) - (frame.getWidth() / 2); // Center horizontally.
        int Y = (screen.height / 2) - (frame.getHeight() / 2); // Center vertically.

        frame.setBounds(X,Y , frame.getWidth(),frame.getHeight());
    }

}
serta kelas \(ChangeIcon.java\)

import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author mulkan.ms@gmail.com
 */
public class ChangeIcon {
    public static void change(JFrame frame)
    {
        URL iconURL = frame.getClass().getResource("/icon/icon.png");
        // iconURL is null when not found
        ImageIcon icon = new ImageIcon(iconURL);
        frame.setIconImage(icon.getImage());
    }
    public static void change(JDialog frame)
    {
        URL iconURL = frame.getClass().getResource("/icon/icon.png");
        // iconURL is null when not found
        ImageIcon icon = new ImageIcon(iconURL);
        frame.setIconImage(icon.getImage());
    }
    

}

Semuanya harus dibuat class static agar mudah digunakan, selanjutnya kalian panggil saja class static tersebut tepat dibawah method \(initComponents()\) seperti contoh dibawah ini

    public MainFrame() {
        initComponents();
        ChangeIcon.change(this);
        Center.center(this);
    }





No comments:

Post a Comment