gpt4 book ai didi

java - 让我的手机根据计时器开始闪烁

转载 作者:行者123 更新时间:2023-11-30 07:20:12 25 4
gpt4 key购买 nike

我有以下代码,当表格包含某些条件时,它基本上为单元格着色,在本例中,第 1 行第 2 列中为 A:

package javaapplication63;

import java.awt.*;
import java.util.*;

import javax.swing.*;
import javax.swing.table.*;
import javax.swing.Timer;

public class NewClass extends JFrame
{
ColorRenderer colorRenderer;

public NewClass()
{
String[] columnNames = {"Date", "String", "Integer", "Boolean"};
Object[][] data =
{
{new Date(), "A", new Integer(1), new Boolean(true)},
{new Date(), "B", new Integer(2), new Boolean(false)},
{new Date(), "C", new Integer(3), new Boolean(true)},
{new Date(), "D", new Integer(4), new Boolean(false)}
};

DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable( model )
{
public Class getColumnClass(int column)
{
return getValueAt(0, column).getClass();
}

// By overriding this method we can use a single renderer for every column

public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
{
Component c = super.prepareRenderer(renderer, row, column);
colorRenderer.setBackground(c, row, column);
return c;
}


};

table.setPreferredScrollableViewportSize(new Dimension(300,400));
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );

// Create blinking color renderer

colorRenderer = new ColorRenderer( table );

colorRenderer.setRowColor(1, Color.GREEN);

colorRenderer.startBlinking(1000);
}

public static void main(String[] args)
{
NewClass frame = new NewClass();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}

/*
** Color cell background
*/
class ColorRenderer
{
private JTable table;
private AbstractTableModel model;
private Map colors;
private boolean isBlinking = true;
private Timer timer;
private Point location;

public ColorRenderer(JTable table)
{
this.table = table;
model = (AbstractTableModel)table.getModel();
colors = new HashMap();
location = new Point();
}

public void setBackground(Component c, int row, int column)
{
// Don't override the background color of a selected cell

//if ( table.isCellSelected(row, column) ) return;

// The default render does not reset the background color
// that was set for the previous cell, so reset it here

if (c instanceof DefaultTableCellRenderer)
{
c.setBackground( table.getBackground() );
}

// Don't highlight this time

if ( !isBlinking ) return;

// In case columns have been reordered, convert the column number

column = table.convertColumnIndexToModel(column);

// Get cell color

Object key = getKey(row, column);
Object o = colors.get( key );

if (o != null)
{
c.setBackground( (Color)o );
return;
}

// Get row color

key = getKey(row, -1);
o = colors.get( key );

if (o != null)
{
c.setBackground( (Color)o );
return;
}

// Get column color

key = getKey(-1, column);
o = colors.get( key );

if (o != null)
{
c.setBackground( (Color)o );
return;
}

}

public void setCellColor(int row, int column, Color color)
{
Point key = new Point(row, column);
colors.put(key, color);
}



public void setRowColor(int row, Color color)
{
setCellColor(row, -1, color);
}

private Object getKey(int row, int column)
{
location.x = row;
location.y = column;
return location;
}

public void startBlinking(int interval)
{

if (model.getValueAt(0,1).equals("A")){//row 0 = first row/ column 2 = column 1
System.out.println("hello");
timer = new Timer(); //here is where i cant get the timer to start based on the interval
TimerTask tasknew = new TimerTask(interval) {
@Override
public void run() {

}
};
timer.schedule(tasknew, 2000, 2000);
}
//timer = new Timer(interval,this);
//timer.start();
}

public void stopBlinking()
{
timer.stop();
}


}
}

但是我似乎无法让计时器根据指定的时间间隔启动,这会使行闪烁有人可以帮忙吗?

最佳答案

有两个 Timer 类

javax.swing.Timer使用 Action 监听器并通过

启动它
timer = new Timer(4000, new ActionListener() {
private int i;
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("hello");
if (i%2 == 0)
setRowColor(1, Color.RED);
else
setRowColor(1, Color.GREEN);
i++;
table.revalidate();
table.repaint();
}
});
timer.start();

java.util.Timer使用 TimerTask 并通过

启动它
timer = new Timer();
timer.schedule(new TimerTask() {
private int i;
@Override
public void run() {
System.out.println("Hello");
if (i%2 == 0)
setRowColor(1, Color.RED);
else
setRowColor(1, Color.GREEN);
i++;
table.revalidate();
table.repaint();
}
}, 0, 4000);

在导入中,您导入 javax.swing.Timer 但将其用作 java.util.Timer

关于java - 让我的手机根据计时器开始闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37711899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com