How To Add Image To Jframe Background
How to Add together an Image in JFrame
Hello, folks!. In this tutorial, we will learn and focus on how to add together an image in Coffee JFrame.
Create a Java JFrame
Firstly, we import the Java AWT and Java Swing libraries into our lawmaking. Secondly, we use the Java Swing library for the cosmos of JFrame. Furthermore, we employ various components and methods within the Swing library to achieve this. Thus, to become more than information nearly Java AWT, Java Swing, their fields, construction, methods, etc., please do visit the beneath posts.
-
How to change JLabel font style and size in Coffee
-
How to Go out JFrame on Close in Coffee Swing
As a consequence, after reading the posts you will learn a lot about various Coffee libraries and use them with ease. Nosotros will utilise the below code for JFrame creation:
Let's look at the lawmaking:
JFrame frame = new JFrame(); //JFrame Cosmos frame.setTitle("Add Image"); //Add together the title to frame frame.setLayout(null); //Terminates default period layout frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //End plan on close push button frame.setBounds(100, 200, 350, 300); //Sets the position of the frame frame.setVisible(truthful); // Exhibit the frame
Output:
Now, we have created a JFrame with a patently background. We must now add an paradigm to the JFrame. Sadly, there is no inbuilt method in JFrame to do this. Notwithstanding, nosotros will accomplish information technology by using JLabel and performing our desired action.
Calculation an Paradigm in Java JFrame
Firstly, we create a JLabel using the Java Swing library. Secondly, we utilize the setIcon()
method to add and display the image. This method defines to brandish the icon. However, if the value of the icon is null, nothing is displayed. We will now convert the epitome to an icon and pass this equally a parameter to the setIcon()
method.
We utilize the ImageIcon()
class to convert the image to icons. This method is an implementation of the Icon Interface to call up icons from images. The internal performance of this method is that it creates an assortment of byte streams which is read from the supported image file types using getResourceAsStream()
form. Furthermore, the array of bytes gets converted to an Image Icon.
Let's expect at the code:
//Java Programme to Add Paradigm in Jframe import javax.swing.*; import java.awt.*; public class AddImage extends JFrame { public static void main(String[] args) { JFrame frame = new JFrame(); //JFrame Cosmos frame.setTitle("Add Image"); //Add the championship to frame frame.setLayout(zippo); //Terminates default catamenia layout frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Cease program on shut push button frame.setBounds(100, 200, 350, 300); //Sets the position of the frame Container c = frame.getContentPane(); //Gets the content layer JLabel label = new JLabel(); //JLabel Creation label.setIcon(new ImageIcon("CodeSpeedy.jpg")); //Sets the image to be displayed every bit an icon Dimension size = label.getPreferredSize(); //Gets the size of the image label.setBounds(50, thirty, size.width, size.acme); //Sets the location of the image c.add(label); //Adds objects to the container frame.setVisible(true); // Exhibit the frame } }
Output:
Quick Analysis :
Nosotros pass the "CodeSpeedy.jpg" prototype location to the ImageIcon()
method, where the image gets converted to a byte array. The assortment now gets converted to an Icon. Now, we add the Image Icon to our JFrame using the setIcon()
method. We can likewise fix the location and dimensions of the image using the setBounds() method and Dimension grade respectively. Therefore, the image is now displayed on the JFrame.
Thanks for reading. I hope yous institute this postal service useful!.
How To Add Image To Jframe Background,
Source: https://www.codespeedy.com/how-to-add-an-image-in-jframe/
Posted by: geerdinduch.blogspot.com
0 Response to "How To Add Image To Jframe Background"
Post a Comment