消除GridLayout元素之间的差距

时间:2016-10-04 14:51:06

标签: java swing

我正在尝试使用JTextFields创建GridLayout的9x9网格,但是我无法消除JTextFieldGridLayout之间的差距。

我尝试使用setHgapsetVgap方法将水平和垂直间隙设置为0,但差距仍然存在,我哪里出错?

这是我到目前为止所尝试的,

class BoardGUI extends JFrame implements ActionListener {
    private JPanel centerPanel, bottomPanel;
    private JTextField grid[][];
    private JButton loadBtn, saveBtn, solveBtn;
    private GridLayout gridLayout;
    private final int N = 9;
    private final int MAX_HEIGHT = 450;
    private final int MAX_WIDTH = 500;

    public BoardGUI() {
        super("Solver v2.0");
        centerPanel = new JPanel();
        bottomPanel = new JPanel();

        grid = new JTextField[N][N];
        for(int i=0; i<N; i++)
            for(int j=0; j<N; j++)
                grid[i][j] = new JTextField(1);

        loadBtn = new JButton("Load");
        saveBtn = new JButton("Save");
        solveBtn = new JButton("Solve");

        gridLayout = new GridLayout(N,N);
        gridLayout.setVgap(0);
        gridLayout.setHgap(0);

        initLayout();
        registerEventListeners();
    }

    private void initLayout() {
        centerPanel.setLayout(gridLayout);
        for(int i=0; i<N; i++)
            for(int j=0; j<N; j++)
                centerPanel.add(grid[i][j]);

        bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        bottomPanel.add(loadBtn);
        bottomPanel.add(saveBtn);
        bottomPanel.add(solveBtn);

        this.setLayout(new BorderLayout());
        this.getContentPane().add(centerPanel, BorderLayout.CENTER);
        this.getContentPane().add(bottomPanel, BorderLayout.SOUTH);
        this.setSize(MAX_WIDTH, MAX_HEIGHT);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    private void registerEventListeners() {
        loadBtn.addActionListener(this);
        saveBtn.addActionListener(this);
        solveBtn.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {

    }
}

图片:enter image description here

0 个答案:

没有答案