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.
JButtons
? Generally, when you create a Swing game, you draw the game on a drawingJPanel
. 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.