日期:2014-05-20 浏览次数:20869 次
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestPanel extends JPanel {
private String data;
public final static int cols = 4;
public final static int x0 = 10; //x偏移量
public final static int y0 = 10; //y偏移量
public final static int rowH = 20;//行距
public final static int colH = 20;//列间距
public static void main(String args[]) {
JFrame f = new JFrame();
f.getContentPane().add(new TestPanel("abcdadsdfs"));
f.setSize(400, 400);
f.setVisible(true);
}
public TestPanel(String str) {
this.data = str;
}
@Override
public void paint(Graphics g) {
super.paint(g);
paintData(g);
}
private void paintData(Graphics g) {
int x = 0;
int y = 0;
int col = 0;
int row = 0;
for(int i =0; i < data.length(); i++) {
col = i % cols;
row = i / cols;
x = x0 + col * rowH;
y = y0 + row * colH;
g.drawString(data.substring(i, i + 1), x, y);
}
}
}
------解决方案--------------------
public void paint(Graphics g) {
}
paint 个点不就o了
自己控制好坐标
------解决方案--------------------
用JTable显示不就行了,每个Cell显示三个字母,如果加上HTML的tag还能改变字体和颜色。