我想将 3 个不同的 15 × 15 网格填充在彼此的顶部,其中一个应该可见,其他网格透明,但 JButton 应该仍然起作用并且应该能够明显着色
这是基础 JFrame,我想在其上添加 3 个不同的 15X15 网格,其中 2 个网格填充透明 JButton:
package Snake;
import javax.swing.*;
public class Base extends JFrame {
public Base(){
super("Snake");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(getMaximumSize());
setVisible(true);
}
}
这是应该可见的基本网格:
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);
}
}
这应该是透明网格,我无法做到透明:
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
}
}
}
所有这些代码都由主文件运行:
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);
}
}
所以总结一下,我想将 3 个不同的 15 × 15 网格填充在彼此的顶部,其中一个应该可见,其他网格应该透明,但 JButton 应该仍然起作用并且应该能够明显着色
PS: 该代码似乎仅在屏幕扩展时运行
我尝试将 setVisible() 设置为 false,并在 JButtons 和 JLayeredPane 上尝试使用 setOblaque,但无法使其工作。
您的主要问题是:
添加到 JFrame 的任何项目都不会堆叠在一起,除非您明确告诉 JFrame 以这种方式工作(
null
布局可以,但不推荐),或者您需要使用 JLayeredPane
正确。
看起来您已尝试将
Grid
和 Apple
类设置为 JLayeredPane
但这不会做您想要的事情。相反,您需要将单个 JLayeredPane
添加到您的 JFrame
中,然后您可以将 Grid 和 Apple 容器添加到该 JLayeredPane 中,如下所示:
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);
}
}
注意我们如何添加
JLayeredPane
并设置 JLayeredPane 的大小以填充 JFrame。我们还创建了一个方法,以便我们可以使用 addNewLayer(...);
直接将组件添加到 LayeredPane
最后,我们只需修改您的 main 方法,将
grid
和 apple
组件添加到我们使用 base.addNewLayer(apple);
: 在基类中创建的实际 LayeredPane 中
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);
最后的想法:
因为
Grid
和 Apple
不需要是 JLayeredPane,所以最好将这两个类更改为 extends JPanel
而不是 extends JLayeredPane
,例如:
public class Grid extends JPanel {
最后,在
setBackground(Color.green);
上使用 JLayeredPane
不会执行任何操作,但将 Grid 和 Apple 更改为 JPanel
将解决此问题,并允许您根据需要设置背景颜色。