0

Sorry if this has been answered somewhere else but I cant find a solution.

I have a JFrame for which ive set a background and a JPanel in a JScrollPane.

However when i add the scrollpane to my jframe it only shows grey even though i set it to opaque.

I am trying to get it so the jpanel back is transparent and the buttons added to the jpanel scroll over the background.

Here is my code:

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();
    }   
}

Does anyone know how to fix this or know where it has been answered?

Thanks.

(Yes i know its null layout, ill change it when ive got this sorted(!))

Cameron
  • 115
  • 1
  • 7

1 Answers1

0

Set the scroller viewport to be non-opaque

scroller.getViewport().setOpaque(false);

Also, I would suggest overriding JButton.paintComponent() instead of JButton.paint(). Difference between paint() and paintcomponent()?

Community
  • 1
  • 1
Joshua
  • 341
  • 2
  • 10