Java GUI中Connect4游戏Circle数组无法显示的问题求助
Hey there! Let's get those white circles appearing in your Connect4 game—you're really close, just missing a couple key steps.
The Core Issue
Even though you're creating a full 2D array of Circle objects in createBoard(), you're never adding those circles to your JPanel. They exist in memory, but Swing has no idea they need to be drawn on the screen. Plus, since you're using a null layout, you also need to tell each circle where to sit on the panel.
Step 1: Add Circles to the JPanel and Set Their Positions
Update your addComponents() method to loop through the gameBoard array, add each circle to the panel, and set its bounds. Here's how to modify that section:
private static void addComponents(JPanel panel) { panel.setLayout(null); JLabel gameTitle = new JLabel("Connect 4"); gameTitle.setBounds(310,20,80,25); panel.add(gameTitle); Circle[][] gameBoard = createBoard(); // Add this loop to render your circles for (int row = 0; row < gameBoard.length; row++) { for (int col = 0; col < gameBoard[row].length; col++) { Circle circle = gameBoard[row][col]; // Calculate position: tweak these values to adjust spacing int x = 40 + col * DISC_SIZE; int y = 60 + row * DISC_SIZE; // Set bounds: x, y, width, height (matches your DISC_SIZE constant) circle.setBounds(x, y, DISC_SIZE, DISC_SIZE); // Add the circle to the panel so it gets drawn panel.add(circle); } } // ... keep all your button code here as-is }
Step 2: Ensure Your Circle Class Draws Correctly
Another common pitfall with custom Swing components is forgetting to properly implement the painting logic. If your Circle class doesn't override paintComponent(), it won't display anything. Here's a corrected version of the Circle class to make sure it draws the filled white circle:
import javax.swing.JComponent; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; public class Circle extends JComponent { private Color fillColor; private float radius; public void setFill(Color fill) { this.fillColor = fill; repaint(); // Tell Swing to redraw when the color changes } public void setRadius(float radius) { this.radius = radius; repaint(); // Tell Swing to redraw when the radius changes } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); // Always call the super method first Graphics2D g2d = (Graphics2D) g; if (fillColor != null) { g2d.setColor(fillColor); // Draw the circle centered within the component's bounds int diameter = (int) (radius * 2); int x = (getWidth() - diameter) / 2; int y = (getHeight() - diameter) / 2; g2d.fillOval(x, y, diameter, diameter); } } }
Why This Works
- Adding to the JPanel: Every Swing component needs to be added to a container (like your JPanel) to be part of the UI hierarchy and rendered.
- Setting Bounds: With a
nulllayout, Swing doesn't handle positioning automatically—you have to define each component'sx,yposition andwidth/height. - Proper Painting: Overriding
paintComponent()is mandatory for custom components to draw themselves. Therepaint()calls ensure the component updates when its properties (like color or radius) change.
Give these changes a try, and those white circles should show up nicely on your blue board!
内容的提问来源于stack exchange,提问作者Stephina Pascho

