myeclipse 用Swing可视化编程,怎么添加背景图片?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了myeclipse 用Swing可视化编程,怎么添加背景图片?相关的知识,希望对你有一定的参考价值。

背景图片大小怎么确定?

import java.awt.*;

import javax.swing.*;

public class TestBackgroundColor extends JFrame  

public static void main(String[] args)  

TestBackgroundColor tbc = new TestBackgroundColor();

tbc.setVisible(true);

private JPanel imagePanel;

private ImageIcon background;

public TestBackgroundColor()  

background = new ImageIcon("C:\\\\Users\\\\Administrator\\\\Documents\\\\

My FTPRush Downloads\\\\项目\\\\Photoes\\\\007.jpg");//背景图片

JLabel label = new JLabel(background);//把背景图片显示在一个标签里面

//把标签的大小位置设置为图片刚好填充整个面板

label.setBounds(0,0,background.getIconWidth(),background.getIconHeight());

//把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明

imagePanel = (JPanel)this.getContentPane();

imagePanel.setOpaque(false);

//内容窗格默认的布局管理器为BorderLayout

imagePanel.setLayout(new FlowLayout());

imagePanel.add(new JButton("测试按钮"));

this.getLayeredPane().setLayout(null);

//把背景图片添加到分层窗格的最底层作为背景

this.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setSize(background.getIconWidth(),background.getIconHeight());

this.setVisible(true);

实现效果:

参考技术A

代码的核心是组件全放一个JPanel里面,然后覆盖paintComponent()、同时绘背景图片。


import java.awt.Image;
import java.awt.Graphics;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class JFrameBackground2 extends JFrame 
    private Image bgImage = new ImageIcon("IMAG0693_resize.jpg").getImage();
    public JFrameBackground2()
        super("JFrameBackground2");
        this.setSize(500, 400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel pnl = new JPanel()
            public void paintComponent(Graphics g) 
                super.paintComponent(g);
                g.drawImage(bgImage, 0, 0, null);
            
        ;
        pnl.add(new JLabel("Graphics Demo"));
        pnl.add(new JTextField("请输入文字"));
        pnl.add(new JButton("ClickMe"));
        this.add(pnl);
        this.setVisible(true);
    
    
    public static void main(String args[]) 
        new JFrameBackground2();
    

追问

用MyEclipse能不能像拖拽的组件设置属性一样快速设置背景图片呢?

追答

日前 没有,自己重写一个JFrame 或 JPanel,就方便了。

本回答被提问者采纳

swing可视化编程-使用label添加图片

                                                                                  -- 感谢代码和视频提供者:黎海波童鞋

      本篇是使用过了java中的可视化swing图形界面编程,讲解如何使用JLabel文本标签显示图片。

1、关键代码:

       JLabel lb=new JLabel();//创建一个标签对象
        ImageIcon icon=new ImageIcon("images/b.jpeg");//创建背景图片
        lb.setIcon(icon);

2、全部代码如下:


import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Myframe extends JFrame 
	public Myframe()
		super("这是一个有图片的窗体");//标题
		this.setSize(500, 300);//窗体大小
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗体自动结束程序
		this.setLayout(null);//没有布局
		
		JLabel lb=new JLabel();//创建一个标签对象
		ImageIcon icon=new ImageIcon("images/b.jpeg");//创建背景图片
		lb.setIcon(icon);
		lb.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());//设置图像的位置和大小
		//要想图片整张显示填满窗体的话就要把图片的宽高改成和窗体大小一样

		this.add(lb);
		this.setLocationRelativeTo(null);//窗体默认居中
		this.setVisible(true);//显示可见
	
	
	
	
	//主函数入口
	public static void main(String[] args) 
		new Myframe();
	


3、视频讲解:https://live.csdn.net/v/172214

以上是关于myeclipse 用Swing可视化编程,怎么添加背景图片?的主要内容,如果未能解决你的问题,请参考以下文章

swing可视化编程-使用label添加图片

swing可视化编程-使用label添加图片

swing可视化编程-使用label添加图片

swing可视化编程-使用label添加图片

myeclipse6.0.1一打开就没响应

java的swing编程,Jtable的单元格如何赋值