当前位置:首页 » 背景图片 » swing背景图片
扩展阅读
花图片代表什么 2025-01-31 14:44:25
小熊图片可爱卡通 2025-01-31 14:39:11
欧美唯美彩妆图片 2025-01-31 14:36:03

swing背景图片

发布时间: 2023-09-01 13:01:30

‘壹’ java中的swing设计界面时怎么加上背景图片。而不覆盖什么label等控件

通过以下方式设置下背景就可以了:

background=newImageIcon("images/backImage.png");

backImage拦码=newJLabel(background);

backImage.setBounds(0,0,background.getIconWidth(),

background.getIconHeight());

backPanel=(JPanel)this.getContentPane();

backPanel.setOpaque(false);

this.getLayeredPane().setLayout(null);

this.getLayeredPane().add(backImage,简滚哪newInteger(Integer.MIN_VALUE));

backPanel.setLayout(newBorderLayout());

这样就可以把图片备漏放在最下面了,效果图:

‘贰’ 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();
}
});
}
}


‘叁’ 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 动态改变背景大小