Java – JFrame background image
•
Java
I'm creating a GUI. Although it's a simple one, I want a background image (2048 x 2048) to fill the whole window and a square to the upper left corner. Occasionally, 64 x 64 images can be loaded Thank anyone for any help in advance:)
Solution
This is a simple example of adding a background image to a JFrame
import javax.swing.*; import java.awt.*; import java.awt.event.*; class BackgroundImageJFrame extends JFrame { JButton b1; JLabel l1; public BackgroundImageJFrame() { setTitle("Background Color for JFrame"); setSize(400,400); setLocationRelativeTo(null); setDefaultCloSEOperation(EXIT_ON_CLOSE); setVisible(true); /* One way ----------------- setLayout(new BorderLayout()); JLabel background=new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png")); add(background); background.setLayout(new FlowLayout()); l1=new JLabel("Here is a button"); b1=new JButton("I am a button"); background.add(l1); background.add(b1); */ // Another way setLayout(new BorderLayout()); setContentPane(new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png"))); setLayout(new FlowLayout()); l1=new JLabel("Here is a button"); b1=new JButton("I am a button"); add(l1); add(b1); // Just for refresh :) Not optional! setSize(399,399); setSize(400,400); } public static void main(String args[]) { new BackgroundImageJFrame(); } }
>Click here for more information
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码