How do I place JComponents on a Jframe background image?,










-1















I'm new here
I need help in placing components on a background image in Jframe using BorderLayout.
I tried adding the components to the label then the label to the frame but it hasn't worked so far



Here's my code:



package com.hosp;

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

public class BackgroundImage extends JFrame

BackgroundImage()

//get image from package
ImageIcon img=new ImageIcon(getClass().getResource("backgroundimage.jpg"));

//add image to Label
JLabel imageLabel =new JLabel(img,JLabel.CENTER);

//button on screen
JButton btn =new JButton("Save");

//
add(imageLabel);
imageLabel.add(btn,BorderLayout.SOUTH);



public static void main(String args)

BackgroundImage frame = new BackgroundImage ();
frame.setVisible(true);
frame.setSize(300,300);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);











share|improve this question



















  • 1





    please show us what have you done so far?

    – Nipun Sampath
    Nov 12 '18 at 21:17











  • Please edit your post and add the relevant code as well as any errors or stack traces encountered.

    – The Head Rush
    Nov 12 '18 at 21:22











  • @The Head Rush. There's been no error so far

    – Omega
    Nov 12 '18 at 22:24











  • I'd be astonished if there was no duplicate question among this lot of Q&As.. General tips: 1) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 2) Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and use it consistently.

    – Andrew Thompson
    Nov 13 '18 at 0:48












  • @AndrewThompson Thanks but they were typo errors I forgot to edit

    – Omega
    Nov 13 '18 at 3:11















-1















I'm new here
I need help in placing components on a background image in Jframe using BorderLayout.
I tried adding the components to the label then the label to the frame but it hasn't worked so far



Here's my code:



package com.hosp;

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

public class BackgroundImage extends JFrame

BackgroundImage()

//get image from package
ImageIcon img=new ImageIcon(getClass().getResource("backgroundimage.jpg"));

//add image to Label
JLabel imageLabel =new JLabel(img,JLabel.CENTER);

//button on screen
JButton btn =new JButton("Save");

//
add(imageLabel);
imageLabel.add(btn,BorderLayout.SOUTH);



public static void main(String args)

BackgroundImage frame = new BackgroundImage ();
frame.setVisible(true);
frame.setSize(300,300);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);











share|improve this question



















  • 1





    please show us what have you done so far?

    – Nipun Sampath
    Nov 12 '18 at 21:17











  • Please edit your post and add the relevant code as well as any errors or stack traces encountered.

    – The Head Rush
    Nov 12 '18 at 21:22











  • @The Head Rush. There's been no error so far

    – Omega
    Nov 12 '18 at 22:24











  • I'd be astonished if there was no duplicate question among this lot of Q&As.. General tips: 1) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 2) Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and use it consistently.

    – Andrew Thompson
    Nov 13 '18 at 0:48












  • @AndrewThompson Thanks but they were typo errors I forgot to edit

    – Omega
    Nov 13 '18 at 3:11













-1












-1








-1








I'm new here
I need help in placing components on a background image in Jframe using BorderLayout.
I tried adding the components to the label then the label to the frame but it hasn't worked so far



Here's my code:



package com.hosp;

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

public class BackgroundImage extends JFrame

BackgroundImage()

//get image from package
ImageIcon img=new ImageIcon(getClass().getResource("backgroundimage.jpg"));

//add image to Label
JLabel imageLabel =new JLabel(img,JLabel.CENTER);

//button on screen
JButton btn =new JButton("Save");

//
add(imageLabel);
imageLabel.add(btn,BorderLayout.SOUTH);



public static void main(String args)

BackgroundImage frame = new BackgroundImage ();
frame.setVisible(true);
frame.setSize(300,300);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);











share|improve this question
















I'm new here
I need help in placing components on a background image in Jframe using BorderLayout.
I tried adding the components to the label then the label to the frame but it hasn't worked so far



Here's my code:



package com.hosp;

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

public class BackgroundImage extends JFrame

BackgroundImage()

//get image from package
ImageIcon img=new ImageIcon(getClass().getResource("backgroundimage.jpg"));

//add image to Label
JLabel imageLabel =new JLabel(img,JLabel.CENTER);

//button on screen
JButton btn =new JButton("Save");

//
add(imageLabel);
imageLabel.add(btn,BorderLayout.SOUTH);



public static void main(String args)

BackgroundImage frame = new BackgroundImage ();
frame.setVisible(true);
frame.setSize(300,300);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);








java image swing jframe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 15:01









The Head Rush

1,15211425




1,15211425










asked Nov 12 '18 at 21:13









OmegaOmega

42




