Java:GridLayout开头的间隙
我正在尝试用Java编写一个游戏,目前正在制作标题屏幕 我想中间有三个按钮放在中间。 这些按钮是“播放”、“选项”和“退出” 我使用网格布局订购按钮,现在我希望屏幕顶部和播放按钮之间有一点间隙。你能告诉我怎么做吗 我当前的代码如下所示:Java:GridLayout开头的间隙,java,user-interface,layout-manager,grid-layout,Java,User Interface,Layout Manager,Grid Layout,我正在尝试用Java编写一个游戏,目前正在制作标题屏幕 我想中间有三个按钮放在中间。 这些按钮是“播放”、“选项”和“退出” 我使用网格布局订购按钮,现在我希望屏幕顶部和播放按钮之间有一点间隙。你能告诉我怎么做吗 我当前的代码如下所示: public class GamePanel extends JPanel { private JPanel optionsPanel; public GamePanel() { super(new FlowLayout())
public class GamePanel extends JPanel {
private JPanel optionsPanel;
public GamePanel() {
super(new FlowLayout());
FlowLayout layout = (FlowLayout) getLayout();
layout.setAlignment(FlowLayout.CENTER);
GridLayout layout2 = new GridLayout(3,1);
optionsPanel = new JPanel(layout2);
add(optionsPanel);
optionsPanel.add(new CustomButton("Play"));
optionsPanel.add(new CustomButton("Options"));
optionsPanel.add(new CustomButton("Quit"));
}
}
optionsPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
试着这样做:
public class GamePanel extends JPanel {
private JPanel optionsPanel;
public GamePanel() {
super(new FlowLayout());
FlowLayout layout = (FlowLayout) getLayout();
layout.setAlignment(FlowLayout.CENTER);
GridLayout layout2 = new GridLayout(3,1);
optionsPanel = new JPanel(layout2);
add(optionsPanel);
optionsPanel.add(new CustomButton("Play"));
optionsPanel.add(new CustomButton("Options"));
optionsPanel.add(new CustomButton("Quit"));
}
}
optionsPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
另见: