我要做一个applet程序,需要在指定的BorderLayout中加入背景和按钮组件。我用add(背景图片,BorderLayout.EAST,-1);add(按钮,BorderLayout.EAST,0); 这种方法根本加不上去,按钮把背景图完全的覆盖了(虽然按钮比背景图小很多)。试了很多的方法都不好用。希望高手能帮助小弟一下。在这里万分的感谢!最好可以给我一个简简单单的实例,不要太复杂! 
(谢谢各位,最好不要说让我去看书什么之类的话,如果我有一个月的时间我就会去看了)

解决方案 »

  1.   

    ImageIcon icon= new ImageIcon("fileName");
    button.setIcon(icon);
      

  2.   

    没为按钮加过背景,只做过JPanel的JPanel topPanel = new JPanel(){            public void paint(Graphics g){
                  super.paint(g);
                  //下面是取得你的背景图片,你根据你自己的要求来取得这个图片,不管什么方法,得到就OK
                  ImageIcon  icon = new ImageIcon(ClassLoader.getSystemResource("com/res/xxx.jpg"));
                  icon.paintIcon(this, g, 0, 0);
               }
            };你可以照着试下,看可以不,要是可以别忘了告诉我
      

  3.   

    给你个思路
    把背景放到一个JPanel里,JPanel的Layout设为FlowLayout
    把Button放到这个JPanel里再把JPanel放到指定层为BorderLayout的那个上层容器里,这样背景图和按钮就不会互相打架了。
      

  4.   

    package jframe;import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.swing.BoxLayout;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;public class FrameTest extends JFrame implements ActionListener {    private JPanel j;    private JButton b = new JButton();    public FrameTest(String title) {
            super.setTitle(title);
            this.setSize(new Dimension(500, 300));
            this.setLocation(200, 200);
            this.setLayout(new BorderLayout());        j = new JPanel() {
                public void paint(Graphics g) {
                    super.paint(g);
                    // 下面是取得你的背景图片,你根据你自己的要求来取得这个图片,不管什么方法,得到就OK
                    ImageIcon icon = new ImageIcon(ClassLoader.getSystemResource("jframe\\xxx.jpg"));
                    icon.paintIcon(this, g, 0, 0);
                    // 下面是按钮的,如果放到外面一拖放就被jpanel刷掉啦(一定要写包名,图片自己换吧)
                    ImageIcon icon2 = new ImageIcon(ClassLoader.getSystemResource("jframe\\x2.jpg"));
                    b.setIcon(icon2);
                }
            };
            
            b.setSize(new Dimension(50, 25));
            b.setText("click");
            j.setSize(new Dimension(300, 200));
            j.setLayout(new BoxLayout(j,BoxLayout.Y_AXIS));
            j.add(b);
            this.add(j, BorderLayout.CENTER);
            this.setVisible(true);
            
            this.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e){
                    System.exit(0);
                }
            });
        }    @Override
        public void actionPerformed(ActionEvent e) {    }    public static void main(String[] args) {
            FrameTest ft = new FrameTest("FrameTest");
        }
    }
      

  5.   

    Image image = Toolkit.getDefaultToolkit().getImage(
    "image/mrv_bgimg.png");
    Icon icon = new ImageIcon(image);JButton b=new JButton();
    b.setIcon(icon);   //这样就可以显示出你的效果了!
      

  6.   

    3楼思路是对的,问题是要设置背景,重载JButton的paint方法吧