A. java窗体如何添加背景图片
B. java swing换背景图片
//回答完毕,采纳即可
importjava.awt.BorderLayout;
importjava.awt.Graphics;
importjava.awt.Image;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjava.io.IOException;
importjavax.imageio.ImageIO;
importjavax.swing.BorderFactory;
importjavax.swing.JButton;
importjavax.swing.JComboBox;
importjavax.swing.JDialog;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
publicclassBarextendsJPanel
{
=1L;
privateJButtonopen;
privatestaticfinalintW=300;
privatestaticfinalintH=300;
publicBar()
{
super(newBorderLayout());
JPanelpanel=newJPanel();
open=newJButton("打开");
open.addActionListener(newActionListener()
{
@Override
publicvoidactionPerformed(ActionEvente)
{
JDialogdialog=newJDialog();
dialog.setTitle("设置背景图");
dialog.setSize(160,60);
dialog.setModal(true);
dialog.setLocationRelativeTo(Bar.this);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
finalJComboBoxcomboBox=newJComboBox(newString[]{"图片a","图片b"});
dialog.addWindowListener(newWindowAdapter()
{
@Override
publicvoidwindowClosing(WindowEvente)
{
try
{
intindex=comboBox.getSelectedIndex();
switch(index)
{
case0:
img=ImageIO.read(Bar.class.getResource("a.jpg"));
Bar.this.repaint();
break;
case1:
img=ImageIO.read(Bar.class.getResource("layout.png"));
Bar.this.repaint();
break;
default:
break;
}
}
catch(IOExceptione1)
{
e1.printStackTrace();
}
}
});
dialog.add(comboBox);
dialog.setVisible(true);
}
});
panel.add(open);
add(panel,BorderLayout.PAGE_START);
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}
Imageimg=null;
@Override
publicvoidpaint(Graphicsg)
{
super.paint(g);
g.clearRect(0,0,W,H);
g.drawImage(img,0,0,null);
g.dispose();
}
()
{
JFrameframe=newJFrame("bar");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Barb=newBar();
b.setOpaque(true);
frame.setContentPane(b);
frame.setSize(W,H);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
publicstaticvoidmain(String[]args)
{
javax.swing.SwingUtilities.invokeLater(newRunnable()
{
publicvoidrun()
{
createAndShowGUI();
}
});
}
}
C. 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 动态改变背景大小