可以利用標簽組件來設置。
創新互聯公司2013年開創至今,是專業互聯網技術服務公司,擁有項目成都做網站、網站建設網站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元綏棱做網站,已為上家服務,為綏棱各地企業和個人服務,聯系電話:18980820575
具體方法如下:
利用標簽組件來設置,具體代碼如下:
JPanelpnlMain=new JPanel(); //創建面板pnlMain。
getContentPane().add(pnlMain); //將pnlMain設置為主面板。
Iconi=new ImageIcon("背景.jpg"); /*用源圖片“背景.jpg”構造一個ImageIcon對象i,需要注意如果圖片的路徑使用的是相對路徑,則圖片文件必須放在類文件所在文件夾或項目的根文件夾中,否則圖片的路徑必須用絕對路徑。*/
JLabellblLogo = new JLabel(i); //用指定的圖片構造標簽對象lb
this.getLayeredPane().add(lb, new Integer(Integer.MIN_VALUE));
//把標簽放在第二層JlayerPane上。
lb.setBounds(0, 0,ii.getIconWidth(),i.getIconHeight());
//設置標簽的尺寸,即背景圖象的大小。
getConentPane().setOpaque(false); /*把內容面板設置為透明,這樣整個框架的背景就不再是內容面板的背景色,而是第二層中標簽的圖像。*/
pnlMain.add(lb); //將標簽添加到主面板pnlMain中。
僅僅是給窗口添加背景的話是很簡單的,添加上以下語句(自己去添加變量哈):
label = new JLabel(background); //background為ImageIcon
// 把標簽的大小位置設置為圖片剛好填充整個面板
label.setBounds(0, 0, this.getWidth(), this.getHeight());
//添加圖片到frame的第二層(把背景圖片添加到分層窗格的最底層作為背景)
this.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
//把內容窗格轉化為JPanel,否則不能用方法setOpaque()來使內容窗格透明
jPanel=(JPanel)this.getContentPane();
//設置透明
jPanel.setOpaque(false);
然后你上面那個JPanel p也設置成透明就可以了
這是我以前的一個小代碼你可以看看
public class demo_9 extends JFrame {
JSplitPane jsp = null;
JList jlist;
JLabel jlabel;
public static void main(String[] args) {
demo_9 a = new demo_9();
}
public demo_9(){
String []words ={"boy","girl"};
JList jlist = new JList(words);
jlabel = new JLabel(new ImageIcon("Image//真三.gif")); //這里就是引入圖片了
//拆分窗格
jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jlist,jlabel);
jsp.setDividerLocation(70);
//設置可以伸縮
jsp.setOneTouchExpandable(true);
this.add(jsp);
this.setTitle("test");
this.setSize(400,300);
this.setLocation(400,200);
this.setVisible(true);
}
}
步驟:首先先在project里新建個文件夾(Folder),然后把你要插入的圖片復制黏貼到這個文件夾里面。
例如我那個引入的圖片代碼:jlabel = new JLabel(new ImageIcon("Image//真三.gif"));
我new一個folder叫Image,圖片名稱叫"真三.gif"
//?不加包,圖片跟類文件在一個目錄,命令行下編譯執行就行了
//?如果建工程,圖片放到工程根目錄
import?java.awt.Container;
import?java.awt.FlowLayout;
import?javax.swing.ImageIcon;
import?javax.swing.JFrame;
import?javax.swing.JLabel;
import?javax.swing.JPanel;
public?class?Student?extends?JFrame?{
public?Student()?{
init();
}
private?void?init()?{
final?Container?c?=?getContentPane();
final?JLabel?imgLabel?=?new?JLabel();
ImageIcon?img?=?new?ImageIcon(System.getProperty("user.dir")?+?"\\a.png");
imgLabel.setIcon(img);
imgLabel.setBounds(0,?0,?img.getIconWidth(),?img.getIconHeight());
((JPanel)getContentPane()).setOpaque(false);
getLayeredPane().add(imgLabel,?new?Integer(Integer.MIN_VALUE));
setLayout(new?FlowLayout());
setSize(500,?500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public?static?void?main(String[]?args)?{
new?Student().setVisible(true);
}
}
在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();
}
}
用Swing包下的ImageIcon類就可以實現,比如在一個按鈕中添加一張圖片,就可以用以下代碼實現:ImageIcon imageicon =new ImageIcon(String s);JButton b=new JButton(imageicon); 其中參數s是所要添加圖片的路徑(絕對路徑或相對路徑)和名字。如想添加D盤下的圖片1.jpg,就可以將上面改成:ImageIcon imageicon =new ImageIcon("D:\1.jpg");