42







  • 1





    please show us what have you done so far?

    – Nipun Sampath
    Nov 12 '18 at 21:17











  • Please edit your post and add the relevant code as well as any errors or stack traces encountered.

    – The Head Rush
    Nov 12 '18 at 21:22











  • @The Head Rush. There's been no error so far

    – Omega
    Nov 12 '18 at 22:24











  • I'd be astonished if there was no duplicate question among this lot of Q&As.. General tips: 1) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 2) Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and use it consistently.

    – Andrew Thompson
    Nov 13 '18 at 0:48












  • @AndrewThompson Thanks but they were typo errors I forgot to edit

    – Omega
    Nov 13 '18 at 3:11












  • 1





    please show us what have you done so far?

    – Nipun Sampath
    Nov 12 '18 at 21:17











  • Please edit your post and add the relevant code as well as any errors or stack traces encountered.

    – The Head Rush
    Nov 12 '18 at 21:22











  • @The Head Rush. There's been no error so far

    – Omega
    Nov 12 '18 at 22:24











  • I'd be astonished if there was no duplicate question among this lot of Q&As.. General tips: 1) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 2) Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and use it consistently.

    – Andrew Thompson
    Nov 13 '18 at 0:48












  • @AndrewThompson Thanks but they were typo errors I forgot to edit

    – Omega
    Nov 13 '18 at 3:11







1




1





please show us what have you done so far?

– Nipun Sampath
Nov 12 '18 at 21:17





please show us what have you done so far?

– Nipun Sampath
Nov 12 '18 at 21:17













Please edit your post and add the relevant code as well as any errors or stack traces encountered.

– The Head Rush
Nov 12 '18 at 21:22





Please edit your post and add the relevant code as well as any errors or stack traces encountered.

– The Head Rush
Nov 12 '18 at 21:22













@The Head Rush. There's been no error so far

– Omega
Nov 12 '18 at 22:24





@The Head Rush. There's been no error so far

– Omega
Nov 12 '18 at 22:24













I'd be astonished if there was no duplicate question among this lot of Q&As.. General tips: 1) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 2) Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and use it consistently.

– Andrew Thompson
Nov 13 '18 at 0:48






I'd be astonished if there was no duplicate question among this lot of Q&As.. General tips: 1) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 2) Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and use it consistently.

– Andrew Thompson
Nov 13 '18 at 0:48














@AndrewThompson Thanks but they were typo errors I forgot to edit

– Omega
Nov 13 '18 at 3:11





@AndrewThompson Thanks but they were typo errors I forgot to edit

– Omega
Nov 13 '18 at 3:11












1 Answer
1






active

oldest

votes


















0














public class backgroundImage extends JFrame{ 


Class names should start with an upper case character



add(imageLabel);
imageLabel.add(btn,BorderLayout.SOUTH);
validate();


  1. Unlike a JPanel, a JLabel isn't designed to be a "container" to hold other components so it doesn't have a default layout manager so you can't just add the component to it using a BorderLayout constraint.


  2. There is not need for the validate() method. You would only use revalidate() and repaint() when you add a component to a panel AFTER the frame is visible.


So your code should look something like:



add(imageLabel);
imageLabel.setLayout( new BorderLayout() );
imageLabel.add(btn,BorderLayout.SOUTH);
//validate();


Note this will only work if the components you add to the label are smaller than the size of the image, otherwise the component will be truncated to fit in the area of the label.



Check out Background Panel for a more flexible solution.






share|improve this answer

























  • The button does show on the frame but its background is separate. Is there and way to place the button on the image?

    – Omega
    Nov 13 '18 at 12:42












  • The button does display on the label. It displays on the bottom of the label because you are using the BorderLayout.SOUTH. If you want the button somewhere else then use a different layout manager. For example use a FlowLayout on the label to see the difference. Or use a GridBagLayout to see the button in the center.

    – camickr
    Nov 13 '18 at 15:26











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270198%2fhow-do-i-place-jcomponents-on-a-jframe-background-image%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














public class backgroundImage extends JFrame{ 


Class names should start with an upper case character



add(imageLabel);
imageLabel.add(btn,BorderLayout.SOUTH);
validate();


  1. Unlike a JPanel, a JLabel isn't designed to be a "container" to hold other components so it doesn't have a default layout manager so you can't just add the component to it using a BorderLayout constraint.


  2. There is not need for the validate() method. You would only use revalidate() and repaint() when you add a component to a panel AFTER the frame is visible.


So your code should look something like:



add(imageLabel);
imageLabel.setLayout( new BorderLayout() );
imageLabel.add(btn,BorderLayout.SOUTH);
//validate();


Note this will only work if the components you add to the label are smaller than the size of the image, otherwise the component will be truncated to fit in the area of the label.



Check out Background Panel for a more flexible solution.






share|improve this answer

























  • The button does show on the frame but its background is separate. Is there and way to place the button on the image?

    – Omega
    Nov 13 '18 at 12:42












  • The button does display on the label. It displays on the bottom of the label because you are using the BorderLayout.SOUTH. If you want the button somewhere else then use a different layout manager. For example use a FlowLayout on the label to see the difference. Or use a GridBagLayout to see the button in the center.

