2014-04-17 28 views
0

对不起,如果这已被回答在别的地方,但我找不到解决方案。未显示背景图片。被JPanel/JScrollPane拦截?

我有一个JFrame,为其设置背景和JScrollPane中的JPanel。

但是,当我将scrollpane添加到我的jframe时,它只显示灰色,即使我将它设置为不透明。

我想弄明白,所以jpanel背部是透明的,并且按钮添加到jpanel 在背景上滚动。

这里是我的代码:

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.io.File; 

import javax.imageio.ImageIO; 
import javax.swing.*; 


public class ZeldaBundle extends JFrame { 

JPanel panel = new JPanel(); 
JScrollPane scroller = new JScrollPane(panel); 
JLabel background = new JLabel(new ImageIcon("images/back.jpg")); 

JButton theLegendOfZelda = new JButton() { 
    public void paint(Graphics g) { 
     try { 
      g.drawImage(ImageIO.read(new File("images/The Legend Of Zelda.png")), 0, 0, null); 
     } catch (Exception e) { 
      setText("The Legend OF Zelda"); 
     } 
    } 
}; 

public ZeldaBundle() { 
    setSize(900, 563); 
    setResizable(false); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    setIconImage(new ImageIcon("images/zelda.png").getImage()); 

    setContentPane(background); 
    background.setLayout(new BorderLayout()); 

    panel.setPreferredSize(new Dimension(1000, 563)); 
    panel.setLayout(null); 

    panel.setOpaque(false); 
    scroller.setOpaque(false); 

    theLegendOfZelda.setSize(270, 110); 
    theLegendOfZelda.setLocation(700, 113); 
    panel.add(theLegendOfZelda); 
    scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); 
    scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER); 
    add(scroller); 
    setVisible(true); 
    } 

    public static void main(String[] args) { 
     new ZeldaBundle(); 
    } 
} 

有谁知道如何解决这个问题或知道它已经有了答案?

谢谢。

(是的,我知道它的空布局,生病改变它时,香港专业教育学院得到这个排序(!))

回答