日期:2014-05-20 浏览次数:21021 次
import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.UIManager; import javax.swing.WindowConstants; import javax.swing.table.DefaultTableCellRenderer; public class Test extends JFrame { public Test() { Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); contentPane.add(new JTable(10, 10)); JTable t = new JTable(10, 10); headerRender render = new headerRender(); t.getTableHeader().setDefaultRenderer(render); t.getTableHeader().setPreferredSize(new Dimension(0, 50)); contentPane.add(new JScrollPane(t)); } public static void main(String args[]) { GraphicJavaWindowHandler.launch(new Test(), "Tables and Scrollpanes", 100, 100, 850, 700); } } class GraphicJavaWindowHandler extends WindowAdapter { public static void launch(final JFrame f, String title, final int x, final int y, final int w, int h) { f.setTitle(title); f.setBounds(x, y, w, h); f.setVisible(true); f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); f.addWindowListener(new WindowAdapter() { public void windowClosed(WindowEvent e) { System.exit(0); } }); } } class headerRender extends DefaultTableCellRenderer { public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { label.setText(value.toString()); label.setBorder(UIManager.getBorder("TableHeader.cellBorder")); Dimension d = label.getSize(); d.height = 36; label.setSize(d); return label; } JLabel label = new JLabel(); }