-1

我有一个带有数字的 JTable。我知道如何改变一个单元格或所有单元格的颜色。但是如何改变单元格的颜色和动画呢?例如,第一个单元格为红色,有延迟,第二个单元格被涂成相同的红色,以此类推。

我继承了类 DefaultTableCellRenderer

    class paintCell extends DefaultTableCellRenderer {
    public Component getTableCellRendererComponent(JTable table,
            Object value, boolean isSelected, boolean hasFocus, int row,
            int column) {
        Component c = super.getTableCellRendererComponent(table, value,
                isSelected, hasFocus, row, column);
        return c;
    }
}

并设置方法 table.setDefaultRenderer(Object.class, new paintCell());

4

2 回答 2

1

创建javax.swing.Timer对象。在您的渲染器类中添加该int pointer字段并在. 然后,在方法中将参数转换为类型(如您所说,单元格中有一个数字)并将其与您的字段进行比较。如果是,将单元格设置为。PaintCellTimer.actionPerfomed()PaintCell.getTableCellRendererComponentvalueintpointerequals or lessbackgroundred

于 2013-05-02T13:01:39.177 回答
1
private JTable table;
private int index;
private void startAnimation() {
    Timer timer = new Timer(1000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            index++;
            if (index > table.getRowCount() * table.getColumnCount())
                index = 0;
            table.repaint();
        }
    });
    timer.setRepeats(true);
    timer.start();
}
class PaintCell extends DefaultTableCellRenderer {
    private static final long serialVersionUID = 1L;
    public Component getTableCellRendererComponent(JTable table,
            Object value, boolean isSelected, boolean hasFocus, int row,
            int column) {
        Component c = super.getTableCellRendererComponent(table, value,
                isSelected, hasFocus, row, column);
        int id = row * table.getRowCount() + column;
        c.setBackground(id < index ? Color.RED : null);
        return c;
    }
}

(SeniorJD比我快……但我写代码没有他的回答)

于 2013-05-02T13:05:48.883 回答