Oracle Java程序添加背景圖片的操作方案
以下的文章主要是對Oracle Java的相關(guān)的實際應(yīng)用程序添加漂亮的背景圖片的實際操作的描述,其在整理之后可執(zhí)行的相關(guān)實際應(yīng)用代碼如下:希望本文章會給你在其實際應(yīng)用方面帶來一些幫助在此方面。
- import java.awt.*;
 - import javax.swing.*;
 - public class TestBackgroundColor extends JFrame
 - {
 - public static void main(String[] args)
 - {
 - TODO Auto-generated method stub
 - TestBackgroundColor tbc = new TestBackgroundColor();
 - tbc.setVisible(true);
 - }
 - private JPanel imagePanel;
 - private ImageIcon background;
 - public TestBackgroundColor()
 - {
 - background = new ImageIcon("漸變背景14.png");
 
背景圖片
- JLabel label = new JLabel(background);
 
把背景圖片顯示在一個標(biāo)簽里面
把標(biāo)簽的大小位置設(shè)置為圖片剛好填充整個面板
- label.setBounds(0,0,background.getIconWidth(),background.getIconHeight());
 
把內(nèi)容窗格轉(zhuǎn)化為JPanel,否則不能用方法setOpaque()來使內(nèi)容窗格透明
- imagePanel = (JPanel)this.getContentPane();
 - imagePanel.setOpaque(false);
 
內(nèi)容窗格默認(rèn)的布局管理器為BorderLayout
- imagePanel.setLayout(new FlowLayout());
 - imagePanel.add(new JButton("測試按鈕"));
 - this.getLayeredPane().setLayout(null);
 
把背景圖片添加到分層窗格的最底層作為背景
- this.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
 - this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 - this.setSize(background.getIconWidth(),background.getIconHeight());
 - this.setVisible(true);
 - }
 - }
 
以上的相關(guān)內(nèi)容就是對為Oracle Java程序添加漂亮背景圖片的介紹,望你能有所收獲。
【編輯推薦】















 
 
 


 
 
 
 