在Java中给JFrame设置背景图片,我们需要继承JFrame类,重写其paintComponent方法,并在其中绘制背景图片。具体步骤如下: 创建一个新的类,继承JFrame类。 publicclassMyFrameextendsJFrame{privateImagebackgroundImage;publicMyFrame(){// 加载背景图片backgroundImage=Toolkit.getDefaultToolkit().getImage("background.jpg")...
* @param frame 将要设置的窗体对象 * @param img 作为背景的图片 */ public static void setBackgroundImage(JFrame frame, ImageIcon img) { JLabel imgLabel = new JLabel(img); imgLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight()); // 下面函数的参数必须为Integer对象 frame.getLaye...
(true); frame.setExtendedState(JFrame.MAXIMIZED_BOTH); //这句写上后没有效果,不知道为什么,jdk1.8 //AWTUtilities.setWindowOpaque(frame, false); //设置透明背景1,重要 frame.setBackground(new Color(0,0,0,0)); final JPanel pane = new JPanel() { private int[] snowX = null; private int[...
方法一:使用JLabel作为背景图片 这种方法将背景图片加载到一个JLabel中,然后将这个JLabel添加到JFrame的分层面板(LayeredPane)的最底层。 java import javax.swing.*; import java.awt.*; public class BackgroundJFrame { public static void main(String[] args) { JFrame frame = new JFrame("Background ...
在Java中设置窗体背景图片可以通过以下步骤来实现: 创建一个继承自JFrame的自定义窗体类,并重写其paintComponent方法。 import javax.swing.*; import java.awt.*; public class CustomFrame extends JFrame { private Image backgroundImage; public CustomFrame() { backgroundImage = new ImageIcon("path_to_your...
import javax.swing.*; public class JFrameBackground4 extends JFrame{ //创建一个JLayeredPane用于分层的。 JLayeredPane layeredPane; //创建一个Panel和一个Label用于存放图片,作为背景。 JPanel jp; JLabel jl; ImageIcon image; //创建一个按钮用于测试的。 JButton jb; public static void main(String[]...
一般我们在往JFrame中添加组件时,都加在了内容面板中,这个面板可以通过JFrame的成员方法getContentPane()取出来,所以如果设置JFrame的背景颜色,仍然会被内容面板盖住,不如设置内容面板的背景颜色,如果框架中还加有其他面板,内容面板的颜色也会被其他面板盖住,要注意一下面板的布局情况。
JButton button = new JButton("Hello" );panel.add( button );}public static void main(String [] args){BackgroundImage frame = newBackgroundImage();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(300, 300);frame.setLocationRelativeTo( null );frame.setVisible(true);}} ...
ImageIcon bg = new ImageIcon("background.jpg");// 把背景图片显示在一个标签里 JLabel label = new JLabel(bg);//把标签的大小位置设置为图片刚好填充整个面 label.setBounds(0,0,bg.getIconWidth(),bg.getIconHeight());//添加图片到frame的第二层 frame.getLayeredPane().add(label,new Integer(Integer...
创建一个JLabel,将图像应用于它的icon属性并将其设置为框架内容窗格。然后您需要适当地设置布局管理器,因为JLabel没有默认布局管理器 JFrame frame = ...; JLabel background = new JLabel(new ImageIcon(ImageIO.read(...))); frame.setContentPane(background); ...