① 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中显示动态图片
试了一下,从网上找了个GIF
可以用啊
--------------------------------------------------------------------------------------------
importjava.awt.Graphics;
importjavax.swing.ImageIcon;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
{
publicImageApp(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400,300);
setResizable(false);
getContentPane().setLayout(null);
JPanelpanel=newImagePanel();
panel.setBounds(0,0,400,300);
getContentPane().add(panel);
setVisible(true);
}
publicstaticvoidmain(String[]args){
newImageApp();
}
classImagePanelextendsJPanel{
publicvoidpaint(Graphicsg){
super.paint(g);
//ImageIconicon=newImageIcon("D:\1.jpg");
ImageIconicon=newImageIcon("D:\14405937jqhjsppeninjf9.gif");
g.drawImage(icon.getImage(),0,0,400,300,this);
}
}
}
③ 怎么在Java里添加背景图片
可以新建个面板,在面板里放入带图片的JLabel,填满面板即可。
JPanel jp = new JPanel(); //新建面板
jp.setLayout(new FlowLayout()); //设置面板布局
ImageIcon ii=new ImageIcon(getClass().getResource("/Picture/i.jpg"));
JLabel uppicture=new JLabel(ii); //往面板里加入JLabel
this.setVisible(true);