JTABLE内容居中显⽰
要让JTable的内容居中显⽰⽅法如下:
JTable tabledata = new JTable();
DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 设置table内容居中
// tcr.setHorizontalAlignment(JLabel.CENTER);
tcr.setHorizontalAlignment(SwingConstants.CENTER);// 这句和上句作⽤⼀样
tabledata.setDefaultRenderer(Object.class, tcr);
html怎么让所有内容居中参考:
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
public class MyJTable extends JTable {
private MyCellRenderer renderer;
public MyJTable(MyCellRenderer renderer) {
super(8, 6);
}
public TableCellRenderer getCellRenderer(int row, int column) {
return renderer;
}
public static void main(String[] args) {
MyJTable t = new MyJTable(new MyCellRenderer());
JScrollPane pane1 = new JScrollPane(t);//
JFrame frame = new JFrame(“JTableDemo”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(pane1);
frame.pack();
frame.setVisible(true);
}
}
class MyCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
return this;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。