如何更改 JTable 中单元格的颜色以提供动画效果?
How to change color of cells in JTable to provide an effect of animation?
下面我提供我的可复制代码。问题是它用灰色填充所有单元格。但是,我只需要从第 0 列到最后一列(第 0 行)制作一个灰色单元格 "moving"。
此外,如何在队列中创建多个单元"moving"?
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
的当前值计算行和列,并与单元格渲染器 row/column 的单元格进行比较
下面我提供我的可复制代码。问题是它用灰色填充所有单元格。但是,我只需要从第 0 列到最后一列(第 0 行)制作一个灰色单元格 "moving"。
此外,如何在队列中创建多个单元"moving"?
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
的当前值计算行和列,并与单元格渲染器 row/column 的单元格进行比较