java - 如何改变JTable中单元格的颜色以提供动画效果?

标签 java swing animation jtable

下面我提供了我的可复制代码。问题是它用灰色填充所有单元格。但是,我只需要使灰色单元格从第 0 列“移动”到最后一列(对于第 0 行)。

此外,如何在队列中创建多个“移动”的单元格?

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.Timer;
import javax.swing.table.DefaultTableCellRenderer;

public class test2 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI(); 
            }
        });     
    }

    private static void createAndShowGUI() {
        gtest t= new gtest("TEST");
        f.pack();
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
    }
}  

class gtest extends JFrame
{

    private static JTable table;
    private int index;

    public gtest(String title)
    {
        table = new JTable(6, 10);
        table.setDefaultRenderer(Object.class, new PaintCell());
        add(table);
        startAnimation();       
    }

    private void startAnimation() {
        Timer timer = new Timer(100, 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 cell = super.getTableCellRendererComponent(table, value,
                    isSelected, hasFocus, row, column);
            int id = row * table.getRowCount() + column;
            cell.setBackground(id < index ? Color.LIGHT_GRAY : null);
            return cell;
        }
    }

}

最佳答案

改变...

int id = row * table.getRowCount() + column;
cell.setBackground(id < index ? Color.LIGHT_GRAY : null);

致...

int checkRow = index / table.getColumnCount();
int checkCol = index % table.getColumnCount();

cell.setBackground(checkRow == row && checkCol == column ? Color.LIGHT_GRAY : null);

这将根据index的当前值以及与单元格渲染器的单元格行/列的比较来计算行和列

关于java - 如何改变JTable中单元格的颜色以提供动画效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28233067/

相关文章:

javascript - 如何使用 JS 根据鼠标位置移动图像?

java - 将 HashMap 刷新到磁盘到有序集合中的最快方法

java - 出现 do-while 错误

java - 如何在 java 中删除 JTextArea 中的文本?

java - 将代码作为函数参数传递

ios - 通过导航 Controller 使用自定义动画转换到 SecondViewController 中的非事件 View 后

Java - Spring数据访问对象实现

java - JVMTI:FollowReferences:如何跳过软/弱/幻像引用?

java - JFrame getWidth() 和 getHeight() 即使在 pack() 之后也返回 0

wpf - 如何实现添加/删除列表项的淡入和淡出