我们如何在GridLayout中显示网格线? [英] How do we show the gridline in GridLayout?

查看:4538
本文介绍了我们如何在GridLayout中显示网格线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何在GridLayout中显示网格线?在Java?

How do we show the gridline in GridLayout? in Java?

JPanel panel = new JPanel(new GridLayout(10,10));
panel.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));

for (int i =0; i<(10*10); i++){
   panel.add(new JLabel("Label"));
}


推荐答案

我会尝试这样做通过在添加组件时为组件添加边框。这样做的简单方法就是使用 BorderFactory.createLineBorder(),如下所示:

I would try to do it by adding borders to the components as they are added. The simple way to do it is just using BorderFactory.createLineBorder(), like this:

JPanel panel = new JPanel(new GridLayout(10,10));
panel.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));

for (int i =0; i<(10*10); i++){
    final JLabel label = new JLabel("Label");
    label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    panel.add(label);
}

然而,这将使细胞之间的边界比边缘更厚面板,因为外边缘只有一个像素厚的边框,内边缘将有两个一像素厚的边框。要解决此问题,您可以使用 BorderFactory.createMatteBorder()仅在任何地方绘制一个像素宽的边框:

However, that will give you thicker borders between the cells than at the edges of the panel, because the outer edges will only have a one-pixel thick border and the inside edges will have two one-pixel thick borders together. To work around that, you can use BorderFactory.createMatteBorder() to only draw one-pixel-wide borders everywhere:

final int borderWidth = 1;
final int rows = 10;
final int cols = 10;
JPanel panel = new JPanel(new GridLayout(rows, cols));
panel.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));

for (int row = 0; row < rows; row++) {
    for (int col = 0; col < cols; col++) {
        final JLabel label = new JLabel("Label");
        if (row == 0) {
            if (col == 0) {
                // Top left corner, draw all sides
                label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            }
            else {
                // Top edge, draw all sides except left edge
                label.setBorder(BorderFactory.createMatteBorder(borderWidth, 
                                                                0, 
                                                                borderWidth, 
                                                                borderWidth, 
                                                                Color.BLACK));
            }
        }
        else {
            if (col == 0) {
                // Left-hand edge, draw all sides except top
                label.setBorder(BorderFactory.createMatteBorder(0, 
                                                                borderWidth, 
                                                                borderWidth, 
                                                                borderWidth, 
                                                                Color.BLACK));
            }
            else {
                // Neither top edge nor left edge, skip both top and left lines
                label.setBorder(BorderFactory.createMatteBorder(0, 
                                                                0, 
                                                                borderWidth, 
                                                                borderWidth, 
                                                                Color.BLACK));
            }
        }
        panel.add(label);
    }
}

这应该给你宽度<$ c $的边框c> borderWidth 无处不在,单元格之间和外边缘。

This should give you borders of width borderWidth everywhere, both between cells and along the outside edges.

这篇关于我们如何在GridLayout中显示网格线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