1

I am making a chess game, and am using JavaSwing for the GUI. the problem im running into is not having the background of the buttons display correctly. The board consists of 8x8 buttons which i want gray and white. Currently the buttons have an outline of the background color, but the main portion of the button itself is white. How do I make it so the entire button is the same color as the background?

Current Code for making the buttons: (note) the images are added in another method. Button


        boardPanel = new JPanel(new GridLayout(8, 8));

        boardButtons = new JButton[8][8];
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                boardButtons[i][j] = new JButton();
                boardButtons[i][j].setPreferredSize(new Dimension(100, 100));
                boardButtons[i][j].setMinimumSize(new Dimension(100, 100));
                boardButtons[i][j].addActionListener(buttonListener);
                if ((i + j) % 2 == 0) {
                    boardButtons[i][j].setBackground(Color.WHITE);
                } else {
                    boardButtons[i][j].setBackground(Color.GRAY);
                }
                boardButtons[i][j].setOpaque(true);
                boardPanel.add(boardButtons[i][j]);
            }
        }

        // Create a new panel and add the board panel and white space panel to it
        JPanel contentPane = new JPanel();
        contentPane.setPreferredSize(new Dimension(1200, 800)); // fixed size for board panel
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
        contentPane.add(boardPanel);

        JPanel whiteSpacePanel = new JPanel();
        whiteSpacePanel.setPreferredSize(new Dimension(400, 800)); // desired size of white space
        whiteSpacePanel.setBackground(Color.CYAN);
        contentPane.add(whiteSpacePanel);

        // Add the new panel to the frame and set some properties
        setContentPane(contentPane);
        setTitle("Capture The Quad Board");
        setResizable(false); // prevent resizing of the window
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
        paintBoard();

Current board:

https://i.stack.imgur.com/YpyvA.png

camickr
  • 321,443
  • 19
  • 166
  • 288
PegLegPaul
  • 11
  • 1
  • 1
    *"Current Code for making the buttons: (note) the images are added in another method"* -- And this may be key. Where do you add images to the buttons and are the images png images and do they have white or transparent backgrounds? – Hovercraft Full Of Eels Mar 15 '23 at 01:10
  • Myself, I wouldn't even use JButtons, but instead would use JPanels and add JLabels with chess piece images that have transparent backgrounds to represent the pieces. This way, it would be easy to drag and drop the images/labels by mouse click. – Hovercraft Full Of Eels Mar 15 '23 at 01:16
  • Why are you creating 132 Dimension objects for the size of the component? Just create a single instance outside the loop. – camickr Mar 15 '23 at 01:16
  • 1
    Buttons have "content", which is different from the background color. You need to configure the button to remove the "content" and allow it to paint the background color [for example](https://stackoverflow.com/questions/63696304/java-set-colour-of-a-button-and-not-the-border-on-a-macos/63697095#63697095) – MadProgrammer Mar 15 '23 at 01:19
  • And [example](https://stackoverflow.com/questions/72155726/change-jbutton-background-color/72157195#72157195); [example](https://stackoverflow.com/questions/60939388/how-do-i-change-the-color-of-a-button-not-just-the-border-around-it/60939785#60939785); [example](https://stackoverflow.com/questions/43311214/color-of-jbutton/43312480#43312480) – MadProgrammer Mar 15 '23 at 01:24

0 Answers0