當前位置:首頁 » 背景圖片 » swing設置背景圖片
擴展閱讀
雞兒圖片大全圖片圖片 2025-01-23 09:22:00
橡皮章圖片素材哈利波特 2025-01-23 09:21:57
春天有什麼茶葉圖片 2025-01-23 09:19:49

swing設置背景圖片

發布時間: 2023-09-26 06:52:09

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 動態改變背景大小