You need to override the method paintComponent
rather than paintMessage
. Adding the @Override
annotation over the method will show that paintMessage
is not a standard method of JComponent
. Also you may want to reduce the y-coordinate in your drawString
as the text is currently not visible due to the additional decoration dimensions of the JFrame
. Finally remember to call super.paintComponent
to repaint the background of the component.
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.RED);
g2.drawString("John", 5, 100);
}