Swing-JFrame 浅谈如何在JFrame中添加背景色及背景图片

原文地址为: Swing-JFrame 浅谈如何在JFrame中添加背景色及背景图片

JFrame默认是BorderLayout

JPanel默认是FlowLayout。

1.JFrame设置背景色,注意体会注释的那句话。

 

package com.tools;

import java.awt.Color;

import javax.swing.JFrame;
public class Test extends JFrame
{
	public static void main(String[] args)
	{
		new Test();
	}
	
	
	public Test()
	{
		this.setSize(400,300);
		this.setLocation(400,300);
		this.setBackground(Color.blue);
		this.getContentPane().setBackground(Color.red);
		this.getContentPane().setVisible(false);//如果改为true那么就变成了红色。
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
}


 2.给JFrame设置背景图片。

方法1:通过在JFrame中添加一个JPanel,背景图片放在JPanel上来实现。代码如下:

import java.awt.*;

import javax.swing.*;


public class Test extends JFrame
{
	//创建一个容器
	Container ct;
	//创建背景面板。
	BackgroundPanel bgp;
	
	//创建一个按钮,用来证明我们的确是创建了背景图片,而不是一张图片。
	JButton jb;
	public static void main(String[] args)
	{
		new Test();
	}
	public Test()
	{
		//不采用任何布局方式。
		ct=this.getContentPane();
		this.setLayout(null);
		
		//在这里随便找一张400*300的照片既可以看到测试结果。
		bgp=new BackgroundPanel((new ImageIcon("images\\background.jpg")).getImage());
		bgp.setBounds(0,0,400,300);
		ct.add(bgp);
		
		//创建按钮
		jb=new JButton("测试按钮");
		jb.setBounds(60,30,160,30);
		ct.add(jb);
		
		this.setSize(400,300);
		this.setLocation(400,300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
}
class BackgroundPanel extends JPanel
{
	Image im;
	public BackgroundPanel(Image im)
	{
		this.im=im;
		this.setOpaque(true);
	}
	//Draw the back ground.
	public void paintComponent(Graphics g)
	{
		super.paintComponents(g);
		g.drawImage(im,0,0,this.getWidth(),this.getHeight(),this);
		
	}
}


效果如图:

 方法2:我们用JLayeredPane,JLayeredPane 为 JFC/Swing 容器添加了深度,允许组件在需要时互相重叠。Integer 对象指定容器中每个组件的深度,其中编号较高的组件位于其他组件之上。常用的几个层如下图:

具体实现代码如下:

/**
 * 给JFrame 添加一个背景图案。
 */
package com.swingpractise;


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[] args)
	{
		new JFrameBackground4();
	}
	
	public JFrameBackground4()
	{
		layeredPane=new JLayeredPane();
		image=new ImageIcon("images\\background.jpg");//随便找一张图就可以看到效果。		
		//创建背景的那些东西
		jp=new JPanel();
		jp.setBounds(0,0,image.getIconWidth(),image.getIconHeight());

		jl=new JLabel(image);
//		jl.setBounds(0,0,image.getIconWidth(),image.getIconHeight());
		jp.add(jl);
		
		//创建一个测试按钮
		jb=new JButton("测试按钮");
		jb.setBounds(100,100,100,100);
		
		//将jp放到最底层。
		layeredPane.add(jp,JLayeredPane.DEFAULT_LAYER);
		//将jb放到高一层的地方
		layeredPane.add(jb,JLayeredPane.MODAL_LAYER);
		
		this.setLayeredPane(layeredPane);
		this.setSize(image.getIconWidth(),image.getIconHeight());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocation(image.getIconWidth(),image.getIconHeight());
		this.setVisible(true);	
	}
}

测试效果如下图:



 欢迎大家一起讨论更好更优的方法。


转载请注明本文地址: Swing-JFrame 浅谈如何在JFrame中添加背景色及背景图片

猜你喜欢

转载自blog.csdn.net/wangchaoqi1985/article/details/80744752