package table;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import sun.swing.table.DefaultTableCellHeaderRenderer;
public class SimpleTableTest extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
protected JTable table;
protected Object[][] data;
protected String[] colname = {"ID","书名","作者","主角","类别","选择"};
public SimpleTableTest(){
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
createTableData(); //创建表格所需数据
table = getSimpleTable();
JScrollPane jsPane = new JScrollPane(table);
pane.add(jsPane,BorderLayout.CENTER);
}
public void createTableData(){
data = new Object[10][6];
int i =0;
data[i++] = new Object[]{"01","射雕英雄传","金庸","郭靖","武侠",true};
data[i++] = new Object[]{"02","神雕侠侣" ,"金庸","杨过","武侠",false};
data[i++] = new Object[]{"03","笑傲江湖" ,"金庸","令狐冲","武侠",true};
data[i++] = new Object[]{"04","鹿鼎记" ,"金庸","韦小宝","武侠",false};
data[i++] = new Object[]{"05","大旗英雄传" ,"古龙","铁中棠","武侠",false};
data[i++] = new Object[]{"06","陆小凤传奇" ,"古龙","陆小凤","武侠",false};
data[i++] = new Object[]{"07","多情剑客无情剑" ,"古龙","李寻欢","武侠",false};
data[i++] = new Object[]{"08","三国演义" ,"罗贯中","无","古典名著",false};
data[i++] = new Object[]{"09","封神演义" ,"陈仲琳","无","古典名著",false};
data[i++] = new Object[]{"10","绿野仙踪" ,"李百川","冷于冰","古典名著",false};
}
public static void main(String[] args){
SimpleTableTest stt= new SimpleTableTest();
stt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
stt.setSize(400, 200);
stt.setVisible(true);
}
/**
* 返回一个简单的表格
* @return
*/
public JTable getSimpleTable(){
TableValues tv = new TableValues(data,colname);
table = new JTable(tv);
setTableStyle(table);
return table;
}
/**
* 设置表格风格
* @param tb
*/
public void setTableStyle(JTable tb){
// 表头文字居中
DefaultTableCellHeaderRenderer rh = new DefaultTableCellHeaderRenderer();
rh.setHorizontalAlignment(JLabel.CENTER);
tb.getTableHeader().setDefaultRenderer(rh);
//设置表头的背景色
tb.getTableHeader().setBackground(Color.BLUE);
//设置表头的文字颜色
tb.getTableHeader().setForeground(Color.RED);
//设置表头字体
tb.getTableHeader().setFont(new Font("隶书",Font.PLAIN,14));
// 设置行高
tb.setRowHeight(30);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.getColumn(colname[0]).setPreferredWidth(150); //设置第1列的宽度
table.getColumn(colname[1]).setPreferredWidth(140); //设置第2列的宽度
table.setBackground(new Color(244, 244, 242)); //设置表格背景
tb.setForeground(Color.GREEN); //设置表格颜色
tb.setFont(new Font("隶书",Font.PLAIN,14)); //设置表格字体
// 隐藏最后一列
table.getColumn(colname[colname.length - 1]).setMaxWidth(0);
table.getColumn(colname[colname.length - 1]).setMinWidth(0);
table.getColumn(colname[colname.length - 1]).setPreferredWidth(0);
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import sun.swing.table.DefaultTableCellHeaderRenderer;
public class SimpleTableTest extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
protected JTable table;
protected Object[][] data;
protected String[] colname = {"ID","书名","作者","主角","类别","选择"};
public SimpleTableTest(){
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
createTableData(); //创建表格所需数据
table = getSimpleTable();
JScrollPane jsPane = new JScrollPane(table);
pane.add(jsPane,BorderLayout.CENTER);
}
public void createTableData(){
data = new Object[10][6];
int i =0;
data[i++] = new Object[]{"01","射雕英雄传","金庸","郭靖","武侠",true};
data[i++] = new Object[]{"02","神雕侠侣" ,"金庸","杨过","武侠",false};
data[i++] = new Object[]{"03","笑傲江湖" ,"金庸","令狐冲","武侠",true};
data[i++] = new Object[]{"04","鹿鼎记" ,"金庸","韦小宝","武侠",false};
data[i++] = new Object[]{"05","大旗英雄传" ,"古龙","铁中棠","武侠",false};
data[i++] = new Object[]{"06","陆小凤传奇" ,"古龙","陆小凤","武侠",false};
data[i++] = new Object[]{"07","多情剑客无情剑" ,"古龙","李寻欢","武侠",false};
data[i++] = new Object[]{"08","三国演义" ,"罗贯中","无","古典名著",false};
data[i++] = new Object[]{"09","封神演义" ,"陈仲琳","无","古典名著",false};
data[i++] = new Object[]{"10","绿野仙踪" ,"李百川","冷于冰","古典名著",false};
}
public static void main(String[] args){
SimpleTableTest stt= new SimpleTableTest();
stt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
stt.setSize(400, 200);
stt.setVisible(true);
}
/**
* 返回一个简单的表格
* @return
*/
public JTable getSimpleTable(){
TableValues tv = new TableValues(data,colname);
table = new JTable(tv);
setTableStyle(table);
return table;
}
/**
* 设置表格风格
* @param tb
*/
public void setTableStyle(JTable tb){
// 表头文字居中
DefaultTableCellHeaderRenderer rh = new DefaultTableCellHeaderRenderer();
rh.setHorizontalAlignment(JLabel.CENTER);
tb.getTableHeader().setDefaultRenderer(rh);
//设置表头的背景色
tb.getTableHeader().setBackground(Color.BLUE);
//设置表头的文字颜色
tb.getTableHeader().setForeground(Color.RED);
//设置表头字体
tb.getTableHeader().setFont(new Font("隶书",Font.PLAIN,14));
// 设置行高
tb.setRowHeight(30);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.getColumn(colname[0]).setPreferredWidth(150); //设置第1列的宽度
table.getColumn(colname[1]).setPreferredWidth(140); //设置第2列的宽度
table.setBackground(new Color(244, 244, 242)); //设置表格背景
tb.setForeground(Color.GREEN); //设置表格颜色
tb.setFont(new Font("隶书",Font.PLAIN,14)); //设置表格字体
// 隐藏最后一列
table.getColumn(colname[colname.length - 1]).setMaxWidth(0);
table.getColumn(colname[colname.length - 1]).setMinWidth(0);
table.getColumn(colname[colname.length - 1]).setPreferredWidth(0);
}
}