Ⅰ java的jpanel上添加背景圖片後eclipse不顯示背景圖片,但運行後卻看得到
p4=newJPanel(newBorderLayout());
p2=newJPanel(newCardLayout());
//處理p2,p3,p4
p2_lab1=newJLabel(newImageIcon("image/21.jpg"));
p2_lab2=newJLabel(newImageIcon("image/22.jpg"));
//把以上倆加入到P2中
p2.add(p2_lab1,"0");
p2.add(p2_lab2,"1");
this.cardp3=newCardLayout();
p3=newJPanel(this.cardp3);
//先給P3加入一個主界面的卡片
Imagezhu_image=null;
try{
zhu_image=ImageIO.read(newFile("image/11.jpg"));
}catch(Exceptione1){
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}
ImagePanelip=newImagePanel(zhu_image);
p3.add(ip,"0");
//對P3添加幾個JLabel
JLabelrs=newJLabel(newImageIcon("image/renshi.jpg"));
p3.add(rs,"1");
JLabeldrgl=newJLabel(newImageIcon("image/dengru.jpg"));
p3.add(drgl,"2");
//把p2p3裝入P4中
p4.add(p2,"West");
p4.add(p3,"Center");
//做一個拆分窗口分別存放P1P4
jsp1=newJSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,p1,p4);
//指定左邊的面板佔多大像素
jsp1.setDividerLocation(120);
//把邊界線設為沒有
jsp1.setDividerSize(0);
Ⅱ 在java中怎麼樣設置JPanel裡面的背景圖片
java要加背景圖比較麻煩,需要重載paintComponent方法,下面以JPanel為例,其它按此繼承就可
JPanel jContentPane = new getpane(800,600,"images\\0044.jpg");
jContentPane.setLayout(null);
jContentPane.setOpaque(false);//此處要設為false
jContentPane.setAlignmentY(0.5F);
jContentPane.add(jLabel, null);
jContentPane.add(getJTestYear(), null);
this.initComboBox();
jContentPane.add(getJSearchByName(), null);
jContentPane.add(getJSearchByID(), null);
jContentPane.add(getJIDOrName(), null);
jContentPane.add(jLabel1, null);
jContentPane.add(getJPanel(), null);
this.setContentPane(jContentPane);
class getpane extends JPanel{
private static final long serialVersionUID = 1L;
int width=0,hight=0;
String imgpath = "";
public getpane(int width,int hight,String file){
this.width = width;
this.hight = hight;
imgpath = file;
}
protected void paintComponent(Graphics g) {
ImageIcon icon = new ImageIcon(imgpath);
Image img = icon.getImage();
g.drawImage(img, 0, 0, width, hight, this);
}
}
Ⅲ java panel設置背景圖片
要在panel上面加圖片,需要重寫paint方法,那樣就麻煩了,JLabel有現成的設置方法;
JLabel l=new JLabel();
Icon icon=new ImageIcon("D:\\a.jpg"); //在此直接創建對象
l.setIcon(icon);
然後把你原來放在panel上的東西放在JLabel上面,再把JLabel放在panel上面就可以了
Ⅳ JAVA 怎麼在JPanel這個容器中插入背景圖片
要在JPanel中顯示圖片
可以將一JLabel控制項加入其中,在JLabel中添加圖片,如:
JLabel lb = new JLabel(new ImageIcon(".\\xxx\\xx.jpg"));
jPanel1.add(lb);
應該可以,不知是否會答非所問?
package .;
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* 如何在JPanel中顯示圖片
* @author jink2005
*
*/
public class ImagePane extends JPanel
{
JLabel jl = null;
ImageIcon img = null;
/**
* @param args
*/
public ImagePane()
{
img = new ImageIcon("E:\\eclipse\\workspace\\Study\\\\\\horse.jpg");
jl = new JLabel(img);
this.setLayout(new BorderLayout());
this.add(jl, BorderLayout.CENTER);
}
public static void main(String[] args)
{
JFrame test = new JFrame("Image Pane");
test.getContentPane().add(new ImagePane());
test.pack();
test.setVisible(true);
test.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
歡迎訪問我的學習日誌http://jink2005.blog.sohu.com/和我一起交流。
Ⅳ java中jpanel的背景圖片問題
寫一個ImageJPanel類繼承自JPanel,在paintComponent方法中用g.drawImage繪制Image背景圖片就擋不著第一張卡片上的四個按鈕了。
完整的程序如下:(注意把背景圖片的路徑換成你的背景圖片路徑)
importjava.awt.CardLayout;
importjava.awt.Color;
importjava.awt.Graphics;
importjava.awt.GridLayout;
importjava.awt.Image;
importjava.awt.Toolkit;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.JSplitPane;
{
=1L;
JPaneljp1=newJPanel();
JPaneltmpjp1=newJPanel();
JPaneljp2=newJPanel();
JPanelcard_jp1=newJPanel();
JPanelcard_jp2=newJPanel();
JPanelcard_jp3=newJPanel();
JPanelcard_jp4=newJPanel();
JButtonjb1=newJButton("按鈕1");
JButtonjb2=newJButton("按鈕2");
JButtonjb3=newJButton("按鈕3");
JButtonjb4=newJButton("按鈕4");
JSplitPanejsp=newJSplitPane(JSplitPane.HORIZONTAL_SPLIT,jp1,jp2);
CardLayoutcl=newCardLayout();
intj=1;
D(){
tmpjp1.setLayout(newGridLayout(4,1,5,5));
tmpjp1.add(jb1);tmpjp1.add(jb2);tmpjp1.add(jb3);tmpjp1.add(jb4);
jp1.add(tmpjp1);
jp2.setLayout(cl);
JPanelp[]=newJPanel[4];
p[0]=newImageJPanel();
jp2.add(p[0],String.valueOf(0));
for(inti=1;i<4;i++){
p[i]=newJPanel();
jp2.add(p[i],String.valueOf(i));
}
JButtonb[]=newJButton[4];
for(inti=1;i<4;i++){
inta=i+1;
b[i]=newJButton("第"+a+"頁,下一頁");
b[i].addActionListener(this);
p[i].add(b[i]);
}
jsp.setResizeWeight(0.3);
add(jsp);
setSize(800,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
publicstaticvoidmain(String[]args){
newD();
}
@Override
publicvoidactionPerformed(ActionEvente){
cl.show(jp2,String.valueOf(j++));
if(j==4)j=0;
}
classImageJPanelextendsJPanel{
privateImageimage;
JButtoncard_jb1=newJButton("按鈕1");
JButtoncard_jb2=newJButton("按鈕2");
JButtoncard_jb3=newJButton("按鈕3");
JButtonjb=newJButton("第1頁,下一頁");
publicImageJPanel(){
super();
setOpaque(true);
add(card_jb1);
add(card_jb2);
add(card_jb3);
jb.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
cl.show(jp2,String.valueOf(j++));
if(j==4)j=0;
}
});
add(jb);
image=Toolkit.getDefaultToolkit().getImage("C:/tmp/F/3.jpg");//這里換成你的背景圖片地址
}
publicvoidpaintComponent(Graphicsg){
super.paintComponent(g);
setBackground(Color.WHITE);
if(image!=null){
intheight=image.getHeight(this);
intwidth=image.getWidth(this);
if(height!=-1&&height>getHeight())
height=getHeight();
if(width!=-1&&width>getWidth())
width=getWidth();
intx=(int)(((double)(getWidth()-width))/2.0);
inty=(int)(((double)(getHeight()-height))/2.0);
g.drawImage(image,x,y,width,height,this);
}
}
}
}
Ⅵ java中,如果一個jframe中有多個jpanel,怎麼對其中的某個jpanel添加背景圖片
自己創建一個JPanel類
繼承JPanel
然後重寫pain()方法
Image
picture=new
ImageIcon("圖片路徑").getImage();
g.draw(picture,0,0,null);
然後在你的窗體中直接調用你寫JPanel類
在把這個JPanel
的屬性(Opaque)設置為false
即顯示設置為透明
Ⅶ JAVA怎樣讓JLabel透明,顯示出下面的JPanel背景
import javax.swing.*;
import java.awt.*;
public class BackGround extends JFrame {
Container content;
Toolkit toolkit;
JLabel bkLabel = new JLabel(); // 背景圖片標簽
JLabel horseLabel = new JLabel();
JButton bButton;
int screenWidth, screenHeight, WIDTH, HEIGHT;
ImageIcon backImage, bImage;
BackGround() {
content = getContentPane();
toolkit = Toolkit.getDefaultToolkit();
screenWidth = toolkit.getScreenSize().width;
screenHeight = toolkit.getScreenSize().height;
bImage = new ImageIcon("pic/logo.jpg");
//我的笨方法
JLabel image_lab = new JLabel(new ImageIcon("D:\\pic\\logo.jpg"));
//編譯的時候會幫你把文件拷貝到classes目錄下面去
//同時要達到LZ的效果很簡單,兩個Panel。直接覆蓋的話,如果覆蓋面板小於底層面板,當滑鼠移動到底層面板區域。則顯示了,所以建議加上 //setEnabled(false);這樣做LZ你不覺得復雜了嗎?你直接在我的image_lab 上載入組件不行嗎?或者你先都把組件隱藏了,等用戶觸發事件再顯//示也可以啊。
//
backImage = new ImageIcon("pic/background.gif");
bkLabel.setIcon(backImage);
bkLabel.setOpaque(true);
WIDTH = backImage.getIconWidth();
HEIGHT = backImage.getIconHeight();
content.add(bkLabel, -1); // 添加背景圖片
bButton = new JButton(bImage);
JPanel panel2 = new JPanel();
panel2.setOpaque(false); // 設置panel2背景透明
panel2.setPreferredSize(new Dimension(40, 40));
panel2.add(bButton);
content.add(panel2);
validate();
setBounds(300, 200, WIDTH, HEIGHT);
setLocation((screenWidth - WIDTH) / 2, (screenHeight - HEIGHT) / 2);
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args)
{
new BackGround();
}
}
Ⅷ 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 動態改變背景大小
Ⅸ 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"你放到你創建的項目的文件夾里就行 測試成功
Ⅹ java 中的JPanel插入背景圖片和按鈕的問題
重寫JPanel的paintComponent方法:
JPanel jp = new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon img = new ImageIcon("xxx.jpg");
g.drawImage(img.getImage(), 0, 0, null);
}
};