public class Test extends JApplet{
private static final long serialVersionUID = 1L; private JLabel test = null; private JPanel panel = null;
private BackGroundPane bg = null; public void init() {
if (panel == null)
panel = new JPanel();
if(bg==null)
bg = new BackGroundPane(new ImageIcon("D:\\Background.jpg"));
panel.setLayout(null);
test = new JLabel("Test");
test.setBounds(10, 10, 100, 100);
this.getContentPane().add(bg);//加背景图Panel
this.getContentPane().add(panel);
panel.add(test);
this.setSize(300, 300);
this.setVisible(true);
}
//这个类是在网上找到抄下来的
class BackGroundPane extends JPanel { private static final long serialVersionUID = 1L;
private Icon bg = null; public Icon getImageIcon() {
return bg;
} public void setImageIcon(Icon imageIcon) {//设置图片
this.bg = imageIcon;
} public BackGroundPane() {
super();
this.setOpaque(false);
}
public BackGroundPane(Icon icon){
super();
this.setOpaque(false);
this.bg = icon;
} protected void paintChildren(Graphics g) {
super.paintChildren(g);
for (int i = 0; i < this.getComponents().length; i++) {
this.getComponent(i).repaint();
}
g.dispose();
} protected void paintComponent(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
super.paintComponent(g2D); if (bg != null) {
g2D.drawImage(((ImageIcon) bg).getImage(), 0, 0, this.getWidth(), this.getHeight(), this);
}
g2D.dispose();
}
}
}-----------------------------------------------
昨天以把空布局和JLabel的坐标以解决,又想加背景图,图加上去了就不能显示标签.
private static final long serialVersionUID = 1L; private JLabel test = null; private JPanel panel = null;
private BackGroundPane bg = null; public void init() {
if (panel == null)
panel = new JPanel();
if(bg==null)
bg = new BackGroundPane(new ImageIcon("D:\\Background.jpg"));
panel.setLayout(null);
test = new JLabel("Test");
test.setBounds(10, 10, 100, 100);
this.getContentPane().add(bg);//加背景图Panel
this.getContentPane().add(panel);
panel.add(test);
this.setSize(300, 300);
this.setVisible(true);
}
//这个类是在网上找到抄下来的
class BackGroundPane extends JPanel { private static final long serialVersionUID = 1L;
private Icon bg = null; public Icon getImageIcon() {
return bg;
} public void setImageIcon(Icon imageIcon) {//设置图片
this.bg = imageIcon;
} public BackGroundPane() {
super();
this.setOpaque(false);
}
public BackGroundPane(Icon icon){
super();
this.setOpaque(false);
this.bg = icon;
} protected void paintChildren(Graphics g) {
super.paintChildren(g);
for (int i = 0; i < this.getComponents().length; i++) {
this.getComponent(i).repaint();
}
g.dispose();
} protected void paintComponent(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
super.paintComponent(g2D); if (bg != null) {
g2D.drawImage(((ImageIcon) bg).getImage(), 0, 0, this.getWidth(), this.getHeight(), this);
}
g2D.dispose();
}
}
}-----------------------------------------------
昨天以把空布局和JLabel的坐标以解决,又想加背景图,图加上去了就不能显示标签.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货