java 边框布局间距/边距

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16066055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:47:02  来源:igfitidea点击:

Border Layout Spacing/Margins

javaswinglayout-managermarginsgrid-layout

提问by Bob Smith

for my beginner Java class we made an automated Tic-Tac-Toe game, and now we are creating a GUI to play it on. However, I'm having a lot of trouble getting the spacing/margins right. So far, I've just been trying to get the general framework for the GUI, and then I'm going to go back and actually implement the Tic-Tac-Toe game I have already made. So, far, I have this:

对于我的初级 Java 课程,我们制作了一个自动井字游戏,现在我们正在创建一个 GUI 来玩它。但是,我在正确设置间距/边距时遇到了很多麻烦。到目前为止,我只是试图获得 GUI 的一般框架,然后我将回去实际实现我已经制作的 Tic-Tac-Toe 游戏。到目前为止,我有这个:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;

public class ITTTGUI extends JFrame implements ActionListener{

    //butimport javax.swing.border.*;tons
    private JPanel sizePanel;
    private JPanel buttonPanel;
    private JPanel displayPanel;
    private JPanel bottomPanel;
    private JTextField size;
    private JButton rebuildButton;
    private JButton[] buttons;
    private JLabel output;

    //constructor
    public NumberChooser(){
        setTitle("BitchFace");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //content pane
        Container cp = getContentPane();

        //add a panel for the size
        sizePanel = new JPanel();
        sizePanel.setBorder(new EmptyBorder(5, 5, 5, 5) );//adds margin to panel
        sizePanel.setLayout(new FlowLayout());
        size = new JTextField("3",5);
        sizePanel.add(new JLabel("N"));
        sizePanel.add(size);
        rebuildButton = new JButton("Rebuild");
        rebuildButton.addActionListener(this);
        sizePanel.add(rebuildButton);
        //add bottom panel for output

            sizePanel.add(new JButton("Player:"), BorderLayout.EAST);
            sizePanel.add(new JButton("Move:"), BorderLayout.EAST);
            sizePanel.add(new JButton("Winner:"), BorderLayout.EAST);


        //add a panel for the numbers
        buttonPanel = new JPanel();
        buttonPanel.setBorder(new EmptyBorder(5, 5, 5, 5) );//adds margin to panel
        buildButtonsPanel();

        //add bottom panel for output
        JPanel bottomPanel = new JPanel();
        bottomPanel.setBorder(new EmptyBorder(5, 5, 5, 5) );//adds margin to panel
        bottomPanel.add(new JButton("New Game"), BorderLayout.SOUTH);
        bottomPanel.add(new JButton("Advise"), BorderLayout.SOUTH);
        bottomPanel.add(new JButton("Quit"), BorderLayout.SOUTH);




        //add panels to main pane
        cp.setLayout(new BorderLayout());
        cp.add(sizePanel, BorderLayout.EAST);
        cp.add(buttonPanel, BorderLayout.CENTER);
        cp.add(bottomPanel, BorderLayout.SOUTH);
        pack();
    }

    //this is a helper method to rebuild the buttons panel
    private void buildButtonsPanel(){
        int n = 3;
        try{
            n = Integer.parseInt(size.getText());
        }catch(Exception e){
            e.printStackTrace();
        }
        buttonPanel.removeAll();
        buttonPanel.setLayout(new GridLayout(n,n,4,4));
        buttons = new JButton[n*n];
        for(int i=0; i < buttons.length; i++){

            buttons[i] = new JButton("*");
            buttonPanel.add(buttons[i]);
            buttons[i].addActionListener(this);
        }
        revalidate();
        repaint();
        pack();
    }

    public void actionPerformed(ActionEvent e){
        Object s = e.getSource();
        //check to see if the action came from the rebuild button
        if(s == rebuildButton){
            buildButtonsPanel();
        }
        //otherwise it came from the grid

    }

    //entry point
    public static void main(String[] args){
        //create the GUI
        NumberChooser nc = new NumberChooser();
        nc.setVisible(true);
    }

}

It doesn't have to be exact, just generally the same. And also there is no X or winner variable since I haven't implemented that. I also tried changing the margins from (5,5,5,5) to like (1,1,1,1), but that didn't change anything at all so that also confused me.

它不必是精确的,只是大体相同。而且也没有 X 或获胜者变量,因为我还没有实现。我还尝试将边距从 (5,5,5,5) 更改为喜欢 (1,1,1,1),但这根本没有改变任何东西,所以这也让我感到困惑。

Any help with this, or how to generally go about this assignment would be appreciated.

对此的任何帮助,或如何一般地进行这项任务,将不胜感激。

(And it's not accepting my images. Sorry.) Links are:

(而且它不接受我的图像。对不起。)链接是:

The problem is that is looks like this: What my GUI looks like

问题是看起来像这样: 我的 GUI 是什么样的

https://i1224.photobucket.com/albums/ee362/Devolutor/COPimage1_zps459f304b.png

https://i1224.photobucket.com/albums/ee362/Devolutor/COPimage1_zps459f304b.png

And it is supposed to look like this: What it is supposed to look like

它应该是这样的: 它应该是什么样子

http://i1224.photobucket.com/albums/ee362/Devolutor/COPimage2_zps8981c846.png

http://i1224.photobucket.com/albums/ee362/Devolutor/COPimage2_zps8981c846.png

回答by JB Nizet

To have more space between the buttons, use a number of pixels beiiger than 4 for the grid layout:

要在按钮之间留出更多空间,请在网格布局中使用比 4 大的像素数:

buttonPanel.setLayout(new GridLayout(n,n,4,4));

To have a grid rather than a flow at the right side, use a GridLayout (just like you did for your buttons, but with 4 rows and 2 columns) rather than a FlowLayout.

要在右侧使用网格而不是流,请使用 GridLayout(就像您为按钮所做的那样,但有 4 行和 2 列)而不是 FlowLayout。