1. /**
  2.  *  [RoundColorTable.java]   间隔色的 JTable
  3.  *
  4.  * 创建日期:(2003-8-11)
  5.  * @author:ONE_Fox
  6.  */
  7. import  java.awt.*;
  8. import  java.awt.event.*;
  9. import  javax.swing.*;
  10. import  javax.swing.table.*;
  11. public  class RoundColorTable extends JFrame {
  12.     private String[] colname = {"第1列","第2列","第3列","第4列","第5列"}; //表头信息
  13.     private String[][] data = new String[10][5]; //表内容
  14.     
  15.     //界面组件----------------------//
  16.     private JScrollPane scroPanel = new JScrollPane(); //中底层滚动面板
  17.     private DefaultTableModel model; //列表默认TableModel
  18.     private JTable table;
  19.     
  20. /**
  21.  * 构造方法 SelectionDemo()
  22.  */     
  23.     public RoundColorTable() {
  24.         makeFace();
  25.         addListener();
  26.         showFace();
  27.     }
  28. /**
  29.  * 方法: 界面构建 makeFace()
  30.  */
  31.     private void makeFace() {
  32.         
  33.         //表内容数组 data[][] 赋值------------//
  34.         for(int i = 0; i < 10; i++) {
  35.             for(int j = 0; j < 5; j++) {
  36.                 data[i][j] = "( " + (j+1) + ", " + (i+1) + " )";
  37.             }
  38.         }
  39.         
  40.         table = new JTable(model = new DefaultTableModel(data,colname));
  41.         
  42.         //新建列表现器------------------------//
  43.         DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
  44.             public Component getTableCellRendererComponent(JTable table, 
  45.                           Object value, boolean isSelected, boolean hasFocus, 
  46.                                                      int row, int column) {
  47.                     
  48.                 if(row%2 == 0)
  49.                     setBackground(Color.white); //设置奇数行底色
  50.                 else if(row%2 == 1)
  51.                     setBackground(new Color(206,231,255));  //设置偶数行底色
  52.                     
  53.                 return super.getTableCellRendererComponent(table, value, 
  54.                                           isSelected, hasFocus, row, column);
  55.             }
  56.         };
  57.         //设置列表现器------------------------//
  58.         for(int i = 0; i < colname.length; i++) {
  59.             table.getColumn(colname[i]).setCellRenderer(tcr);
  60.         }
  61.                 
  62.                 
  63.         scroPanel.getViewport().setBackground(Color.white);
  64.         scroPanel.getViewport().add(table);
  65.         
  66.         //总体界面布局------------------------//
  67.         getContentPane().add(scroPanel, BorderLayout.CENTER);
  68.     }
  69. /**
  70.  * 方法: 界面显示 showFace()
  71.  */
  72.     private void showFace() {
  73.         setTitle("间隔色的 JTable");
  74.         setSize(500,400);
  75.         Toolkit tmpTK = Toolkit.getDefaultToolkit();
  76.         Dimension dime = tmpTK.getScreenSize();
  77.         
  78.         setLocation(dime.width2 - 250, dime.height2 - 200);
  79.         show();
  80.     }
  81. /**
  82.  * 方法: 添加事件监听 addListener()
  83.  */    
  84.     private void addListener() {
  85.         this.addWindowListener(new WindowAdapter(){  //添加窗口关闭事件
  86.             public void windowClosing(WindowEvent e){
  87.                 setVisible(false);
  88.                 dispose();
  89.                 System.exit(0);
  90.             }
  91.         });
  92.     }
  93.         
  94.         
  95. /**
  96.  * 程序入口 main()
  97.  */
  98.     public static void main(String args[]) {
  99.         //获取设置系统风格-------------------//
  100.         try {
  101.             String laf = UIManager.getSystemLookAndFeelClassName();
  102.             UIManager.setLookAndFeel(laf);
  103.         } catch (Exception e) {}
  104.         
  105.         new RoundColorTable();
  106.     }
  107. }