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

发布于 2024-08-24 22:28:07 字数 244 浏览 1 评论 0原文

我们如何在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"));
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

倾城°AllureLove 2024-08-31 22:28:07

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

但是,这会给单元格之间的边框比面板边缘的边框更厚,因为外边缘只会有一个一像素厚的边框,内部边缘将有两个一像素厚的边框在一起。要解决这个问题,您可以使用 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 的边框,两者之间细胞和沿着外边缘。

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

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

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

吾性傲以野 2024-08-31 22:28:07

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

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:

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);
}
说不完的你爱 2024-08-31 22:28:07

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

    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 的 paint() 方法并手动绘制由 GridBagLayout.getLayoutDimensions() 定义的网格在 JPanel 自己的图像之上。

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.

心不设防 2024-08-31 22:28:07

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

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

滴情不沾 2024-08-31 22:28:07

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

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

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);
    }
}
就像说晚安 2024-08-31 22:28:07
//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();
 }
}
//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();
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文