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

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

我会尝试通过在添加组件时为组件添加边框来实现。 这样做的简单方法就是使用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边框,无论是在单元格之间还是在外边缘之间。

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

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

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

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

或者只是将面板的背景颜色设置为边框颜色,网格线将显示为魔术:

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