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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2444004/
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-08-13 07:44:52  来源:igfitidea点击:

How do we show the gridline in GridLayout?

javaswinggrid-layout

提问by Jessy

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

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

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

采纳答案by Joe Carnahan

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:

我会尝试通过在添加组件时向组件添加边框来实现。这样做的简单方法就是使用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);
}

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:

但是,这将使单元格之间的边框比面板边缘的边框更厚,因为外边缘将只有一个 1 像素厚的边框,而内边缘将有两个 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);
    }
}

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

这应该为您提供borderWidth无处不在的宽度边界,无论是在单元格之间还是沿着外边缘。

回答by Tom Hawtin - tackline

I would be tempted to use JLayeredPaneto place a non-opaque component over the top that draws lines based on the grid.

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

回答by camickr

Or just set the background color of the panel to be your border color and the gridlines will appear like magic:

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

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

回答by Saulius ?im?ikas

There is an easier work around to the thick borders problem mentioned by Joe Carnahan: GridLayout(10,10, -1, -1)sets the vertical gaps and the horizontal gaps between components to -1. So the full code is:

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

回答by gomzee

//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();
?}
}

回答by Frederic Leitenberger

I found a very simple solution:

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

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

    };

EDIT: For this solution i simply override the paint()method of the JPanel and paint the grid as defined by GridBagLayout.getLayoutDimensions()manually on top of the JPanel's own image.

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