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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:49:15+00:00 2026-06-14T07:49:15+00:00

So, following from my last post Java Button Width im looking to add some

  • 0

So, following from my last post Java Button Width im looking to add some images and set a background color. Ive tried a few things, just everytime i do it. It always gives me errors.

i’ve tried

setBackground(args);

and

img = addImage("image.png");

they dont work for me. Can somebody give me a hand please?

Ok i tried the post made by Disha. And the applet still stays the same color, not black

http://pastebin.com/iijj7fSr

  • 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-14T07:49:16+00:00Added an answer on June 14, 2026 at 7:49 am

    At the beginning, Please do learn Java Naming Conventions and stick to them.

    In order for you to provide a background Color to your JFrame, since you had added one JPanel to the CENTER.
    Hence you cannot get one background color by writing :

    interfaceFrame.setBackground(Color.black);
    

    Now you have to set the opaque property of the JPanel to true and set one Background color for the same like :

    setOpaque(true);
    setBackground(Color.BLUE);
    

    inside your MenuPane Class’s constructor.

    Here here is your modified code :

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Gmine {
            JFrame interfaceFrame;
            JButton singleplayerButton, multiplayerButton, optionsButton, quitButton;
    
    
            public Gmine() {
                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) {
                        }
    
                        interfaceFrame = new JFrame("G-Mine");
                        interfaceFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        interfaceFrame.setLayout(new BorderLayout());
                        interfaceFrame.setSize(800,500);
                        //interfaceFrame.setBackground(Color.black);
                        interfaceFrame.add(new MenuPane());
                        interfaceFrame.setLocationRelativeTo(null);
                        interfaceFrame.setVisible(true);
                    }
                });
            }
    
            public class MenuPane extends JPanel {
    
                public MenuPane() {
                    setLayout(new GridBagLayout());
    
                    setOpaque(true);
                    setBackground(Color.BLUE);
    
                    singleplayerButton = new JButton("SinglePLayer");
                    multiplayerButton = new JButton("MultiPlayer");
                    optionsButton = new JButton("Options");
                    quitButton = new JButton("Quit");
    
                    GridBagConstraints gbc = new GridBagConstraints();
                    gbc.gridx = 0;
                    gbc.gridy = 0;
                    gbc.fill = GridBagConstraints.HORIZONTAL;
                    gbc.ipadx = 20;
                    gbc.ipady = 20;
    
                    add(singleplayerButton, gbc);
                    gbc.gridy++;
                    add(multiplayerButton, gbc);
                    gbc.gridy++;
                    add(optionsButton, gbc);
                    gbc.gridy++;
                    add(quitButton, gbc);
                }
            }
            public static void main(String[] args) {
                new Gmine();
            }
    }
    

    Now in order to add images to your project you can either see this answer for how to add images to your Project in Java and you can get help from this small sample code as well which is as follows :

    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class PaintingExample
    {
        private CustomPanel contentPane;
        private JTextField userField;
        private JPasswordField passField;
        private JButton loginButton;
    
        private void displayGUI()
        {
            JFrame frame = new JFrame("Painting Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            contentPane = new CustomPanel();        
    
            frame.setContentPane(contentPane);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new PaintingExample().displayGUI();
                }
            });
        }
    }
    
    class CustomPanel extends JPanel
    {
        private BufferedImage image;
    
        public CustomPanel()
        {
            setOpaque(true);
            setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
            try
            {
                /*
                 * Since Images are Application Resources,
                 * it's always best to access them in the
                 * form of a URL, instead of File, as you are doing.
                 * Uncomment this below line and watch this answer
                 * of mine, as to HOW TO ADD IMAGES TO THE PROJECT
                 * https://stackoverflow.com/a/9866659/1057230
                 * In order to access images with getClass().getResource(path)
                 * here your Directory structure has to be like this
                 *                 Project
                 *                    |
                 *         ------------------------
                 *         |                      |
                 *        bin                    src
                 *         |                      |
                 *     ---------             .java files             
                 *     |       |                   
                 *  package   image(folder)
                 *  ( or              |
                 *   .class        404error.jpg
                 *   files, if
                 *   no package
                 *   exists.)
                 */
                //image = ImageIO.read(
                //      getClass().getResource(
                //              "/image/404error.jpg"));
                image = ImageIO.read(new URL(
                            "http://gagandeepbali.uk.to/" + 
                                    "gaganisonline/images/404error.jpg"));
            }
            catch(IOException ioe)
            {
                System.out.println("Unable to fetch image.");
                ioe.printStackTrace();
            }
        }
    
        /*
         * Make this one customary habbit,
         * of overriding this method, when
         * you extends a JPanel/JComponent,
         * to define it's Preferred Size.
         * Now in this case we want it to be 
         * as big as the Image itself.
         */
        @Override
        public Dimension getPreferredSize()
        {
            return (new Dimension(image.getWidth(), image.getHeight()));
        }
    
        /*
         * This is where the actual Painting
         * Code for the JPanel/JComponent
         * goes. Here we will draw the image.
         * Here the first line super.paintComponent(...),
         * means we want the JPanel to be drawn the usual 
         * Java way first, then later on we will
         * add our image to it, by writing the other line,
         * g.drawImage(...).
         */
        @Override
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, this);
        }
    }
    

    Do uncomment the lines given below and add your image at the specified location :

    image = ImageIO.read(
          getClass().getResource(
                  "/image/404error.jpg")); 
    

    If still in doubt, ask any question you might have, I’ll try to provide information, if it is within my bounds 🙂

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

Sidebar

Related Questions

Ref: Forms, Post and submit buttons Following on from my last question, I've attempted
Following from my last question which @Jon Skeet gave me a lot of help
In TestDriven.Net I can set the following from the TestDriven.Net Options Pane Run tests
Im trying to get the last string from a URL for example... http://www.mywebsite/blog/this-post I
Hi i need to post some data to a web server from windows phone
I am studying the sample code from the last answer on this post to
Following from this: Every specific weekday, sql and php I got this: deals_bookings.everyWeekDay =
Following from these question Subset sum problem and Sum-subset with a fixed subset size
I have noted the following from a website: The JVM HotSpot memory is split
Is the following From header incorect? // To send HTML mail, the Content-type header

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.