标签:stat lse private sele hit jtable value 背景 ace
1 package view; 2 3 import java.awt.Color; 4 import java.awt.Component; 5 6 import javax.swing.JLabel; 7 import javax.swing.JTable; 8 import javax.swing.table.DefaultTableCellRenderer; 9 10 public class SetTableBg { 11 public static void setColumnColor(JTable table) { 12 try 13 { 14 DefaultTableCellRenderer tcr = new DefaultTableCellRenderer(){ 15 private static final long serialVersionUID = 1L; 16 public Component getTableCellRendererComponent(JTable table,Object value, boolean isSelected, boolean hasFocus,int row, int column){ 17 if(row%2 == 0) 18 setBackground(Color.WHITE);//设置奇数行底色 19 else if(row%2 == 1) 20 setBackground(new Color(220,230,241));//设置偶数行底色 21 return super.getTableCellRendererComponent(table, value,isSelected, hasFocus, row, column); 22 } 23 }; 24 for(int i = 0; i < table.getColumnCount(); i++) { 25 table.getColumn(table.getColumnName(i)).setCellRenderer(tcr); 26 } 27 tcr.setHorizontalAlignment(JLabel.CENTER); 28 } 29 catch (Exception e){ 30 e.printStackTrace(); 31 } 32 } 33 34 }
将其中的Table换成你自己创建的Tabel就好了。
标签:stat lse private sele hit jtable value 背景 ace
原文地址:https://www.cnblogs.com/ustc-anmin/p/10233963.html