JTable单元格中的图像偏离一个像素? [英] Images in JTable cells off by one pixel?

查看:96
本文介绍了JTable单元格中的图像偏离一个像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我现在可以将图像加载到JTable的单元格中,但是由于某种原因,图形全部向右移动了一个像素,从而使我能够看到JTable的背景.有任何想法吗?对不起,如果我的格式关闭了;仍然没有完全习惯这种标记.

So, I'm able to load images into my JTable's cells now, but for some reason the graphics are all shifted to the right by one pixel, allowing me to see the JTable's background. Any ideas? Sorry if my formatting's off; still not entirely used to this markup.

public static void main(String[] args) {  

  final int rows = 16;  
  final int columns = 16;  
  final int dimTile = 32;

  JFrame frame = new JFrame("test");  
  JTable table = new JTable(rows, columns);  
  table.setIntercellSpacing(new Dimension(0, 0));  
  table.setShowGrid(false);
  table.setBackground(Color.cyan);  
  table.setTableHeader(null);  
  table.setRowHeight(dimTile);  
  table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);  
  table.setPreferredSize(new Dimension(rows * dimTile, columns * dimTile));  

  Tile tile = new Tile(0);  
  for(int i = 0; i < rows; i++) {  
     for(int j = 0; j < columns; j++) {  
        table.getColumnModel().getColumn(j).setCellRenderer(new MyRenderer());  
        table.setValueAy(tile, i, j);  
     }  
  }  

  JScrollPane scrollPane = new JScrollPane(table, ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);  
scrollPane.setBorder(BorderFactory.createEmptyBorder());  

  frame.getContentPane().add(scrollPane);  
  frame.setSize(512, 512);  
  frame.setVisible(true);  
  int adjustedSizeX = frame.getInsets().left + frame.getInsets().right + 512;  
  int adjustedSizeY = frame.getInsets().top + frame.getInsets().bottom + 512;  
  frame.setSize(adjustedSizeX, adjustedSizeY);  
  frame.pack();  

  ...  
}  


public class MyRenderer extends DefaultTableCellRenderer {  
  @Override  
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {  
     super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);  

      Tile tile = (Tile) value;  
      setIcon(tile.getIcon());  
      return this;  
   }  
}  


public class Tile {  
  ImageIcon icon;  

  public Tile(int graphic) {  
     icon = new ImageIcon(PATH/TO/"...test.png");  
  }  

  public ImageIcon getIcon() {  
     return icon;  
  }  
}  

推荐答案

不能完全确定关闭一个像素"是什么意思-但要实现零单元间距,而没有任何视觉伪像,则必须将边距设置为零并关闭网格线:

Not entirely sure what you mean by "one pixel off" - but achieve zero intercell spacing without any visual artefacts you have to both zero the margins and turn off the grid lines:

table.setIntercellSpacing(new Dimension(0, 0)); 
table.setShowGrid(false)

修改

好的,仔细看,您的代码有几个问题

Okay, looking closer, there are several issues with your code

  • 您间接地而不是直接地进行列大小调整(还有另一个很好的示例,为什么永远不做component.setPreferredSize:-)
  • 渲染器的边框需要一定的大小

要修复第一个问题,请配置每个的宽度,表格布局将自动配置其自身

to fix the first, configure each column's width, table layout will automagically configure its itself

    final int rows = 16;
    final int columns = 16;
    Tile tile = new Tile(0);
    int tileHeight = tile.getIcon().getIconHeight();
    int tileWidth = tile.getIcon().getIconWidth();

    JTable table = new JTable(rows, columns);
    // remove all margin
    table.setIntercellSpacing(new Dimension(0, 0));
    table.setShowGrid(false);
    table.setTableHeader(null);
    // set the rowHeight
    table.setRowHeight(tileHeight);
    // turn off auto-resize
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    // configure each column with renderer and prefWidth
    for (int j = 0; j < columns; j++) {
        TableColumn column = table.getColumnModel().getColumn(j);
        column.setCellRenderer(new MyRenderer());
        column.setPreferredWidth(tileWidth);
    }

第二次,将每个呼叫的边框设为空:

for the second, null the border in each call:

public static class MyRenderer extends DefaultTableCellRenderer {
    @Override
    public Component getTableCellRendererComponent(JTable table,
            Object value, boolean isSelected, boolean hasFocus, int row,
            int column) {
        super.getTableCellRendererComponent(table, value, isSelected,
                hasFocus, row, column);
        setBorder(null);
        Tile tile = (Tile) value;
        setIcon(tile.getIcon());
        return this;
    }
}

这篇关于JTable单元格中的图像偏离一个像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