Java 在JPanel中以特定方式定位按钮
我试图以一种特定的方式定位六个按钮,但目前输出几乎正确,但按钮不在我希望它们处于的确切位置(所有按钮都接触相邻的按钮,没有间隙)。这是如下所示: 有人能看看我的代码,看看为什么定位不准确吗Java 在JPanel中以特定方式定位按钮,java,swing,button,layout-manager,boxlayout,Java,Swing,Button,Layout Manager,Boxlayout,我试图以一种特定的方式定位六个按钮,但目前输出几乎正确,但按钮不在我希望它们处于的确切位置(所有按钮都接触相邻的按钮,没有间隙)。这是如下所示: 有人能看看我的代码,看看为什么定位不准确吗 public void createDirectionButtonPanel() { JButton northButton = new JButton("north"); JButton southButton = new JButton("south"); JButton w
public void createDirectionButtonPanel() {
JButton northButton = new JButton("north");
JButton southButton = new JButton("south");
JButton westButton = new JButton("west");
JButton eastButton = new JButton("east");
JButton upButton = new JButton("up");
JButton downButton = new JButton("down");
JPanel directionButtonPanel = new JPanel();
// directionButtonPanel.setOpaque(false);
directionButtonPanel.setLayout(new BoxLayout(directionButtonPanel, BoxLayout.Y_AXIS));
JPanel directionRow_1 = new JPanel();
// directionRow_1.setOpaque(false);
directionRow_1.setLayout(new BoxLayout(directionRow_1, BoxLayout.X_AXIS));
directionRow_1.add(Box.createRigidArea(northButton.getPreferredSize()));
directionRow_1.add(northButton);
directionRow_1.add(Box.createRigidArea(northButton.getPreferredSize()));
directionRow_1.add(upButton);
JPanel directionRow_2 = new JPanel();
// directionRow_2.setOpaque(false);
directionRow_2.setLayout(new BoxLayout(directionRow_2, BoxLayout.X_AXIS));
directionRow_2.add(westButton);
directionRow_2.add(Box.createRigidArea(northButton.getPreferredSize()));
directionRow_2.add(eastButton);
directionRow_2.add(Box.createRigidArea(northButton.getPreferredSize()));
JPanel directionRow_3 = new JPanel();
// directionRow_3.setOpaque(false);
directionRow_3.setLayout(new BoxLayout(directionRow_3, BoxLayout.X_AXIS));
directionRow_3.add(Box.createRigidArea(northButton.getPreferredSize()));
directionRow_3.add(southButton);
directionRow_3.add(Box.createRigidArea(northButton.getPreferredSize()));
directionRow_3.add(downButton);
upButton.setMaximumSize(southButton.getPreferredSize());
northButton.setMaximumSize(southButton.getPreferredSize());
westButton.setMaximumSize(southButton.getPreferredSize());
southButton.setMaximumSize(southButton.getPreferredSize());
downButton.setMaximumSize(southButton.getPreferredSize());
eastButton.setMaximumSize(southButton.getPreferredSize());
directionButtonPanel.add(directionRow_1);
directionButtonPanel.add(directionRow_2);
directionButtonPanel.add(directionRow_3);
}
试试这个。GridLayout似乎很合适,正如JavaDoc所说:
创建具有指定行数和列数的网格布局。布局中的所有组件都具有相同的大小。
还省去了您自己排列行的麻烦。只需将所有内容添加到方向按钮面板
JButton northButton = new JButton("north");
JButton southButton = new JButton("south");
JButton westButton = new JButton("west");
JButton eastButton = new JButton("east");
JButton upButton = new JButton("up");
JButton downButton = new JButton("down");
JPanel directionButtonPanel = new JPanel();
directionButtonPanel.setLayout(new GridLayout(3,4));
// row 1
directionButtonPanel.add(new JPanel());
directionButtonPanel.add(northButton);
directionButtonPanel.add(new JPanel());
directionButtonPanel.add(upButton);
// row 2
directionButtonPanel.add(westButton);
directionButtonPanel.add(new JPanel());
directionButtonPanel.add(eastButton);
directionButtonPanel.add(new JPanel());
// row 3
directionButtonPanel.add(new JPanel());
directionButtonPanel.add(southButton);
directionButtonPanel.add(new JPanel());
directionButtonPanel.add(downButton);
1,simpest是通过放置12个JButtun(其余部分不应设置为可见(false)),2。最好是在这里搜索操纵杆或类似导航键的东西(如TVs remote),这样上面的图像就是您需要的?或者您从显示的代码中得到了什么?我很困惑。。使用
GridLayout
可以轻松创建图像。