如何在swing中给某一行,某一列,某几个连续的单元格,某几个离散的单元格添加背景颜色呢?这四种情况各有不同吗?
谢谢大家了!
以下是我的源代码,没有添加背景颜色的功能,该怎么改?
public class TableUseModel {
public static void main(String[] args){
JFrame frame = new InvestmentTableFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class InvestmentTableFrame extends JFrame{
public InvestmentTableFrame(){
setTitle("InvestmentTable");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
InvestmentTableModel model = new InvestmentTableModel(30,5,10);
JTable table = new JTable(model);
add(new JScrollPane(table));
Object last = model.getValueAt(5,3);
last.toString();
System.out.println("the number in (5,3) is : " + last);
}
private static final int DEFAULT_WIDTH = 400;
private static final int DEFAULT_HEIGHT = 200;
}
class InvestmentTableModel extends AbstractTableModel{
public InvestmentTableModel(int y,int r1,int r2){
years = y;
minRate = r1;
maxRate = r2;
}
public int getRowCount(){
return years;
}
public int getColumnCount(){
return maxRate - minRate +1;
}
public Object getValueAt(int r,int c){
return getValue(r,c);
}
public double getValue(int x,int y){
double value=x+y+1;
return value;
}
public String getColumnName(int c){
return (c+minRate)+"%";
}
public double getOne(int x,int y){
return getValue(x,y);
}
private int years;
private int minRate;
private int maxRate;
private static double INITIAL_BALANCE = 100000.0;
}