如何在Java JTable的单元格之间设置空白像素?


要设置表格单元格之间的空白像素,即行和列空白像素,请使用 setIntercellSpacing() 方法 -

Dimension dim = new Dimension(50,2);
table.setIntercellSpacing(new Dimension(dim));

在上面,我们使用了 Dimension 类 -

以下是一个在 JTable 元格之间设置空白像素的示例 -

示例

package my;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class SwingDemo {
   public static void main(String[] argv) throws Exception {
      JFrame frame = new JFrame("Demo");
      JPanel panel = new JPanel();
      String data[][] = {
         {"Australia","5","1"},
         {"US","10","2"},
         {"Canada","9","3"},
         {"India","7","4"},
         {"Poland","2","5"},
         {"SriLanka","5","6"}
      };
      String col [] = {"Team","Selected Players","Rank"};
      DefaultTableModel tableModel = new DefaultTableModel(data,col);
      JTable table = new JTable(tableModel);
      Dimension dim = new Dimension(50,2);
      table.setIntercellSpacing(new Dimension(dim));
      JScrollPane scrollPane = new JScrollPane(table);
      panel.add(scrollPane);
      frame.add(panel);
      frame.setSize(600,400);
      frame.setUndecorated(true);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getRootPane().setWindowDecorationStyle(JRootPane.COLOR_CHOOSER_DIALOG);
      frame.setVisible(true);
   }
}

输出

更新于30-7-2019

489 次浏览

开启你的 职业生涯

完成课程以获得认证

开始学习
广告