编辑:根据 camickr 的建议,我将 JTable 添加到滚动窗格中。他/她似乎是正确的,布局管理器正在改变列的大小。
我有一个 50 行和 40 列的 JTable。每个单元格要么不显示任何内容,要么显示一个字符。
我试图通过将行高和列宽设置为相同的值来使每个单元格成为正方形。
public IFace() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 966, 740);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
mntmFile = new JMenuItem("File");
menuBar.add(mntmFile);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new MigLayout("", "[457.00,grow][grow]", "[][grow][center][][grow]"));
JToolBar toolBar = new JToolBar();
contentPane.add(toolBar, "cell 0 0");
lblPalette = new JLabel("Palette");
contentPane.add(lblPalette, "cell 1 0");
AsciiTableModel myTableModel = new AsciiTableModel();
tableASCII = new JTable(myTableModel);
tableASCII.getSelectionModel();
tableASCII.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
tableASCII.setCellSelectionEnabled(true);
tableASCII.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
System.out.println(tableASCII.getColumnModel().getColumn(0).getMaxWidth());
final int cellSize = 20;
for(int i = 0; i < tableASCII.getColumnCount(); i++){
tableASCII.getColumnModel().getColumn(i).setPreferredWidth(cellSize);
System.out.println(tableASCII.getColumnModel().getColumn(i).getWidth());
}
for(int i = 0; i < tableASCII.getRowCount(); i++){
tableASCII.setRowHeight(i, cellSize );
}
contentPane.add(tableASCII, "cell 0 1,span 0 5");
btnFill = new JButton("Fill");
contentPane.add(btnFill, "flowx,cell 1 2");
textFill = new JTextField();
textFill.setHorizontalAlignment(SwingConstants.CENTER);
contentPane.add(textFill, "cell 1 2");
textFill.setColumns(10);
}
调用setPreferredWidth(cellSize)
后的输出System.out.println(tableASCII.getColumnModel().getColumn(i).getWidth());
是75
。应该是20。
这是 tableASCII 使用的 tablemodel 的代码。
public class AsciiTableModel extends AbstractTableModel {
public final int NUM_CHARACTERS_PER_CELL = 1;
private int numColumns = 50;
private int numRows = 40;
private List<ArrayList<Character>> asciiChars = new ArrayList<ArrayList<Character>>();
public AsciiTableModel() {
createListElements();
System.out.println(asciiChars.get(0).size());
asciiChars.get(2).set(0, 'C');
asciiChars.get(1).set(0, 'a');
asciiChars.get(1).set(1, 't');
}
public AsciiTableModel(int rows, int columns) {
numColumns = columns;
numRows = rows;
}
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return numColumns;
}
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return numRows;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return asciiChars.get(rowIndex).get(columnIndex);
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if(aValue != null) {
//System.out.println(aValue.toString());
asciiChars.get(rowIndex).set(columnIndex, aValue.toString().charAt(1));
//System.out.println(asciiChars.get(rowIndex).get(columnIndex));
}
}
void createListElements() {
for(int i = 0; i < numRows; i++) {
asciiChars.add(new ArrayList<Character>());
for(int j = 0; j < numColumns; j++) {
asciiChars.get(i).add(' ');
}
}
}