0

I would like to stack 3 different 15 × 15 grids filled with JButtons on top of each other where one of them should be visible and the others transparent, but the JButtons should still function and should be able to be colored visibly

This is the base JFrame ontop of which I would like to add 3 diffrent 15X15 grids 2 of them being filled with transparent JButtons:

package Snake;

import javax.swing.*;

public class Base extends JFrame {
    public Base(){
        super("Snake");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(getMaximumSize());
        setVisible(true);

    }

}

This is the base Grid which should be visable:

package Snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Grid extends JLayeredPane {
    public Grid() {

        GridLayout layout = new GridLayout(15, 15);
        layout.setHgap(1);
        layout.setVgap(1);
        setLayout(layout);
        setBackground(Color.green);
        for (int i = 0; i < 15 * 15; i++) {
            JButton button = new JButton();
            button.setSize(30, 30);
            button.setBackground(Color.pink);
            button.setVisible(true);
            add(button);
        }
        setVisible(true);
    }
}

This should be the transparent Grid which I cant get to be transparent:

package Snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



public class Apple extends JLayeredPane {
    public Apple() {
        GridLayout layout = new GridLayout(15, 15);
        layout.setHgap(1);
        layout.setVgap(1);
        setLayout(layout);
        for (int i = 0; i < 15 * 15; i++) {
            JButton button = new JButton();
            button.setSize(30, 30);
            button.setOpaque(false);
            button.setContentAreaFilled(false);
            button.setBorderPainted(false);
            button.addActionListener(new ButtonListener());
            add(button);
        }
        setOpaque(false);
    }



    private class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            JButton clickedButton = (JButton) e.getSource();
            clickedButton.setContentAreaFilled(true);
            clickedButton.setVisible(true);
            clickedButton.setBackground(Color.RED); // Example action, you can customize this
        }
    }
}

All of this code is being run by the Main file:

package Snake;

public class Main {
    public static void main(String[] args) {
        Base base = new Base();
        Grid grid = new Grid();
        Apple apple = new Apple();



        base.add(grid);
        base.add(apple);

    }
}

So to summarize I would like to stack 3 different 15 × 15 grids filled with JButtons on top of each other where one of them should be visible and the others transparent, but the JButtons should still function and should be able to be colored visibly

PS: The code only seems to run when the screen is extenet

I tried setting setVisible() to false and also experimented with setOblaque on both the JButtons and the JLayeredPane, but couldn't get it to work.

1
  • 1
    Why does your game require 765 JButtons? Generally, when you create a Swing game, you draw the game on a drawing JPanel. Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section. Pay particular attention to the Performing Custom Painting section. Commented Apr 15 at 10:31

1 Answer 1

0

Your main issue are:

  • You never set sizes for your Grid and Apple components.
  • You have not stacked the Grid and Apple components on top of each other.

Any items added to the JFrame will not be stacked on top of each other unless you specifically tell your JFrame to work that way (A null layout would work, but is not recommended), or you need to use a JLayeredPane correctly.

It looks like you have tried to make the Grid and Apple classes a JLayeredPane however that won't do what you want. Instead, You need to add a single JLayeredPane into your JFrame, then you can the Grid and Apple containers to that JLayeredPane a bit like this:

public class Base extends JFrame {
    //Create the JLayeredPane here so that you can use it in other methods
    final JLayeredPane layeredPane = new JLayeredPane();

    public Base(){
        super("Snake");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(getMaximumSize());
        //Add the layered pane
        add(layeredPane);
        setVisible(true);
    }

    //Call this method when adding your Grid and Apple containers
    public void addNewLayer(Component comp) {
        //You need to set the size of each component otherwise it will not be visible
        //We can just fill the entire layeredPane
        comp.setSize(layeredPane.getSize());
        //Add component to a new layer
        layeredPane.add(comp);
    }
}

Note how we have added the JLayeredPane and set the size of the JLayeredPane to fill the JFrame. We have also created a method so that we can add components directly to the layeredPane using addNewLayer(...);

Finally, we just modify your main method to add both the grid and apple components to the actual layeredPane we created in the Base class using base.addNewLayer(apple);:

    Base base = new Base();
    Grid grid = new Grid();
    Apple apple = new Apple();

    //Always add the top most layer first, then any 
    //  additional layers in descending order of display. 
    //  (Or call the layeredPane directly and set the index of the layer)
    base.addNewLayer(apple);
    base.addNewLayer(grid);

Now it should all work, here is an example: Screenshot of the working layers


Final thought:

Because Grid and Apple do not need to be a JLayeredPane it's best to change both of those classes to extends JPanel instead of extends JLayeredPane, for example:

public class Grid extends JPanel {

Lastly, using setBackground(Color.green); on a JLayeredPane won't do anything, but changing both Grid and Apple to a JPanel will fix this issue and let you set background colors if you wish.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.