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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:37:04+00:00 2026-05-16T15:37:04+00:00

How do I add background image on this code? i tried everything but the

  • 0

How do I add background image on this code? i tried everything but the image just won’t show.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class sampleProg extends JFrame {

private Image img;

public void sampleProg(){


ImageIcon icon = new ImageIcon("bg.jpg");

       img=icon.getImage();
}

public void paint (Graphics g){

    g.drawImage(img,0,0,getSize().width,getSize().height,this);
    super.paint(g);

}



JButton button = new JButton ("Result");
JButton button2 = new JButton ("Clear");


JLabel label = new JLabel ("Full Name");
JLabel label2 = new JLabel ("Age");
JLabel label3 = new JLabel ("English");
JLabel label4 = new JLabel ("Mathematics");
JLabel label5 = new JLabel ("Science");
JLabel label6 = new JLabel ("Social Studies");
JLabel label7 = new JLabel ("Height in cm");
JLabel label8 = new JLabel ("Weight in lbs");
JLabel label9 = new JLabel ("Message");
JLabel label10 = new JLabel ("Average");
JLabel label11 = new JLabel ("Remarks");
JLabel label12 = new JLabel ("Laurize Albarracin");



JTextField text = new JTextField ("");
JTextField text2 = new JTextField ("");
JTextField text3 = new JTextField ("");
JTextField text4 = new JTextField ("");
JTextField text5 = new JTextField ("");
JTextField text6 = new JTextField ("");
JTextField text7 = new JTextField ("");
JTextField text8 = new JTextField ("");
JTextField text9 = new JTextField ("");
JTextField text10 = new JTextField ("");
JTextField text11 = new JTextField ("");

int average;

JPanel background = new JPanel();
JFrame frame = new JFrame();

public sampleProg (String str){
    super(str);

    background.setLayout (null);
    background.setBounds (30,50,90,20);

    button.setBounds(350,170,90,20);
    button2.setBounds(450,170,90,20);


    label.setBounds(30,90,90,20);
    label2.setBounds(30,130,90,20);
    label3.setBounds(30,170,90,20);
    label4.setBounds(30,210,90,20);
    label5.setBounds(30,250,90,20);
    label6.setBounds(30,290,90,20);
    label7.setBounds(350,90,90,20);
    label8.setBounds(350,130,90,20);
    label9.setBounds(350,210,90,20);
    label10.setBounds(350,250,90,20);
    label11.setBounds(350,290,90,20);
    label12.setBounds(300,35,150,20);




    text.setBounds(130,90,190,20);
    text2.setBounds(130,130,190,20);
    text3.setBounds(130,170,190,20);
    text4.setBounds(130,210,190,20);
    text5.setBounds(130,250,190,20);
    text6.setBounds(130,290,190,20);
    text7.setBounds(450,90,250,20);
    text8.setBounds(450,130,250,20);
    text9.setBounds(450,210,250,20);
    text10.setBounds(450,250,250,20);
    text11.setBounds(450,290,250,20);

    button.addActionListener (new ActionListener () {
        public void actionPerformed (ActionEvent e){

            text9.setText("Hi! "+text.getText()+"you are"+text2.getText()+"years of age and now you stand"+text7.getText()+"in cm while you're weight is"+text8.getText()+"in lbs. These are your remark and average");
            int English = Integer.parseInt (text3.getText());
            int Mathematics = Integer.parseInt (text4.getText());
            int Science = Integer.parseInt (text5.getText());
            int SocialStudies = Integer.parseInt(text6.getText());

            average = (English+Mathematics+Science+SocialStudies)/4;
            text10.setText(Integer.toString(average)); 

        }   
    }
    );

    button2.addActionListener (new ActionListener(){
        public void actionPerformed(ActionEvent e){
            text.setText("");
            text2.setText("");
            text3.setText("");
            text4.setText("");
            text5.setText("");
            text6.setText("");
            text7.setText("");
            text8.setText("");
            text9.setText("");
            text10.setText("");
            text11.setText("");

        }
    });

    background.add(button);
    background.add(button2);
    background.add(label);
    background.add(label2);
    background.add(label3);
    background.add(label4);
    background.add(label5);
    background.add(label6);
    background.add(label7);
    background.add(label8);
    background.add(label9);
    background.add(label10);
    background.add(label11);
    background.add(label12);

    background.add(text);       
    background.add(text2);
    background.add(text3);
    background.add(text4);
    background.add(text5);
    background.add(text6);
    background.add(text7);
    background.add(text8);
    background.add(text9);
    background.add(text10);
    background.add(text11);

    getContentPane().add(background);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    pack();
     }

    public sampleProg(){

    setLayout(new FlowLayout());
}

     public static void main (String[]args){    

     sampleProg frame = new sampleProg("Swing Application");
     frame.setSize(730,350);
     frame.show();

}


}
  • 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-16T15:37:05+00:00Added an answer on May 16, 2026 at 3:37 pm

    Your code never sets the img field. Move the code from the void sampleProg() method into your constructor. You should also set the background panel’s background to a transparent color for the image to show through. Also, you should paint the image on the background panel rather than on the frame, otherwise it will always be covered up. You can create an anonymous panel and move your paint() code into it instead.

    There are cleaner ways to do this found in the link Mondain provided.

    public class sampleProg extends JFrame {
        JPanel background = new JPanel() {
            public void paint (Graphics g){
                g.drawImage(img,0,0,getSize().width,getSize().height,this);
                super.paint(g);
            }
        };
    
        public sampleProg(string str) {
            //...
    
            ImageIcon icon = new ImageIcon("bg.jpg");
            img=icon.getImage();
    
            //...
    
            Color transparent = new Color(0, true);
            background.setBackground(transparent);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 509k
  • Answers 509k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer -Mfoo simply generates the code use foo; and places it… May 16, 2026 at 4:36 pm
  • Editorial Team
    Editorial Team added an answer yes: The purpose of Weight is to ensure searching does… May 16, 2026 at 4:36 pm
  • Editorial Team
    Editorial Team added an answer // Here... fstat(fd, &fs); // And here... n=read(fd, buf, 10);… May 16, 2026 at 4:36 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm probably just being daft but my Google searches are not working out well.
i would like to add an image that acts like a border after every
I have a webpage with a tiled background body image. It needs to be
Basically I've got the same background image throughout my entire iphone application. I don't
I've got an Custom UIButton . It's got a static background image a variable
I have posted several questions related to this problem I am having and I
I've been trying to recreate an effect from this tutorial: http://jqueryfordesigners.com/jquery-look-tim-van-damme/ Unfortunately, I want
I am trying to add the UIButton to UIScrollView . I have added the
Please refer to this page for reference: http://loadedgranola.valitics.com/_product_83484/blackberry_lime I have a jQuery script that
I'm trying to make a subclassed UITableViewCell where I draw an image in the

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.