键盘不在底部
我正在在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?
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);
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是我使用
gridlayout
和borderlayout
的矛盾行。我将gridlayout
分配给jframe,但使用borderlayout
将组件对齐。小修复程序正在更改
this.setlayout(new Gridlayout(6,5));
this.setLayout(new BorderLayout());
。但是我仍然不太确定它为什么以前工作。这条线完全
The problem was the conflicting lines where I use
GridLayout
andBorderLayout
. I assignGridLayout
to JFrame but useBorderLayout
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