日期:2014-05-20  浏览次数:20760 次

java中如何实现复杂表头
请各位高手留步,帮忙解释一下

------解决方案--------------------
什么复杂表头?
------解决方案--------------------

蛮复杂
------解决方案--------------------
刚学JAVA想找个高手帮下忙 

------解决方案--------------------
//下面是我改的一个双表头的的例子,关键部分代码,希望对你有帮助
//----------------------------------
Header header = new Header(30);
header.add(new HeaderCellLabel( "开奖日期 ", 0, 0, 1, 1), new HeaderCellConstraints(0, 0, 1, 2)); //format(列号,行号,列数,行数)
header.add(new HeaderCellLabel( "期号 ", 0, 0, 1, 1), new HeaderCellConstraints(1, 0, 1, 2));
header.add(new HeaderCellLabel( "销售额(元) ", 0, 0, 1, 1), new HeaderCellConstraints(2, 0, 1, 2));
header.add(new HeaderCellLabel( "开奖结果 ", 0, 0, 1, 1), new HeaderCellConstraints(3, 0, 1, 2));
header.add(new HeaderCellLabel( "特等奖 ", 0, 0, 1, 1), new HeaderCellConstraints(4, 0, 2, 1));
header.add(new HeaderCellLabel( "注数 ", 0, 0, 1, 1), new HeaderCellConstraints(4, 1, 1, 1));
header.add(new HeaderCellLabel( "每注奖金 ", 0, 0, 1, 1), new HeaderCellConstraints(5, 1, 1, 1));
header.add(new HeaderCellLabel( "一等奖 ", 0, 0, 1, 1), new HeaderCellConstraints(6, 0, 2, 1));
header.add(new HeaderCellLabel( "注数 ", 0, 0, 1, 1), new HeaderCellConstraints(6, 1, 1, 1));
header.add(new HeaderCellLabel( "每注奖金 ", 0, 0, 1, 1), new HeaderCellConstraints(7, 1, 1, 1));
header.add(new HeaderCellLabel( "二等奖 ", 0, 0, 1, 1), new HeaderCellConstraints(8, 0, 2, 1));
header.add(new HeaderCellLabel( "注数 ", 0, 0, 1, 1), new HeaderCellConstraints(8, 1, 1, 1));
header.add(new HeaderCellLabel( "每注奖金 ", 0, 0, 1, 1), new HeaderCellConstraints(9, 1, 1, 1));
header.add(new HeaderCellLabel( "三等奖 ", 0, 0, 1, 1), new HeaderCellConstraints(10, 0, 2, 1));
header.add(new HeaderCellLabel( "注数 ", 0, 0, 1, 1), new HeaderCellConstraints(10, 1, 1, 1));
header.add(new HeaderCellLabel( "每注奖金 ", 0, 0, 1, 1), new HeaderCellConstraints(11, 1, 1, 1));
header.add(new HeaderCellLabel( "四等奖 ", 0, 0, 1, 1), new HeaderCellConstraints(12, 0, 2, 1));
header.add(new HeaderCellLabel( "注数 ", 0, 0, 1, 1), new HeaderCellConstraints(12, 1, 1, 1));
header.add(new HeaderCellLabel( "每注奖金 ", 0, 0, 1, 1), new HeaderCellConstraints(13, 1, 1, 1));
header.add(new HeaderCellLabel( "五等奖 ", 0, 0, 1, 1), new HeaderCellConstraints(14, 0, 2, 1));
header.add(new HeaderCellLabel( "注数 ", 0, 0, 1, 1), new HeaderCellConstraints(14, 1, 1, 1));
header.add(new HeaderCellLabel( "每注奖金 ", 0, 0, 1, 1), new HeaderCellConstraints(15, 1, 1, 1));
// header.add(new HeaderCellLabel( " <html> aa <p> bb <p> cc </html> ", 0, 0, 1, 1),new HeaderCellConstraints(3, 1, 1, 3));

return header;
}


class Header extends MyHeader {
private int rowHeight = 20;

public Header(int rowHeight) {
setLayout(new HeaderLayout());

this.rowHeight = rowHeight;
}

public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
HeaderLayout layout = (HeaderLayout) getLayout();
d.height = layout.getRowCount() * rowHeight;

return d;
}
}


class HeaderCellLabel extends JButton {

public HeaderCellLabel(Icon icon, int top, int left, int bottom, int right) {
this( " ", icon, top, left, bottom, right);