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

  • Home
  • SEARCH
  • 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 6020805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:37:17+00:00 2026-05-23T03:37:17+00:00

I am writing a basic timer program with Java, and in the program I

  • 0

I am writing a basic timer program with Java, and in the program I would like to have a menu bar at the top. The code I have as of now is:

public Main() {
    JMenuBar menubar = new JMenuBar();

    setJMenuBar(menubar);

    JMenu menu = new JMenu("Test");
    menubar.add(menu);

    JMenuItem menuitem = new JMenuItem("Item");
    menu.add(menuitem);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500, 500);
    setTitle(Namer.name);
    new Timer(delay, timer).start();
    new Timer(1, new Refresher()).start();
    setResizable(false);
    setVisible(true);
}

public void paint(Graphics g) {
    Graphics buffer = unscreen.getGraphics();
    buffer.setColor(Color.white);
    buffer.fillRect(0, 0, 500, 500);
    buffer.setColor(Color.black);
    buffer.setFont(new Font("Times New Roman", Font.PLAIN, 25));
    // buffer.drawString("hours:minutes:seconds: ", 25, 100);
    buffer.drawString(hourss + numhours + ":" + minutess + numminutes + ":"
            + secondss + numseconds, 100, 100);
    g.drawImage(unscreen, 0, 0, null);
}

When I run this code, I get everything I would expect, which is some numbers showing how long the program has been up and a menu bar at the top of the screen, except the menu bar. I have tried commenting out the paint method, and when i do that it works. Is there a better way to do what I am doing or a different solution to my problem? Also, I don’t need to have the paint method there if there is a better way to print stuff on the window.

  • 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-05-23T03:37:17+00:00Added an answer on May 23, 2026 at 3:37 am

    It is not a good idea to paint the JFrame directly. It may be less troublesome to extend a JComponent, override its paintComponent() to do your custom painting, and add it to the JFrame. This way the JMenuBar goes on the JFrame and everything works fine.

       public class DrawingSurface extends JComponent{
    
              public void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics buffer = unscreen.getGraphics();
                buffer.setColor(Color.white);
                buffer.fillRect(0, 0, 500, 500);
                buffer.setColor(Color.black);
                buffer.setFont(new Font("Times New Roman", Font.PLAIN, 25));
                // buffer.drawString("hours:minutes:seconds: ", 25, 100);
                buffer.drawString(hourss + numhours + ":" + minutess + numminutes + ":"
                 + secondss + numseconds, 100, 100);
                g.drawImage(unscreen, 0, 0, null);
             }
       }
    

    Now you add an instance of DrawingSurface to the JFrame.

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

Sidebar

Related Questions

I'm writing a basic planet viewer OpenGL Perl application, just for fun. I have
I'm actually writing an MPI program. This is a basic client / server pattern.
I got this doubt while writing some code. Is 'bool' a basic datatype defined
I am writing a very basic web spider in java.I am facing one problem,
I am writing a basic word processing application and am trying to settle on
I'm writing a basic crawler that simply caches pages with PHP. All it does
I'm writing a basic Django application. For testing / development purposes I'm trying to
I'm interested in writing some basic computerized object recognition application, so I figure I
I'm in the process of writing a basic cookie for an ecommerce site which
I'm currently writing some methods that do some basic operations on form controls eg

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.