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 8836373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:28:46+00:00 2026-06-14T09:28:46+00:00

Been trying to create a program which basically has a background (Which is painted

  • 0

Been trying to create a program which basically has a background (Which is painted using Paint() ) and then putting a Label with an Image in above that.

Keep getting the JLabel underneath the Paint…

Any ideas?

Thanks a lot in advance.


public class GUI extends JFrame {

    JPanel menuBar = new JPanel();
    JButton button1 = new JButton("Press Me");
    JLayeredPane layeredPane = new JLayeredPane();
    private ImageIcon image1;
    private static JLabel label1;

    public GUI() {
        super("Add a profile");

        setLayout(null);

        try {
            image1 = new ImageIcon(getClass().getResource(
                    "Images/location.PNG"));
        } catch (Exception e) {
            System.out.println("Image not found!");
        }
        label1 = new JLabel(image1);
        label1.setBounds(new Rectangle(new Point(262, 94), label1.getPreferredSize()));
        label1.setLocation(1, 1);
        label1.setSize(114, 105);
        add(label1);
    }

    public void paint(Graphics g) {
        paintComponents(g);
        Graphics2D g2d = (Graphics2D) g;

        // Menu Bar
        g2d.setColor(Color.BLACK);
        g2d.drawRect(60, 93, 190, 373);
        g2d.setColor(Color.GRAY);
        g2d.fillRect(61, 94, 189, 372);

        // Background box
        g2d.setColor(Color.BLACK);
        g2d.drawRect(281, 106, 560, 360);
        g2d.setColor(Color.GRAY);
        g2d.fillRect(282, 107, 559, 359);
    }

    public static void main(String[] args) {
        GUI gui = new GUI();
        gui.setVisible(true);
        gui.setSize(900, 550);
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setResizable(false);
        gui.setLocationRelativeTo(null);

    }

}

Sadly can’t get it to work, thanks a bunch anyway
Oh i didn’t see the images you did! Sorry! i’ll have a look through now

Really appreciate it all!

  • 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-14T09:28:47+00:00Added an answer on June 14, 2026 at 9:28 am

    From the looks of you’re code, you’re trying to use paint to do the work of other containers. I’d suggest, don’t.

    Don’t override the paint method of top level containers like JFrame, these methods do to much important work for you to messy about it with and frames have a number of components already laid out on top of them normally rendering any of your work obsolete.

    Instead, create your self a custom component (extending from something like JPanel) and use it’s paintComponent method instead THEN add this to the frame.

    In you’re case, you can get away with changing the background and border of the component and it will achieve the same result.

    UPDATED

    enter image description here

    With just some basic layouts and a couple of components, I was able to produce this…

    public class BadPaint {
    
        public static void main(String[] args) {
            new BadPaint();
        }
    
        public BadPaint() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new MenuPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class MenuPane extends JPanel {
    
            public MenuPane() {
                setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.weighty = 1;
                gbc.fill = GridBagConstraints.VERTICAL;
                gbc.insets = new Insets(10, 10, 10, 10);
    
                BackgroundPane left = new BackgroundPane();
                left.setLayout(new BorderLayout());
                JLabel label = new JLabel(" Menu ");
                label.setHorizontalAlignment(JLabel.CENTER);
                label.setVerticalAlignment(JLabel.CENTER);
                left.add(label);
                add(left, gbc);
    
                gbc.gridx++;
                gbc.weighty = 0;
                gbc.weightx = 1;
                gbc.fill = GridBagConstraints.BOTH;
                gbc.insets = new Insets(40, 10, 40, 10);
                BackgroundPane right = new BackgroundPane();
                right.setLayout(new BorderLayout());
                label = new JLabel(" Content ");
                label.setHorizontalAlignment(JLabel.CENTER);
                label.setVerticalAlignment(JLabel.CENTER);
                right.add(label);
                add(right, gbc);
    
            }
    
        }
    
        public class BackgroundPane extends JPanel {
    
            public BackgroundPane() {
                setBackground(Color.GRAY);
                setBorder(new LineBorder(Color.BLACK));
            }
    
        }
    
    }
    

    I’d suggest you would benefit from having a read through

    • Creating a GUI With JFC/Swing
    • A Visual Guide to Layout Managers
    • Using Layout Managers
    • Performing Custom Painting
    • Painting in AWT and Swing
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to create a simple program with Python which uses OpenCV
I have been trying to create a ListView which I can sort using drag
I am trying to create a program in which - I am putting this
I've been trying to create a client side editor which allows the end user
I have been trying to create an observable tweeter feed using tweetsharp with the
I'm trying to create a program which invokes an action as soon as new
I'm trying to create a program which gets the various notes in a sound
I'm trying to implement Window socket using Python. Mostly, everything has been so far
I've written a Python program which I distribute using pyinstaller. I've been using the
I have recently been trying to create an updater for my program. The updater

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.