2012-06-20 69 views
1

我有一個包含2個JPanel的JPanel。位於左側(左側框)和右側(右側),我想在右側JPanel(rB)上添加背景圖像。嵌套JPanel中的背景圖像?

但結果我得到的是

http://i.imgur.com/tHz1x.jpg

我想

http://i.imgur.com/xHbpx.jpg

public void paintComponent(Graphics g) 
{ 
    //this.paintComponent(g); 
    if(wdimage != null) g.drawImage(wdimage,0,0,800,800,rB); //(image,location x, location y, size x, size y) 

} 
結果

rB Panel阻止圖像,我想要的是在JPanel上顯示圖像,並在稍後的JPanel和圖像頂部添加一些jlabel和文本字段。

+0

'//this.paintComponent(G);'使你正在尋找'super.paintComponent方法遞歸(G) '在'g.drawImage'之後。雖然使用JLabel和Z順序更容易(可能)。其他組件也必須是非透明的。 – bestsss

+0

僅供參考:你爲什麼要提供'rB'作爲圖像觀察者?任何特定的原因或其錯誤? –

回答

5

BACKGROUND IMAGE OUTPUT

在這裏它的出現沒有任何問題,看看:

import java.awt.*; 
import java.awt.image.BufferedImage; 
import java.net.URL; 
import javax.imageio.ImageIO; 
import javax.swing.*; 

public class PanelExample 
{ 
    private void createAndDisplayGUI() 
    { 
     JFrame frame = new JFrame("Panel Example"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel contentPane = new JPanel(); 
     contentPane.setOpaque(true); 
     contentPane.setBorder(
        BorderFactory.createMatteBorder(
            5, 5, 5, 5, Color.WHITE)); 
     contentPane.setBackground(Color.WHITE); 
     contentPane.setLayout(new BorderLayout(10, 10)); 

     ImagePanel imagePanel = new ImagePanel(); 
     //imagePanel.createGUI(); 
     BlankPanel blankPanel = new BlankPanel(); 

     contentPane.add(blankPanel, BorderLayout.LINE_START); 
     contentPane.add(imagePanel, BorderLayout.CENTER); 
     frame.setContentPane(contentPane); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String... args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       new PanelExample().createAndDisplayGUI(); 
      } 
     }); 
    } 
} 

class ImagePanel extends JPanel 
{ 
    private BufferedImage image; 

    public ImagePanel() 
    { 
     setOpaque(true); 
     setBorder(BorderFactory.createLineBorder(Color.BLACK, 5)); 
     try 
     { 
      image = ImageIO.read(new URL("http://gagandeepbali.uk.to/gaganisonline/images/background.jpg")); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
     createGUI(); 
    } 

    public void createGUI() 
    { 
     setLayout(new GridBagLayout()); 
     JPanel loginPanel = new JPanel(); 
     loginPanel.setOpaque(false); 
     loginPanel.setLayout(new GridLayout(2, 2, 2, 2)); 
     JLabel userLabel = new JLabel("USERNAME : "); 
     userLabel.setForeground(Color.WHITE); 
     JTextField userField = new JTextField(10); 
     JLabel passLabel = new JLabel("PASSWORD : "); 
     passLabel.setForeground(Color.WHITE); 
     JPasswordField passField = new JPasswordField(10); 

     loginPanel.add(userLabel); 
     loginPanel.add(userField); 
     loginPanel.add(passLabel); 
     loginPanel.add(passField); 

     add(loginPanel); 
     System.out.println("I am finished"); 
    } 

    @Override 
    public Dimension getPreferredSize() 
    { 
     return (new Dimension(300, 300)); 
    } 

    public void paintComponent(Graphics g) 
    { 
     super.paintComponent(g); 
     g.drawImage(image, 0, 0, this); 
    } 
} 

class BlankPanel extends JPanel 
{ 
    public BlankPanel() 
    { 
     setOpaque(true); 
     setBorder(BorderFactory.createLineBorder(Color.BLACK, 5)); 
     setBackground(Color.WHITE); 
    } 

    @Override 
    public Dimension getPreferredSize() 
    { 
     return (new Dimension(100, 300)); 
    } 
} 
+1

+1 [Twinkle Twinkle Little Star](http://en.wikipedia.org/wiki/Twinkle_Twinkle_Little_Star) – mKorbel