Java Swing 键盘格式不正确
Java Swing keyboard not formatting properly
我目前正在为一个小型游戏项目制作 GUI,但我在键盘布局方面遇到了问题,尤其是主行和底行。我通过我的代码完全错过了它,但我有这段代码:
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);
其中 keyboard.add(topkeys)
实际上应该是 keyboard.add(bottomkeys)
,尽管它正是我想要的格式:
但是当我把它改成keyboard.add(bottomkeys)
的时候,有一个奇怪的格式:
为什么当我将它更改为使用 bottomkeys
时,它的格式很奇怪,我该如何解决这个问题?
完整代码如下:
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();
}
}
您正在创建三个面板 topkeys、homekeys 和 buttomkeys 但实际上您并不是按您的意愿使用它们。将按钮添加到这些而不是 keyboard 面板上。
我目前正在为一个小型游戏项目制作 GUI,但我在键盘布局方面遇到了问题,尤其是主行和底行。我通过我的代码完全错过了它,但我有这段代码:
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);
其中 keyboard.add(topkeys)
实际上应该是 keyboard.add(bottomkeys)
,尽管它正是我想要的格式:
但是当我把它改成keyboard.add(bottomkeys)
的时候,有一个奇怪的格式:
为什么当我将它更改为使用 bottomkeys
时,它的格式很奇怪,我该如何解决这个问题?
完整代码如下:
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();
}
}
您正在创建三个面板 topkeys、homekeys 和 buttomkeys 但实际上您并不是按您的意愿使用它们。将按钮添加到这些而不是 keyboard 面板上。