java jframe 设置背景图片_JFrame如何设置背景图片
代碼:
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class JFrameBackground {
private JFrame frame = new JFrame("背景圖片測(cè)試");
private JPanel imagePanel;
private ImageIcon background;
public static void main(String[] args) {
new JFrameBackground();
}
public JFrameBackground() {
background = new ImageIcon("003.jpg");// 背景圖片
JLabel label = new JLabel(background);// 把背景圖片顯示在一個(gè)標(biāo)簽里面
// 把標(biāo)簽的大小位置設(shè)置為圖片剛好填充整個(gè)面板
label.setBounds(0, 0, background.getIconWidth(),
background.getIconHeight());
// 把內(nèi)容窗格轉(zhuǎn)化為JPanel,否則不能用方法setOpaque()來(lái)使內(nèi)容窗格透明
imagePanel = (JPanel) frame.getContentPane();
imagePanel.setOpaque(false);
// 內(nèi)容窗格默認(rèn)的布局管理器為BorderLayout
imagePanel.setLayout(new FlowLayout());
imagePanel.add(new JButton("測(cè)試按鈕"));
frame.getLayeredPane().setLayout(null);
// 把背景圖片添加到分層窗格的最底層作為背景
frame.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(background.getIconWidth(), background.getIconHeight());
frame.setResizable(false);
frame.setVisible(true);
}
}
總結(jié)
以上是生活随笔為你收集整理的java jframe 设置背景图片_JFrame如何设置背景图片的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: php filespl,PHP SPL-
- 下一篇: win7 mysql添加到服务_MySQ