我有一个 JPanel,我正在向网格内部添加 25 个 JPanel。我希望每个面板周围都有一个边框,以便您可以清楚地区分每个元素。填充也会起作用。如果我尝试添加边框,我将面板添加到面板的方式会将其应用于包含元素的较大面板。
public class LightsOutView
Run Code Online (Sandbox Code Playgroud)
{ GridLayout ExperimentLayout = new GridLayout(5, 5);
// Creates layout of the GUI
public LightsOutView ()
{
JFrame frame = new JFrame();
frame.setTitle("Lights Out");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setContentPane(makeContents());
frame.setVisible(true);
}
/**
* Creates the blank game board. Returns the panel
*
* @return JPanel
*/
public JPanel makeContents ()
{
// Create a panel to hold the 5x5 grid
JPanel board = new JPanel(new GridLayout(5, 5));
board.setLayout(experimentLayout);
// Add the components to …Run Code Online (Sandbox Code Playgroud)