19

我们如何在 GridLayout 中显示网格线?在 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"));
}
4

6 回答 6

19

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

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);
}

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

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);
    }
}

这应该给你在任何地方都有宽度的边界borderWidth,包括单元格之间和沿着外边缘。

于 2010-03-15T00:00:46.823 回答
9

Joe Carnahan 提到的粗边框问题有一个更简单的解决方法:GridLayout(10,10, -1, -1)将组件之间的垂直间隙和水平间隙设置为 -1。所以完整的代码是:

JPanel panel = new JPanel(new GridLayout(10,10, -1, -1));
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);
}
于 2012-08-13T12:00:17.957 回答
9

我找到了一个非常简单的解决方案:

    final GridBagLayout layout = new GridBagLayout();
    JPanel content = new JPanel(layout)
    {

        @Override
        public void paint(Graphics g)
        {
            super.paint(g);
            int[][] dims = layout.getLayoutDimensions();
            g.setColor(Color.BLUE);
            int x = 0;
            for (int add : dims[0])
            {
                x += add;
                g.drawLine(x, 0, x, getHeight());
            }
            int y = 0;
            for (int add : dims[1])
            {
                y += add;
                g.drawLine(0, y, getWidth(), y);
            }
        }

    };

编辑:对于这个解决方案,我只需覆盖 JPanel 的方法并在 JPanel 自己的图像之上手动paint()绘制网格。GridBagLayout.getLayoutDimensions()

于 2014-01-25T17:56:20.823 回答
0

我很想JLayeredPane在顶部放置一个不透明的组件,该组件根据网格绘制线条。

于 2010-03-14T22:20:00.293 回答
0

或者只需将面板的背景颜色设置为边框颜色,网格线就会像魔术一样出现:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class GridLayoutLines extends JFrame
{
    public GridLayoutLines()
    {
        JPanel grid = new JPanel( new GridLayout(10, 10, 2, 2) );
        grid.setBackground( Color.BLACK );
        grid.setBorder( new MatteBorder(2, 2, 2, 2, Color.BLACK) );

        for (int i = 0; i < 100; i++)
        {
            JLabel label = new JLabel();
            label.setText(" label" + i);
            label.setOpaque( true );
            grid.add( label );
        }

        add( grid );
    }

    public static void main(String[] args)
    {
        GridLayoutLines frame = new GridLayoutLines();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}
于 2010-03-15T02:39:45.640 回答
0
//http://www.geekssay.com/how-to-make-grid-layout-using-swing/

import java.awt.*;
import javax.swing.*;

class GridExample {

 private JFrame f;

 private JButton b1, b2, b3, b4, b5, b6;

 
public GridExample() {

 f = new JFrame("Grid Example");

 b1 = new JButton("Button 1");

 b2 = new JButton("Button 2");

 b3 = new JButton("Button 3");

 b4 = new JButton("Button 4");
 b5 = new JButton("Button 5");
 b6 = new JButton("Button 6");
 }
 
public void launchFrame() {
 f.setLayout (new GridLayout(3,2));
 
f.add(b1);

 f.add(b2);

 f.add(b3);
 f.add(b4);

 f.add(b5);
 f.add(b6);

 
f.pack();
 f.setVisible(true);

 }
 
public static void main(String args[]) {

 GridExample grid = new GridExample();
 grid.launchFrame();
 }
}
于 2013-01-15T17:43:45.110 回答