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 動態改變背景大小