键盘不在底部
Keyboard is not at the bottom
我在 IntelliJ 中 运行 这个键盘应该在底部 (this.getContentPane().add(keyboard, BorderLayout.SOUTH);
) 但它卡在中心上方并且将我的文本字段调整得太小。为什么会这样?
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 有关。
问题是我使用 GridLayout
和 BorderLayout
的行冲突。我将 GridLayout
分配给 JFrame,但使用 BorderLayout
来对齐组件。
小修复正在更改 this.setLayout(new GridLayout(6, 5));
至 this.setLayout(new BorderLayout());
。但我仍然不太确定为什么它之前工作..
编辑:this.setLayout(new BorderLayout());
也没有必要添加,所以我最好还是完全删除该行
我在 IntelliJ 中 运行 这个键盘应该在底部 (this.getContentPane().add(keyboard, BorderLayout.SOUTH);
) 但它卡在中心上方并且将我的文本字段调整得太小。为什么会这样?
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 有关。
问题是我使用 GridLayout
和 BorderLayout
的行冲突。我将 GridLayout
分配给 JFrame,但使用 BorderLayout
来对齐组件。
小修复正在更改 this.setLayout(new GridLayout(6, 5));
至 this.setLayout(new BorderLayout());
。但我仍然不太确定为什么它之前工作..
编辑:this.setLayout(new BorderLayout());
也没有必要添加,所以我最好还是完全删除该行