2009-09-23 51 views

risposta

20

Non esiste un concetto di "immagine di sfondo" in un JPanel, quindi è necessario scrivere il proprio modo di implementare tale funzione.

Un modo per ottenere ciò sarebbe quello di ignorare il metodo paintComponent per disegnare un'immagine di sfondo ogni volta che viene aggiornato il JPanel.

Per esempio, si potrebbe creare una sottoclasse un JPanel, e aggiungere un campo per contenere l'immagine di sfondo, e l'override del metodo paintComponent:

public class JPanelWithBackground extends JPanel { 

    private Image backgroundImage; 

    // Some code to initialize the background image. 
    // Here, we use the constructor to load the image. This 
    // can vary depending on the use case of the panel. 
    public JPanelWithBackground(String fileName) throws IOException { 
    backgroundImage = ImageIO.read(new File(fileName)); 
    } 

    public void paintComponent(Graphics g) { 
    super.paintComponent(g); 

    // Draw the background image. 
    g.drawImage(backgroundImage, 0, 0, this); 
    } 
} 

(. Sopra il codice non è stato testato)

Il seguente codice può essere usato per aggiungere il JPanelWithBackground in un JFrame:

JFrame f = new JFrame(); 
f.getContentPane().add(new JPanelWithBackground("sample.jpeg")); 

In questo esempio, il 012 Il metodoè stato utilizzato per leggere nel file JPEG esterno.

+1

Questo non risponde alla domanda. Mette un'immagine di sfondo su un pannello, ma poi inserisce semplicemente il pannello nel layout normale. La domanda era come impostare uno sfondo su una cornice dietro altri componenti. – Boann

+0

Provocherebbe problemi se gli avessi dato "null" come ImageObserver? – georgiaboy82

1

Ecco un altro approccio rapido senza l'utilizzo di un pannello aggiuntivo.

JFrame f = new JFrame("stackoverflow") { 
    private Image backgroundImage = ImageIO.read(new File("background.jpg")); 
    public void paint(Graphics g) { 
    super.paint(g); 
    g.drawImage(backgroundImage, 0, 0, null); 
    } 
}; 
+2

Non ho trovato questa tecnica per funzionare correttamente. L'immagine a volte richiama i componenti del bambino, oppure a volte viene coperta dallo sfondo normale del frame quando non dovrebbe. – Boann

+0

Sì, questo non funziona con i componenti figlio. –

5

Questo è fatto facilmente sostituendo riquadro del contenuto del telaio con un JPanel che attira la vostra immagine:

try { 
    final Image backgroundImage = javax.imageio.ImageIO.read(new File(...)); 
    setContentPane(new JPanel(new BorderLayout()) { 
     @Override public void paintComponent(Graphics g) { 
      g.drawImage(backgroundImage, 0, 0, null); 
     } 
    }); 
} catch (IOException e) { 
    throw new RuntimeException(e); 
} 

questo esempio viene impostato anche il layout del pannello di BorderLayout per abbinare il layout riquadro del contenuto di default.

(Se avete problemi a vedere l'immagine, potrebbe essere necessario chiamare setOpaque(false) su alcuni altri componenti in modo che si può vedere attraverso il fondo.)

0
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
class BackgroundImageJFrame extends JFrame 
{ 
    JButton b1; 
    JLabel l1; 
public BackgroundImageJFrame() 
{ 
setTitle("Background Color for JFrame"); 
setSize(400,400); 
setLocationRelativeTo(null); 
setDefaultCloseOperation(EXIT_ON_CLOSE); 
setVisible(true); 
/* 
One way 
-----------------*/ 
setLayout(new BorderLayout()); 
JLabel background=new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png")); 
add(background); 
background.setLayout(new FlowLayout()); 
l1=new JLabel("Here is a button"); 
b1=new JButton("I am a button"); 
background.add(l1); 
background.add(b1); 

// Another way 
setLayout(new BorderLayout()); 
setContentPane(new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads \\colorful design.png"))); 
setLayout(new FlowLayout()); 
l1=new JLabel("Here is a button"); 
b1=new JButton("I am a button"); 
add(l1); 
add(b1); 
// Just for refresh :) Not optional! 
    setSize(399,399); 
    setSize(400,400); 
    } 
    public static void main(String args[]) 
    { 
    new BackgroundImageJFrame(); 
} 
} 
0

se si sta utilizzando NetBeans è possibile aggiungere una jlabel al frame e attraverso le proprietà cambia la sua icona nella tua immagine e rimuove il testo. quindi sposta la jlabel nella parte inferiore di Jframe o in qualsiasi riquadro del contenuto tramite il navigatore

Problemi correlati