    – camickr
    Nov 13 '18 at 15:26
















0














public class backgroundImage extends JFrame{ 


Class names should start with an upper case character



add(imageLabel);
imageLabel.add(btn,BorderLayout.SOUTH);
validate();


  1. Unlike a JPanel, a JLabel isn't designed to be a "container" to hold other components so it doesn't have a default layout manager so you can't just add the component to it using a BorderLayout constraint.


  2. There is not need for the validate() method. You would only use revalidate() and repaint() when you add a component to a panel AFTER the frame is visible.


So your code should look something like:



add(imageLabel);
imageLabel.setLayout( new BorderLayout() );
imageLabel.add(btn,BorderLayout.SOUTH);
//validate();


Note this will only work if the components you add to the label are smaller than the size of the image, otherwise the component will be truncated to fit in the area of the label.



Check out Background Panel for a more flexible solution.






share|improve this answer

























  • The button does show on the frame but its background is separate. Is there and way to place the button on the image?

    – Omega
    Nov 13 '18 at 12:42












  • The button does display on the label. It displays on the bottom of the label because you are using the BorderLayout.SOUTH. If you want the button somewhere else then use a different layout manager. For example use a FlowLayout on the label to see the difference. Or use a GridBagLayout to see the button in the center.

    – camickr
    Nov 13 '18 at 15:26














0












0








0







public class backgroundImage extends JFrame{ 


Class names should start with an upper case character



add(imageLabel);
imageLabel.add(btn,BorderLayout.SOUTH);
validate();


  1. Unlike a JPanel, a JLabel isn't designed to be a "container" to hold other components so it doesn't have a default layout manager so you can't just add the component to it using a BorderLayout constraint.


  2. There is not need for the validate() method. You would only use revalidate() and repaint() when you add a component to a panel AFTER the frame is visible.


So your code should look something like:



add(imageLabel);
imageLabel.setLayout( new BorderLayout() );
imageLabel.add(btn,BorderLayout.SOUTH);
//validate();


Note this will only work if the components you add to the label are smaller than the size of the image, otherwise the component will be truncated to fit in the area of the label.



Check out Background Panel for a more flexible solution.






share|improve this answer















public class backgroundImage extends JFrame{ 


Class names should start with an upper case character



add(imageLabel);
imageLabel.add(btn,BorderLayout.SOUTH);
validate();


  1. Unlike a JPanel, a JLabel isn't designed to be a "container" to hold other components so it doesn't have a default layout manager so you can't just add the component to it using a BorderLayout constraint.


  2. There is not need for the validate() method. You would only use revalidate() and repaint() when you add a component to a panel AFTER the frame is visible.


So your code should look something like:



add(imageLabel);
imageLabel.setLayout( new BorderLayout() );
imageLabel.add(btn,BorderLayout.SOUTH);
//validate();


Note this will only work if the components you add to the label are smaller than the size of the image, otherwise the component will be truncated to fit in the area of the label.



Check out Background Panel for a more flexible solution.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 1:52

























answered Nov 13 '18 at 1:44









camickrcamickr

275k15127239




275k15127239












  • The button does show on the frame but its background is separate. Is there and way to place the button on the image?

    – Omega
    Nov 13 '18 at 12:42












  • The button does display on the label. It displays on the bottom of the label because you are using the BorderLayout.SOUTH. If you want the button somewhere else then use a different layout manager. For example use a FlowLayout on the label to see the difference. Or use a GridBagLayout to see the button in the center.

    – camickr
    Nov 13 '18 at 15:26


















  • The button does show on the frame but its background is separate. Is there and way to place the button on the image?

    – Omega
    Nov 13 '18 at 12:42












  • The button does display on the label. It displays on the bottom of the label because you are using the BorderLayout.SOUTH. If you want the button somewhere else then use a different layout manager. For example use a FlowLayout on the label to see the difference. Or use a GridBagLayout to see the button in the center.

    – camickr
    Nov 13 '18 at 15:26

















The button does show on the frame but its background is separate. Is there and way to place the button on the image?

– Omega
Nov 13 '18 at 12:42






The button does show on the frame but its background is separate. Is there and way to place the button on the image?

– Omega
Nov 13 '18 at 12:42














The button does display on the label. It displays on the bottom of the label because you are using the BorderLayout.SOUTH. If you want the button somewhere else then use a different layout manager. For example use a FlowLayout on the label to see the difference. Or use a GridBagLayout to see the button in the center.

– camickr
Nov 13 '18 at 15:26






The button does display on the label. It displays on the bottom of the label because you are using the BorderLayout.SOUTH. If you want the button somewhere else then use a different layout manager. For example use a FlowLayout on the label to see the difference. Or use a GridBagLayout to see the button in the center.

– camickr
Nov 13 '18 at 15:26


















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270198%2fhow-do-i-place-jcomponents-on-a-jframe-background-image%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo