首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

jframe添加背景图片有关问题

2013-07-04 
jframe添加背景图片问题package com.instantcommunication.guiimport java.awt.Containerimport java.aw

jframe添加背景图片问题
package com.instantcommunication.gui;

import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Login extends JFrame implements ActionListener {

   /**
 * 
 */
   private static final long serialVersionUID = 1L;
   private JPanel l_jp=null;
   private JLabel l_nameLabel=null,l_passLabel=null,l_image=null;  
   private JTextField l_jtf=null;
   private JPasswordField l_jpf=null;
   private JButton l_jb=null;
   private ImageIcon icon=new ImageIcon("a.jpg");
   Container con=this.getLayeredPane();
   Container con2=this.getContentPane();
   
   public Login(){
   setTitle("登录");
   l_jp=new JPanel(null);
   l_nameLabel=new JLabel("用户名");
   l_passLabel=new JLabel("密    码");
   l_image=new JLabel(icon);
   l_jtf=new JTextField(10);
   l_jpf=new JPasswordField(10);
   l_jb=new JButton("登录");  
   this.setBounds(200, 200, 380, 280);
   l_nameLabel.setBounds(70,60,50,30);
   l_passLabel.setBounds(70,110,50,30);
   l_jtf.setBounds(120, 60, 180, 30);
   l_jpf.setBounds(120, 110, 180, 30);
   l_jb.setBounds(150,170,80,30);
   l_image.setBounds(0, 0, 380, 280);
   con.add(l_image);
   con2.add(l_nameLabel);
   con2.add(l_passLabel);
   con2.add(l_jtf);
   con2.add(l_jpf);
   con2.add(l_jb);
   this.add(l_jp);
   l_jb.addActionListener(this);
   this.setVisible(true);
   this.setResizable(false);
   this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 
   //repaint();
   }
  


   
   public void actionPerformed(ActionEvent e){
   if(e.getSource().equals(l_jb)){
   setVisible(false);
   }
   }
   
   public static void main(String[] args){
   new Login();
   }
}

这里的两个Container实际上指的都是当前的容器。
因此下面的用法:
jframe添加背景图片有关问题
就相当于this和con,con2是等价的。
也就是说,你在往一个容器里面添加组件,当然会出现覆盖。
还有你要用"a.jpg"做背景的话,其实是分层排放的,你没有设置l_image放置在更下的一层。看似你分层了,其实没有。可以使用:con..add(l_image,new Integer(Integer.MIN_VALUE));来将l_image放置在最底层。
还有你要设置你顶层的组件属性为透明,不然底层会被顶层覆盖。
[解决办法]

   con.add(l_image);  
改成
con.add(l_image, new Integer(Integer.MIN_VALUE));
   l_image.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());试试看。

[解决办法]
我记得有个setOpaque()方法,你试试。。
至于paint和painComponent方法的好处吗?
painComponent只存在于swing中,awt中没有此方法。painComponent指的的是容器本身自己画出自己的组件;
paint方法swing和awt中都有此方法。
paint在swing中:改变容器本身的组件;
paint在awt中:改变组件样式。
说的有可能不对,要是详细的话,还是自己去查查的好!jframe添加背景图片有关问题
[解决办法]


//得到顶级容器
Container c=this.getContentPane();
//设置为空布局
this.setLayout(null);

//背景图片必须放在最下面 
BacImage bi=new BacImage();
///设置图片位置
bi.setBounds(0, 0, 360, 360);
c.add(bi);

//背景图片内部类
class BacImage extends JPanel
{

private static final long serialVersionUID = 1L;

Image im=null;
public BacImage()
{
im=new ImageIcon("image/login.gif").getImage();

}
//画图片函数
public void paintComponent(Graphics g)
{

g.drawImage(im, 0, 0, 360, 360, this);
}
}



[解决办法]
放一个我正在用的方法,可以在背景图片上放置任何控件。
 

热点排行