import java.awt.*;
import javax.swing.*;
public class TestBackgroundColor extends JFrame {
public static void main(String[] args) {
TestBackgroundColor tbc = new TestBackgroundColor();
tbc.setVisible(true);
}
private JPanel imagePanel;
private ImageIcon background;
public TestBackgroundColor() {
background = new ImageIcon("C:\\Users\\Administrator\\Documents\\
My FTPRush Downloads\\项ç®\\Photoes\\007.jpg");//èæ¯å¾ç
JLabel label = new JLabel(background);//æèæ¯å¾çæ¾ç¤ºå¨ä¸ä¸ªæ ç¾éé¢
//ææ ç¾ç大å°ä½ç½®è®¾ç½®ä¸ºå¾çå好填å æ´ä¸ªé¢æ¿
label.setBounds(0,0,background.getIconWidth(),background.getIconHeight());
//æå 容çªæ ¼è½¬å为JPanelï¼å¦åä¸è½ç¨æ¹æ³setOpaque()æ¥ä½¿å 容çªæ ¼éæ
imagePanel = (JPanel)this.getContentPane();
imagePanel.setOpaque(false);
//å 容çªæ ¼é»è®¤çå¸å±ç®¡çå¨ä¸º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);
}
}
å®ç°ææï¼
代码的核心是组件全放一个JPanel里面,然后覆盖paintComponent()、同时绘背景图片。
用MyEclipse能不能像拖拽的组件设置属性一样快速设置背景图片呢?
追答日前 没有,自己重写一个JFrame 或 JPanel,就方便了。
本回答被提问者采纳