键盘不在底部

发布于 2025-01-25 13:44:07 字数 4008 浏览 5 评论 0原文

我正在在Intellij中运行此操作,键盘应在底部(this.getContentPane()。add(键盘,borderlayout.south.south);),但它卡在中心上方并调整我的Textfields方法的大小太小。为什么这样做?

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

    public class GameGUI extends JFrame
    {
        private final JPanel letters; // for textfields
        private final JPanel keyboard; // for buttons
        private final JTextField [][] tfs; // array for textfields
        private final JButton[] top, home, bottom; // buttons for rows
        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"};
    
        public GameGUI()
        {
            this.setSize(350, 450);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            this.setLayout(new GridLayout(6, 5));
    
            letters = new JPanel();
            letters.setLayout(new GridLayout(6, 5));
    
            tfs = new JTextField[6][5];
            JPanel letterPlaceholder = new JPanel(new GridLayout(6, 5));
            for(int r = 0; r < 6; r++)
            {
                for(int c = 0; c < 5; c++)
                {
                    JTextField board = new JTextField(String.valueOf(tfs[r][c]));
                    tfs[r][c] = board;
                    letterPlaceholder.add(tfs[r][c]);
                }
            }
            letters.add(letterPlaceholder);
    
            keyboard = new JPanel();
            keyboard.setLayout(new GridLayout(3, 1));
    
            // top row
            top = new JButton[topRow.length];
            JPanel keys = new JPanel(new GridLayout(1, topRow.length));
            for(int i = 0; i < topRow.length; i++)
            {
                JButton topsize = new JButton(topRow[i]);
                top[i] = topsize;
                keys.add(top[i]);
            }
            keyboard.add(keys);
    
            // home row
            home = new JButton[homeRow.length];
            keys = new JPanel(new GridLayout(1, homeRow.length));
            for(int i = 0; i < homeRow.length; i++)
            {
                JButton homesize = new JButton(homeRow[i]);
                home[i] = homesize;
                keys.add(home[i]);
            }
            keyboard.add(keys);
    
            // bottom row
            bottom = new JButton[bottomRow.length];
            keys = new JPanel(new GridLayout(1, bottomRow.length));
            for(int i = 0; i < bottomRow.length; i++)
            {
                JButton bottomsize = new JButton(bottomRow[i]);
                bottom[i] = bottomsize;
                keys.add(bottom[i]);
            }
            keyboard.add(keys);
    
            this.getContentPane().add(letters, BorderLayout.NORTH);
            this.getContentPane().add(keyboard, BorderLayout.SOUTH);
            this.setVisible(true);
        }
    
        public static void main(String[] args)
        {
            new GameGUI();
        }
    }

我进行了试验,并评论了此代码(以及此部分所需的其他行),以查看是否是问题:

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

tfs = new JTextField[6][5];
JPanel letterPlaceholder = new JPanel(new GridLayout(6, 5));
for(int r = 0; r < 6; r++)
{
    for(int c = 0; c < 5; c++)
    {
        JTextField board = new JTextField(String.valueOf(tfs[r][c]));
        tfs[r][c] = board;
        letterPlaceholder.add(tfs[r][c]);
    }
}
letters.add(letterPlaceholder);

但是它的作用是这样:

甚至很奇怪的是,我的代码与几天前的键盘目前的代码相同,而且工作正常。我要么做错了什么,要么与Intellij有关。

I'm running this in IntelliJ and the keyboard should be at the bottom (this.getContentPane().add(keyboard, BorderLayout.SOUTH);) but it's stuck above the center and resizes my textfields way too small. Why is it acting that way?
keyboard

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

    public class GameGUI extends JFrame
    {
        private final JPanel letters; // for textfields
        private final JPanel keyboard; // for buttons
        private final JTextField [][] tfs; // array for textfields
        private final JButton[] top, home, bottom; // buttons for rows
        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"};
    
        public GameGUI()
        {
            this.setSize(350, 450);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            this.setLayout(new GridLayout(6, 5));
    
            letters = new JPanel();
            letters.setLayout(new GridLayout(6, 5));
    
            tfs = new JTextField[6][5];
            JPanel letterPlaceholder = new JPanel(new GridLayout(6, 5));
            for(int r = 0; r < 6; r++)
            {
                for(int c = 0; c < 5; c++)
                {
                    JTextField board = new JTextField(String.valueOf(tfs[r][c]));
                    tfs[r][c] = board;
                    letterPlaceholder.add(tfs[r][c]);
                }
            }
            letters.add(letterPlaceholder);
    
            keyboard = new JPanel();
            keyboard.setLayout(new GridLayout(3, 1));
    
            // top row
            top = new JButton[topRow.length];
            JPanel keys = new JPanel(new GridLayout(1, topRow.length));
            for(int i = 0; i < topRow.length; i++)
            {
                JButton topsize = new JButton(topRow[i]);
                top[i] = topsize;
                keys.add(top[i]);
            }
            keyboard.add(keys);
    
            // home row
            home = new JButton[homeRow.length];
            keys = new JPanel(new GridLayout(1, homeRow.length));
            for(int i = 0; i < homeRow.length; i++)
            {
                JButton homesize = new JButton(homeRow[i]);
                home[i] = homesize;
                keys.add(home[i]);
            }
            keyboard.add(keys);
    
            // bottom row
            bottom = new JButton[bottomRow.length];
            keys = new JPanel(new GridLayout(1, bottomRow.length));
            for(int i = 0; i < bottomRow.length; i++)
            {
                JButton bottomsize = new JButton(bottomRow[i]);
                bottom[i] = bottomsize;
                keys.add(bottom[i]);
            }
            keyboard.add(keys);
    
            this.getContentPane().add(letters, BorderLayout.NORTH);
            this.getContentPane().add(keyboard, BorderLayout.SOUTH);
            this.setVisible(true);
        }
    
        public static void main(String[] args)
        {
            new GameGUI();
        }
    }

I experimented around and commented this code (and other lines that is needed for this part) out to see if maybe that was the problem:

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

tfs = new JTextField[6][5];
JPanel letterPlaceholder = new JPanel(new GridLayout(6, 5));
for(int r = 0; r < 6; r++)
{
    for(int c = 0; c < 5; c++)
    {
        JTextField board = new JTextField(String.valueOf(tfs[r][c]));
        tfs[r][c] = board;
        letterPlaceholder.add(tfs[r][c]);
    }
}
letters.add(letterPlaceholder);

But then it acts like this:
enter image description here

What's even weirder is that I had the same code as I have currently for the keyboard days ago and it was working fine. I'm either doing something wrong or it's something that has to do with IntelliJ.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放血 2025-02-01 13:44:07

问题是我使用gridlayoutborderlayout的矛盾行。我将gridlayout分配给jframe,但使用borderlayout将组件对齐。

小修复程序正在更改this.setlayout(new Gridlayout(6,5));
this.setLayout(new BorderLayout());。但是我仍然不太确定它为什么以前工作

。这条线完全

The problem was the conflicting lines where I use GridLayout and BorderLayout. I assign GridLayout to JFrame but use BorderLayout to align the components.

The small fix was changing this.setLayout(new GridLayout(6, 5));
to this.setLayout(new BorderLayout());. But I'm still not too sure on why it was working before..

Edit: this.setLayout(new BorderLayout()); would also just be unnecessary to add, so I could better yet just remove the line completely

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文