A. 怎么为Java程序添加背景图片代码
仅仅是给窗口添加背景的话是很简单的,添加上以下语句(自己去添加变量哈):x0dx0ax0dx0alabel = new JLabel(background); //background为ImageIconx0dx0a// 把标签的大小位置设置为图片刚好填充整个面板 x0dx0alabel.setBounds(0, 0, this.getWidth(), this.getHeight());x0dx0a//添加图片到frame的第二层(把背景图片添加到分层窗格的最底层作为背景)x0dx0athis.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));x0dx0a//把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明x0dx0ajPanel=(JPanel)this.getContentPane();x0dx0a//设置透明x0dx0ajPanel.setOpaque(false);x0dx0ax0dx0a然后你上面那个JPanel p也设置成透明就可以了
B. java 窗体中添加背景图片的方法
import java.awt.*;
import java.net.URL;
import javax.swing.*;
import java.awt.Dimension;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import java.awt.event.*;
public class Example1 extends JFrame{
public static void main(String[] args){
JFrame jf=new JFrame("系统登录");
JPanel jpanel = new JPanel();
jf.setContentPane(jpanel);
//设置窗口在屏幕中居中显示
double width = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
double height = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
int x=(int)(width-300)/2;
int y=(int)(height-180)/2;
jf.setBounds(x,y,300,180);
//添加标签组件
GridLayout gird = new GridLayout(3,0);
JPanel jpanel1=new JPanel();
JPanel jpanel2=new JPanel();
JPanel jpanel3=new JPanel();
jpanel.setLayout(gird);
jpanel.add(jpanel1);
jpanel.add(jpanel2);
jpanel.add(jpanel3);
ImageIcon img = new ImageIcon("111.png");
JLabel imgLabel = new JLabel(img);//将背景图放在标签里。
jf.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));
imgLabel.setBounds(0,0,img.getIconWidth(), img.getIconHeight());
JLabel name=new JLabel("姓 名:");
JTextField nametext=new JTextField(10);
JLabel pwd=new JLabel("密 码:");
JPasswordField pwd1=new JPasswordField(10);
JButton login=new JButton("登录");
JButton exit=new JButton("退出");
jpanel1.add(name);
jpanel1.add(nametext);
jpanel2.add(pwd);
jpanel2.add(pwd1);
jpanel3.add(login);
jpanel3.add(exit);
jf.getLayeredPane().setLayout(null);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jpanel.setOpaque(false);
jpanel1.setOpaque(false);
jpanel2.setOpaque(false);
jpanel3.setOpaque(false);
jf.setVisible(true);
}
}
那个图片 "111.png"你放到你创建的项目的文件夹里就行 测试成功
C. java怎样在登陆界面添加背景图片最好有代码及详解,先谢谢了各位大神!!!
有两种方式,一个是用CSS层叠样式表设定,第二个就是在<div>里面写,代码我这也有一些,我给你,在CSS文件中写body {
background:#fff;写上要用的背景图片url,一般设一个包中,写入路径
font-size:12px;
color:#000;
font-family:"宋体";
height:100%;
}
D. java中swing界面上怎么给jpanel上添加背景图片
//重写JPanel的这个方法就可以了
@Override
publicvoidpaintComponent(Graphicsgs){
Graphics2Dg=(Graphics2D)gs;
super.paintComponent(g);
//画背景图片
Imageimage=Toolkit.getDefaultToolkit().getImage(getClass().getResource(imgPath));
g.drawImage(image,0,0,width,height,this);
}
一个完整的代码:
packagetest;
importjava.awt.Graphics;
importjava.awt.Image;
importjava.net.URL;
importjavax.swing.ImageIcon;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
//为窗口添加背景图片
publicclassJFrameBackground{
privateJFrameframe=newJFrame("带背景图片的JFrame");
privateJPanelimagePanel;
privateImageIconbackgroundimg;
publicJFrameBackground(){
imagePanel=newJPanel(){
@Override
protectedvoidpaintComponent(Graphicsg){
super.paintComponent(g);
URLlocation=this.getClass().getResource("grapes.gif");
backgroundimg=newImageIcon(location);
Imageimg=backgroundimg.getImage();
g.drawImage(img,0,0,backgroundimg.getIconWidth(),
backgroundimg.getIconHeight(),
backgroundimg.getImageObserver());
frame.setSize(backgroundimg.getIconWidth(),
backgroundimg.getIconHeight());
}
};
frame.add(imagePanel);
frame.setVisible(true);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
publicstaticvoidmain(String[]args){
newJFrameBackground();
}
}
这是运行的一个截图:
参考网址:
http://www.linuxidc.com/Linux/2011-08/41689.htm 两种设置背景的方法
http://blog.csdn.net/one_and_only4711/article/details/6594770 动态改变背景大小