FontMetrics fm = g.getFontMetrics(); int fontHeight = fm.getHeight();
int x1 = Math.max(insets.left, clip.x); int x2 = Math.min(getWidth()-insets.right, clip.x + clip.width); int y = startRect.y + startRect.height - fm.getDescent();
g.setColor(Color.LIGHT_GRAY); while (y <= clip.y + clip.height) { g.drawLine(x1, y, x2, y); y += fontHeight; } } }; ta.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); //ta.setLineWrap(true);
JFrame f = new JFrame(); f.getContentPane().add(new JScrollPane(ta), BorderLayout.CENTER); f.pack(); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } }
http://blog.csdn.net/mq612/archive/2006/11/10/1377948.aspx
//
private ImageIcon image = null;
//
public ImageTextArea(ImageIcon image) {
this.image = image;
}
//
protected void paintComponent(Graphics g) {
setOpaque(false);
Dimension d = getSize();
for (int x = 0; x < d.width; x += image.getIconWidth()) {
for (int y = 0; y < d.height; y += image.getIconHeight()) {
g.drawImage(image.getImage(), x, y, this);
}
}
super.paintComponent(g);
}}
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.BadLocationException;public class T
{
public static void main(String[] args) {
JTextArea ta = new JTextArea(20, 20) {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Insets insets = getInsets();
Rectangle clip = getVisibleRect();
int startPos = viewToModel(new Point(clip.x, clip.y));
Rectangle startRect = null;
try {
startRect = modelToView(startPos);
} catch (BadLocationException e) {
e.printStackTrace();
}
FontMetrics fm = g.getFontMetrics();
int fontHeight = fm.getHeight();
int x1 = Math.max(insets.left, clip.x);
int x2 = Math.min(getWidth()-insets.right, clip.x + clip.width);
int y = startRect.y + startRect.height - fm.getDescent();
g.setColor(Color.LIGHT_GRAY);
while (y <= clip.y + clip.height) {
g.drawLine(x1, y, x2, y);
y += fontHeight;
}
}
};
ta.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
//ta.setLineWrap(true);
JFrame f = new JFrame();
f.getContentPane().add(new JScrollPane(ta), BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}