我正在开发一款桌面应用程序,它需要比较列车时间到达和离开之间的日期,并突出显示各种颜色代码的差异。我目前面临两个问题: -
如果有人能帮忙解决这个问题,我们将非常感激。干杯!!!
import java.awt.Color;
import java.awt.Component;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
public class CellRenderer extends DefaultTableCellRenderer{
public CellRenderer(){
super.setOpaque(true);
}
private static final long serialVersionUID = 1L;
public Component getTableCellRendererComponent(JTable table, Object obj,
boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(table, obj,
isSelected, hasFocus, row, column);
/*Define Date format*/
SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yy HH.mm.ss");
/*Date Array Initialisation*/
Date [][] actDep = new Date[row+1][1];
Date [][] planDep = new Date[row+1][1];
Date [][] actArr = new Date[row+1][1];
Date [][] planArr = new Date[row+1][1];
String values=(String) table.getValueAt(row, column);
String actArr1 = (String) table.getValueAt(row, 6);
String planArr1 = (String) table.getValueAt(row, 3);
String actDep1 = (String) table.getValueAt(row, 7);
String planDep1 = (String) table.getValueAt(row, 4);
long [][] arrDiff = new long[row+1][1];
long [][] depDiff = new long[row+1][1];
/*Parse Date Arrays*/
for (int i = 0; i<=row; i++) {
if(values.equals("-"))
{
table.setValueAt("01-JAN-01 00.00.00", row, column);
}
try {
actDep[i][0] = format.parse(actDep1);
planDep[i][0] = format.parse(planDep1);
actArr[i][0] = format.parse(actArr1);
planArr[i][0] = format.parse(planArr1);
/*Calculate differences in actual and planned arrivals in milliseconds*/
arrDiff[i][0]=actArr[i][0].getTime() - planArr[i][0].getTime();
/*Calculate differences in actual and planned departures in milliseconds*/
depDiff[i][0]=actDep[i][0].getTime() - planDep[i][0].getTime();
}
catch(ParseException e) {
e.printStackTrace();
}
}
Color lightOrange = new Color(255,215,0);
Color darkOrange = new Color(255,140,0);
Color aqua = new Color(0,255,255);
Color pinky = new Color(255,0,255);
/**
* Colours have been highlighed in the table based on the following:
* Aqua - early arrivals and departures
* Green - no delay, departures within a minute
* Yellow - up to five minutes late
* Light Orange - 5-7 minutes late
* Dark Orange - 7-10 minutes late
* Red - later than 10 minutes
* Pink - unplanned arrivals and departures
* Gray - missed events
*/
for(int i=0; i<=row; i++){
if((column == 6) && (arrDiff[i][0] >= 600000 && arrDiff[i][0] < 21600000) || ((column == 7) && (depDiff[i][0] >= 600000 && depDiff[i][0] < 21600000)))
cell.setBackground(Color.RED);
else if((column == 6) && (arrDiff[i][0] >= 420000 && arrDiff[i][0] < 600000) || ((column == 7) && (depDiff[i][0] >= 420000 && depDiff[i][0] < 600000)))
cell.setBackground(darkOrange);
else if((column == 6) && (arrDiff[i][0] >= 300000 && arrDiff[i][0] < 420000) || ((column == 7) && (depDiff[i][0] >= 300000 && depDiff[i][0] < 420000)))
cell.setBackground(lightOrange);
else if((column == 6) && (arrDiff[i][0] >= 60000 && arrDiff[i][0] < 300000) || ((column == 7) && (depDiff[i][0] >= 60000 && depDiff[i][0] < 300000)))
cell.setBackground(Color.YELLOW);
else if((column == 6) && (arrDiff[i][0]== 0 && arrDiff[i][0] < 60000 && !actArr1.equals("01-JAN-01 00.00.00")) || ((column == 7) && (depDiff[i][0]== 0 && depDiff[i][0] < 60000 && !actDep1.equals("01-JAN-01 00.00.00"))))
cell.setBackground(Color.GREEN);
else if((column == 6) && (arrDiff[i][0]>= -600000 && arrDiff[i][0] < 0) || ((column == 7) && (depDiff[i][0]>= -600000 && depDiff[i][0] < 0)))
cell.setBackground(aqua);
else if((column == 6) && (arrDiff[i][0] > -600000 && !actArr1.equals("01-JAN-01 00.00.00")) || ((column == 7) && (depDiff[i][0] > -600000 && !actDep1.equals("01-JAN-01 00.00.00"))))
cell.setBackground(pinky);
else if((column == 6) && (arrDiff[i][0] < -600000) || ((column == 7) && (depDiff[i][0] < -600000)))
cell.setBackground(Color.GRAY);
else{
cell.setBackground(Color.WHITE);
cell.setForeground(Color.BLACK);
}
}
return cell;
}
}