当前位置: 代码迷 >> Java相关 >> java添加背景,帮帮忙
  详细解决方案

java添加背景,帮帮忙

热度:94   发布时间:2011-12-06 20:12:39.0
java添加背景,帮帮忙
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class qq<BackgroundPanel> extends JFrame {
    private JPanel jPanel1;
    private JPanel jPanel2;
    private JPanel jPanel3;
    private ImageIcon background;
    public static void main(String[] args) {
       qq text=new qq();
       text.setLocationRelativeTo(null);
       text.setVisible(true);
    }
    private qq()
    {
        super();
        init();
    }
    private void init(){
    try{
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        {
            jPanel1 = new JPanel();
             getContentPane().add(jPanel1, BorderLayout.CENTER);
             jPanel1.setLayout(null);
             {
                // background=new ImageIcon("picture/1.png");
                 jPanel2=new JPanel(){
                 public void paintComponents(Graphics g) {
                     Image img = new ImageIcon("2.jpg").getImage();
                     super.paintComponent(g);
                      g.drawImage(img,0,0,this);
                 }
                 };
                 jPanel1.add(jPanel2);
                // jPanel2.setBackground(java.awt.Color.BLUE);
                 jPanel2.setBounds(0, 0, 300, 75);
             }
        }
        pack();
        setSize(300, 620);
    }catch (Exception e) {
        e.printStackTrace();
       }
    }
}
没有背景啊!!!高手帮忙啊
搜索更多相关的解决方案: class  background  private  public  

----------------解决方案--------------------------------------------------------
// jPanel2.setBackground(java.awt.Color.BLUE);
你自己把它注释了吧
----------------解决方案--------------------------------------------------------
你试一下用Image img = new ImageIcon(getClass().getResource("2.jpg")).getImage();

----------------解决方案--------------------------------------------------------
  相关解决方案