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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:24:06+00:00 2026-06-02T00:24:06+00:00

I create a JPanel and added a few simple buttons with listeners attached to

  • 0

I create a JPanel and added a few simple buttons with listeners attached to them. Then I decided to add an Image to the background of my panel, so I switched my JPanel to an ImagePanel. The buttons were working on JPanel, but now that I added a bunch of code for the background image to be displayed, the buttons no longer show. I did not change of any of the button adding code so I’m very confused as to why the buttons no longer show. This also happened in my separate GameFrame class. I added 2 rectangle components to a panel, then 3 buttons. For that panel, only the buttons show, despite the rectangles working before the buttons were added. Can I only have one type of JComponent per panel or something? I really do not understand why it’s doing this. Thank you for your time.

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TitleFrame extends JFrame
{
    private ImagePanel panel;
    private JButton mage;
    private JButton rogue;
    private JButton warrior;
    private Image image;

    public TitleFrame()
    {
        JFrame frame = new JFrame();

        frame.setSize(1024, 768);
        frame.setTitle("Title Screen");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        createMageButton();
        createRogueButton();
        createWarriorButton();
        ImagePanel panel = new ImagePanel(new ImageIcon("C:/Users/Derek Reitz/Documents/Eclipse Projects/GameApp/src/background.jpg").getImage());
        panel.add(mage);
        panel.add(rogue);
        panel.add(warrior);
        panel.paintComponent(frame.getGraphics());
        frame.getContentPane().add(panel);
    }

    private void createRogueButton() {
        rogue = new JButton("Select Rogue");

        class AddButtonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                moveToNextFrame('r');
            }
        }

        ActionListener listener = new AddButtonListener();
        rogue.addActionListener(listener);
    }

    private void createWarriorButton() {
        warrior = new JButton("Select Warrior");

        class AddButtonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                moveToNextFrame('w');
            }

        }

        ActionListener listener = new AddButtonListener();
        warrior.addActionListener(listener);
    }

    private void createMageButton() {
        mage = new JButton("Select Mage");

        class AddButtonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                moveToNextFrame('m');
            }
        }

        ActionListener listener = new AddButtonListener();
        mage.addActionListener(listener);
    }

    public void moveToNextFrame(char c) 
    {
        GameFrame game = new GameFrame(c);
    }

    class ImagePanel extends JPanel 
    {

        private Image img;

        public ImagePanel(Image img) {
            this.img = img;
            Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
            setPreferredSize(size);
            setMinimumSize(size);
            setMaximumSize(size);
            setSize(size);
            setLayout(null);
        }

        public void paintComponent(Graphics g) {
            g.drawImage(img, 0, 0, null);
        }

    }
}
  • 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-02T00:24:07+00:00Added an answer on June 2, 2026 at 12:24 am

    You set as layout null. That is a special case where absolute positions of the components are accepted. So use `setBounds(x, y, width, height). Better still use a real layout.

    Another remark, you can take the image from the class path, say from out of the resulting .jar file):

    URL url = getClass().getResource("/background.jpg");
    ... new ImageIcon(url);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JPanel for which I set some image as the background. I
I have made two panels and then added in third panel. How can I
I need create custom dialog and put JPanel into it. Is it possible?
Is there a simply layout manager I can use in a JPanel to create
In short, my need is to have a background Image in my java app,
i need to create a line graph inside a JPanel using paintComponent. I don't
I have to create a Matlab figure window with custom maximize/minimize and close buttons.
column header not visible in my JTable i have created a JPanel and added
I want to create a 'generic' JPanel which can be rendered in any of
I've created JPanel and have already added components into it and I'm going to

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.