感谢您的所有代码。我非常感谢那些通过贡献时间和精力真正帮助我们的人。那么所有这些解决方案都将解决问题。我用下面的代码得到了一个很好的解决方案: -
package com.swing.data;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
public class TableExample{
public static void main(String[] args) {
final JFrame f = new JFrame("TableExample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(createTable());
f.pack();
SwingUtilities.invokeLater(new Runnable(){
public void run() {
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
static JComponent createTable() {
final JTable table = new JTable(9,9) {
private static final long serialVersionUID = 0;
Color B = Color.RED;
Color C = this.getGridColor();
final Border[][] borders = {
{new ZoneBorder(C,B,B,B), new ZoneBorder(B,C,C,C), new ZoneBorder(B,B,C,C)},
{new ZoneBorder(C,C,C,B), new ZoneBorder(C,C,C,C), new ZoneBorder(C,B,C,C)},
{new ZoneBorder(C,C,B,B), new ZoneBorder(C,C,B,C), new ZoneBorder(C,B,B,C)}
};
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component result = super.prepareRenderer(renderer, row, column);
if (result instanceof JComponent) {
if(row == 0 && column == 0)
((JComponent) result).setBorder(borders[0][0]);
}
return result;
}
};
table.setRowHeight(28);
//table.setGridColor(Color.BLACK);
TableColumnModel tcm = table.getColumnModel();
for(int c = 0; c<table.getColumnCount(); ++c) {
TableColumn tc = tcm.getColumn(c);
tc.setPreferredWidth(28);
}
JPanel inner = new JPanel(new GridLayout());
//inner.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
inner.add(table);
return inner;
}
}
package com.swing.data;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class ZoneBorder implements Border {
private static final int WIDTH = 1;
private Color colorN, colorE, colorS, colorW;
public ZoneBorder(Color colorN, Color colorE, Color colorS, Color colorW) {
this.colorN=colorN;
this.colorE=colorE;
this.colorS=colorS;
this.colorW=colorW;
}
public boolean isBorderOpaque() {
return false;
}
public Insets getBorderInsets(Component c) {
return new Insets(WIDTH,WIDTH,WIDTH,WIDTH);
}
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Color old = g.getColor();
if (colorN != null) {
g.setColor(colorN);
g.fillRect(x, y, width, WIDTH);
}
if (colorE != null) {
g.setColor(colorE);
g.fillRect(x+width-WIDTH, y, WIDTH, height);
}
if (colorS != null) {
g.setColor(colorS);
g.fillRect(x, y+height-WIDTH, width, WIDTH);
}
if (colorW != null) {
g.setColor(colorW);
g.fillRect(x, y, WIDTH, height);
}
g.setColor(old);
}
}
谢谢!