当前位置:首页 » 背景图片 » javajpanel背景图片
扩展阅读
电脑截图图片怎么一样大 2024-11-16 14:47:01
ps图片立体光 2024-11-16 14:40:30

javajpanel背景图片

发布时间: 2022-05-15 07:17:42

Ⅰ 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);
}
};