2013-11-24 36 views
0

我使用NetBeans IDE创建使用java的应用程序。我想包含一个JButtonJLabel和图像。实际上,当我在JLabel上添加JButton时,它会插入,但它会透明,名称和按钮不显示。它似乎是在JLabel下添加的按钮。我怎样才能克服这个问题?如何将JButton放置在带图像的JLabel上?

+1

那么,你真的想在这里完成什么?在Swing中,分层组件并不那么容易。有关更多信息,请参阅[如何使用分层窗格](http://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html)。 – mre

+2

**第一**,告诉我们你已经尝试了什么**第二**制作描述你的需求的图像,上传到一些图像托管网站,并将它链接到你的问题。 – Sage

+0

如果你想保持简单,我使用JLayeredPane :) –

回答

4

有两种方式

通过覆盖 paintComponent(标准方式)

  • JLabel

    1. 放图像JPanel还没有API的任何LayoutManager,你必须设置LayoutManager,然后JLabel会货柜

  • +0

    +1的建议。我认为OP面临的问题是缺少'JLabel'的布局管理器。 – dic19

    0
    ImageIcon img = null; 
    class Panel extends JPanel{ 
        @Override 
        protected void paintComponent(Graphics g) { 
         super.paintComponent(g); 
         if (img == null){ 
          img = new ImageIcon("d:\\picture.jpg"); 
          g.drawImage(img.getImage(), 0, 0, null); 
         }    
        } 
    } 
    
    //in the constructor 
    JButton button = new JButton("OK"); 
    Panel panel = new Panel(); 
    panel.add(button); 
    add(panel);