僅僅是給窗口添加背景的話是很簡單的,添加上以下語句(自己去添加變量哈):\x0d\x0a\x0d\x0alabel = new JLabel(background); //background為ImageIcon\x0d\x0a// 把標簽的大小位置設(shè)置為圖片剛好填充整個面板 \x0d\x0alabel.setBounds(0, 0, this.getWidth(), this.getHeight());\x0d\x0a//添加圖片到frame的第二層(把背景圖片添加到分層窗格的最底層作為背景)\x0d\x0athis.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));\x0d\x0a//把內(nèi)容窗格轉(zhuǎn)化為JPanel,否則不能用方法setOpaque()來使內(nèi)容窗格透明\x0d\x0ajPanel=(JPanel)this.getContentPane();\x0d\x0a//設(shè)置透明\x0d\x0ajPanel.setOpaque(false);\x0d\x0a\x0d\x0a然后你上面那個JPanel p也設(shè)置成透明就可以了
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比富陽網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式富陽網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務覆蓋富陽地區(qū)。費用合理售后完善,十余年實體公司更值得信賴。
在java swing中需要為容器添加圖片,或者背景圖片。
提供兩種簡單的解決方案,一種利用JPanel,另一種利用JLabel
1.JPanel(源代碼)
package oo;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Drawing {
JFrame jframe = new JFrame();
public static JPanel GImage = null;
public Drawing() {
initFrame();
}
// 初始化窗口
public void initFrame() {
// 利用JPanel添加背景圖片
GImage = new JPanel() {
protected void paintComponent(Graphics g) {
ImageIcon icon = new ImageIcon("image\\benbenla.jpg");
Image img = icon.getImage();
g.drawImage(img, 0, 0, icon.getIconWidth(),
icon.getIconHeight(), icon.getImageObserver());
jframe.setSize(icon.getIconWidth(), icon.getIconHeight());
}
};
jframe.setTitle("測試背景圖片");
jframe.add(GImage);
jframe.pack();
jframe.setVisible(true);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Drawing();
}
}
2.JLabel源代碼
package swing.draw;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Drawing2 {
JLabel jlpic = new JLabel();
JFrame jframe = new JFrame();
public Drawing2() {
init1Frame();
}
public void init1Frame() {
ImageIcon icon = new ImageIcon("image\\benbenla.jpg");
icon.setImage(icon.getImage().getScaledInstance(icon.getIconWidth(),
icon.getIconHeight(), Image.SCALE_DEFAULT));
System.out.println(icon.getIconHeight() + "" + icon.getIconWidth());
jlpic.setBounds(0, 0, 1366, 768);
jlpic.setHorizontalAlignment(0);
jlpic.setIcon(icon);
jframe.setSize(1366, 768);
jframe.add(jlpic);
jframe.pack();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
}
public static void main(String args[]) {
new Drawing2();
}
}
以下的代碼就可以,lz自己在*的地方填入星星的圖片就可以了
body bgcolor=#ee3300
style
.drop { position: absolute; width: 3; filter: flipV(), flipH(); font-size: 40; color: #ffffff }
/style
script language="javascript"
snow = false; // false-rain; true-snow
snowsym = " * " are the symbols for each
rainsym = " * " can put images here.
howmany = 20 many drops/snowflakes?
/**************Do not need to change anything below***********/
if(snow){sym = snowsym; speed=1; angle=10; drops=howmany}
else{sym = rainsym; speed=30; drops=howmany; angle=6}
movex = -speed/angle; movey = speed; count = 0;
function moverain(){
for(move = 0; move drops; move++){
xx[move]+=movex; yy[move]+=mv[move];
hmm = Math.round(Math.random()*1);
if(xx[move] 0){xx[move] = maxx+10;}
if(yy[move] maxy){yy[move] = 10;}
drop[move].left = xx[move]
drop[move].top = yy[move]+document.body.scrollTop;
}setTimeout('moverain()','1')}
/script
script language="javascript"
if (document.all){
drop = new Array(); xx = new Array(); yy = new Array(); mv = new Array()
ly = "document.all[\'"; st = "\'].style"
for(make = 0; make drops; make++){
document.write('div id="drop'+make+'" class=drop'+sym+'/div');
drop[make] = eval(ly+'drop'+make+st);
maxx = document.body.clientWidth-40
maxy = document.body.clientHeight-40
xx[make] = Math.random()*maxx;
yy[make] = -100-Math.random()*maxy;
drop[make].left = xx[make]
drop[make].top = yy[make]
mv[make] = (Math.random()*5)+speed/16;
drop[make].fontSize = (Math.random()*10)+20;
if(snow){col = 'white'}else{col = 'white'}
drop[make].color = col;
}
window.onload=moverain
}
/script
/body