Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8785759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:26:46+00:00 2026-06-13T21:26:46+00:00

The Jlabel is not showing up when I put it in the paint(Graphics2d g)

  • 0

The Jlabel is not showing up when I put it in the paint(Graphics2d g) method and I can’t figure out why.

My text class:

import java.awt.Color;
import java.awt.Graphics2D;

import javax.swing.JLabel;


public class Text {
    int ballX,ballY,squareX,squareY;
    Text text;
    private Game game;
    private Ball ball;
    private Racquet racquet;

    public  void main(){
        ballX = ball.getBallX();
        ballY = ball.getBallY();
        squareX = racquet.getSquareX();
        squareY = racquet.getSquareY();
    }

    public void paint(Graphics2D g) {

        g.setColor(Color.red);
        JLabel balltext = new JLabel("the ball is at " + ballX + ballY);
        balltext.setVisible(true);

        g.setColor(Color.green);
        JLabel squaretext = new JLabel("the ball is at " + squareX + squareY);
        squaretext.setVisible(true);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:26:48+00:00Added an answer on June 13, 2026 at 9:26 pm

    There are a few things not quite right with your code.

    Firstly, Text does not extend from anything that is paintable, so paint will never be called. Convention tends to favor overriding paintComponent of Swing components anyway.

    Also, you should always call super.paintXxx, this would have highlighted the problem in the first place.

    Secondly, components are normally added to some kind container which takes care of painting them for you.

    If you want to use Swing components in your program, I’d suggest taking a look at Creating a GUI With JFC/Swing.

    If you want to paint text, I’d suggest you take a look at 2D Graphics, in particular Working with Text APIs

    An bit more information about what it is you’re trying to achieve might also help

    Also, I’m not sure if this deliberate or not, but public void main(){ ins’t going to act as the main entry point of the program, it should be public static void main(String args[]), but you might just be using main as means to call into the class from else where 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I trying to put the text of a JLabel under its icon (centered) at
Why is the UI not showing up in my code below: public class GUI
Can anyone explain why the update method on printobserver is not being called when
When I am running this, JLabel is not visible, but when I resize window
My question is if JLabel (java swing) supports html4 and css2. I know, it
I have a JLabel which has a lot of text on it. Is there
I am having trouble moving this JLabel across this JPanel? I put the code
Hi is it possible to have a JLabel text growing to the right instead
Normally a left-orientated JLabel shortens text at the right by adding periods, e.g. Hello
I have simple problem as I am not much familiar with Java GUI. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.