JButton JLabel 上能只设置图片,不要文字
方法是
  setText()方法

解决方案 »

  1.   

    和jButton1.setIcon(new ImageIcon(new java.net.URL("file:///D:/Documents and Settings/My Documents/18.gif")));
      

  2.   

    重载paint()方法,我以前贴过怎么写
      

  3.   

    能够加载背景图了
    但是出现新问题,加了背景图后,原先定位在JFrame上的控件不能显示,比如JButton JLabel----JButton要在上面点击一下才能显示出来,为什么??
      

  4.   

    请看代码:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class applet3  extends JFrame 
    {
    Image img1;
    MediaTracker mt;
    Container contentPanel=this.getContentPane();public applet3() {
    this.setSize(400,300);
    this.setResizable(false);
    this.setLocation(this.getToolkit().getScreenSize().width/2-this.getWidth()/2,this.getToolkit().getScreenSize().height/2-this.getHeight()/2);


    img1=this.getToolkit().getImage("1.jpg");
    mt=new MediaTracker(this);
    mt.addImage(img1,0);
    ImageIcon icon=new ImageIcon("4.jpg");
    contentPanel.setLayout(null);
      JButton button=new JButton(icon);
      JLabel label=new JLabel(icon);
      button.setBounds(0,0,80,30);
      label.setBounds(90,90,80,50);
      contentPanel.add(label);
      contentPanel.add(button);

    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    this.setVisible(true);
    }public void paint(Graphics g) {
     super.paint(g);
              g.drawImage(img1,0,0,this.getWidth(),this.getHeight(),this);
        
    }public static void main(String args[]) {
        new applet3();
    }
    }出现的效果是:先看到两个控件button label,然后出现背景图片,背景图片将控件覆盖,就只能看见背景图片???不知道怎么解决,还请大虾门多帮忙!!~~~
      

  5.   


    paint()里顺序写反了,应该是
    public void paint(Graphics g) {
              g.drawImage(img1,0,0,this.getWidth(),this.getHeight(),this);
              super.paint(g);
    }
    而且我建议你重载JPanel而不是JFrame,因为JFrame的paint()可能比较复杂,如果其中又重写了背景,那你的drawimage就给覆盖回去了,而JPanel我做过,没有问题
      

  6.   

    谢谢  Apocalypse(逍遥思辨) 拉~!解决了
    代码如下:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;public class JPanelTemp extends JFrame
    {
    public JPanelTemp()
    {
    Container con=this.getContentPane();
    con.setLayout(null);
    ImagePanel panelTemp=new ImagePanel("chess.jpg");
    panelTemp.setBounds(100,0,600,500);
    con.add(panelTemp);
    this.addWindowListener (new WindowAdapter(){
    public void windowClosing(WindowEvent e){
    dispose();
    System.exit(0);
    }
    });
    }

    public static void main(String args[])
    {
    System.out.println("Starting App");
    JPanelTemp f = new JPanelTemp();
    f.setBounds(0,0,800,600);
    f.show();
    }
    }class ImagePanel extends JPanel implements ActionListener
    {
        private String strPicName;
        ImageIcon white=new ImageIcon("white.jpg");
        public ImagePanel(String strName)
        {
            strPicName = strName;
         this.setLayout(null);
         
         JButton button=new JButton(white);
         //JLabel label=new JLabel();
         button.setBounds(0,0,30,20);
         button.setFocusPainted(false); 
        button.setBorderPainted(false); 
        button.setContentAreaFilled(false); 
        button.setMargin(new Insets(0,0,0,0));
         //label.setBounds(120,80,80,50);
         //label.addActionListener(this);
         add(button);
         //add(label);
        }
        
        public void paintComponent(Graphics g)
        {
            loadPic(strPicName,g);
        }
        
          public void loadPic(String picName,Graphics g)
         {
              ImageIcon img=new ImageIcon(picName);
              int pWidth=this.getWidth();
              int pHeight=this.getHeight();
              int mWidth=img.getIconWidth() ;
              int mHeight=img.getIconHeight() ;
              int x,y;
              int width,height;
              if((mWidth<=pWidth)&&(mHeight<=pHeight)){
                 x=(pWidth-mWidth)/2;    y=(pHeight-mHeight)/2;
                 width=mWidth;   height=mHeight;
              }else {
                 width=pWidth;   height=pHeight;
                float widthScale=(float)pWidth/(float)mWidth;            float heightScale=(float)pHeight/(float)mHeight;
                if(widthScale<heightScale)
                    height=(int)(mHeight*widthScale);
                else
                    width=(int)(mWidth*heightScale);
                 x=(pWidth-width)/2;
                 y=(pHeight-height)/2;
              }
             //statusBar.setText("文件:"+ this +"    大小:"+mWidth+"X"+mHeight);
             g.clearRect(0,0,pWidth,pHeight);
             g.drawImage(img.getImage(),x,y,width,height,this);
        }
        public void actionPerformed(ActionEvent e)
        {
         //if(e.getSource()==label)
        {
        
        }
        }
    }