Home > Net >  Java Swing keyboard not formatting properly
Java Swing keyboard not formatting properly

Time:05-02

I'm currently making a GUI for a small game project, but I'm having trouble with the keyboard layout, specifically with the home row and bottom row. I missed it entirely looking through my code, but I have this piece of code:

        home = new JButton[homeRow.length];
        JPanel homekeys = new JPanel(new GridLayout(1, homeRow.length));
        for(int i = 0; i < homeRow.length; i  )
        {
            JButton homesize = new JButton(homeRow[i]);
            homesize.setSize(50, 50);
            home[i] = homesize;
            keyboard.add(home[i]);
        }
        keyboard.add(homekeys);

        bottom = new JButton[bottomRow.length];
        JPanel bottomkeys = new JPanel(new GridLayout(1, bottomRow.length));
        for(int i = 0; i < bottomRow.length; i  )
        {
            JButton bottomsize = new JButton(bottomRow[i]);
            bottomsize.setSize(50, 50);
            bottom[i] = bottomsize;
            keyboard.add(bottom[i]);
        }
        keyboard.add(topkeys);

where keyboard.add(topkeys) should have really been keyboard.add(bottomkeys), though it's exactly in the format I want:

keyboard.add(topkeys)

But when I change it to keyboard.add(bottomkeys), there is a weird format: keyboard.add(bottomkeys)

Why is it being formatted strangely when I change it to use bottomkeys, and how would I fix this?

Here is the entire code:

import javax.swing.*;
import java.awt.*;

public class GameGUI extends JFrame
{
    private final JPanel letters;
    private final JPanel keyboard;
    private final String[] topRow = {"Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"};
    private final String[] homeRow = {"A", "S", "D", "F", "G", "H", "J", "K", "L"};
    private final String[] bottomRow = {"Enter", "Z", "X", "C", "V", "B", "N", "M", "Backspace"};
    private final JButton[] top, home, bottom; // buttons for rows

    public WordleGUI()
    {
        this.setSize(350,400);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        letters = new JPanel();
        letters.setLayout(new GridLayout(6, 5));

        for(int i = 0; i < 30; i  )
            letters.add(new JTextField());

        keyboard = new JPanel();
        keyboard.setLayout(new GridLayout(3, 1));

        // top row
        top = new JButton[topRow.length];
        JPanel topkeys = new JPanel(new GridLayout(1, topRow.length));
        for(int i = 0; i < topRow.length; i  )
        {
            JButton topsize = new JButton(topRow[i]);
            topsize.setSize(50, 50);
            top[i] = topsize;
            keyboard.add(top[i]);
        }
        keyboard.add(topkeys);

        // home row
        home = new JButton[homeRow.length];
        JPanel homekeys = new JPanel(new GridLayout(1, homeRow.length));
        for(int i = 0; i < homeRow.length; i  )
        {
            JButton homesize = new JButton(homeRow[i]);
            homesize.setSize(50, 50);
            home[i] = homesize;
            keyboard.add(home[i]);
        }
        keyboard.add(homekeys);

        bottom = new JButton[bottomRow.length];
        JPanel bottomkeys = new JPanel(new GridLayout(1, bottomRow.length));
        for(int i = 0; i < bottomRow.length; i  )
        {
            JButton bottomsize = new JButton(bottomRow[i]);
            bottomsize.setSize(50, 50);
            bottom[i] = bottomsize;
            keyboard.add(bottom[i]);
        }
        keyboard.add(bottomkeys); // weird formatting occuring here

        this.getContentPane().add(letters, BorderLayout.NORTH);
        this.getContentPane().add(keyboard, BorderLayout.SOUTH);
        this.setVisible(true);
    }

    public static void main(String[] args)
    {
        new GameGUI();
    }
}

CodePudding user response:

You are creating three panels topkeys, homekeys and buttomkeys but you are not actually using them as you intended to. Add the buttons onto these and not onto the keyboard panel.

  • Related