在一个界面上添加了按钮
想在按钮上插入图片
但是图片却不能显示
望高手们指教!!!
谢谢~~~~~~~

解决方案 »

  1.   

    请先看看用JBuilder生成的代码
    package 图书馆管理系统1;import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.Graphics;
    import javax.swing.ImageIcon;
    import javax.swing.Icon;
    import com.borland.jbcl.layout.*;
    public class Frame1 extends JFrame {  ImageIcon img;
      ImageIcon img2;
      JButton a=new JButton();
      //Construct the frame
      public Frame1() {
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);    try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      //Component initialization
      private void jbInit() throws Exception  {
        img = new ImageIcon("123.jpg");
        img2= new ImageIcon("555.jpg");
        a.setBounds(new Rectangle(577, 273, 42, 95));
        a.setIcon(img2);
        a.setFont(new java.awt.Font("Serif", 1, 20));
        a.setForeground(Color.red);
        a.setText("guanglichu");
        JPanel panel = new JPanel() {
            public void paintComponent(Graphics g) {
            g.drawImage(img.getImage(), 0, 0, null);
             super.paintComponent(g);
            }
         };        panel.setDebugGraphicsOptions(0);
            panel.setOpaque(false);
           panel.setLayout(null);
           setContentPane(panel);
            this.setResizable(false);
           this.setSize(700,525);
           panel.add(a);
       a.validate();
      this.getContentPane().add(panel,null);    this.setTitle("图书馆欢迎你");
       this.setVisible(true);
      }
      //Overridden so we can exit when window is closed
      protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
          System.exit(0);
        }
      }
    }
    很奇怪的是,在按钮上能插入图片“123.jpg”但是却不能插入图片“555.jpg”
      

  2.   

    package net.xiaohai;import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;/**
     * @author haihai
     * 
     */
    public class TestMain extends JFrame  {
    private JButton b=null;
    private JPanel panel=null;
    private ImageIcon i=null;
    public TestMain() {
    super("带图片按钮");
    b=new JButton("button",new ImageIcon("E:\\1006.jpg"));//图片绝对路径
    panel=new JPanel();
    panel.add(b);
    this.getContentPane().add(panel);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(300, 250);
    this.setLocationRelativeTo(null);
    this.setVisible(true);
    } public static void main(String[] args) {
    new TestMain();
    }
    }
      

  3.   

    结果得出了
    应该是我的图片555.jpg问题
    不过还是谢谢大家
      

  4.   

    换成其他图片的话就可以在按钮上显示
    我的那张图是用QQ本身的那个截图工具截得的,而且又比较小,然后我把它转换为jpg格式
    但是我用其他截图工具截的同样一副图(尺寸大一点),却能显示。
    所以我想是图片问题