Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2017
    Rep Power

    Default Can't get both Image and Button in JFrame window

    Hi I'm new. To Java and to programming and to here.

    Been struggling with this problem for most of a day. No matter how I try it, I can't get a JButton and an Image to display in a JFrame window at the same time. I can get each to show up individually but not together. Not trying to make a button with an image inside it, I know how to do that.

    Attached my code. I think.


  2. #2
    gozzy is offline Member
    Join Date
    Jan 2017
    Rep Power

    Default Re: Can't get both Image and Button in JFrame window

    Look at
    Add a button and and label using the above to a JPanel and add that panel to the frame. Job done

    Don't use null layouts - you will soon get into difficulties until you know exactly what you're doing. Learn how Java layout managers work

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default Re: Can't get both Image and Button in JFrame window

    If you have code that you have questions, please post it here wrapped in code tags.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    cronnin is offline Member
    Join Date
    Jan 2017
    Rep Power

    Default Re: Can't get both Image and Button in JFrame window

    Yep, your code is not working. Try executing and reading this and then ask questions.

    Java Code:
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    public class ButtonAndImage {
            private JFrame frame;
            private MyCanvas canvas;
            private static int width = 800;
            private static int height = 600;
            public ButtonAndImage() {
                    frame = new JFrame();
                    frame.setSize(width, height);
                    canvas = new MyCanvas();
                    canvas.setBounds(100, 100, 200, 200);
    // important, try to change it with frame.add(canvas); to see what's happening
    // if you want to set your own bounds, you should bypass the default layout.  it doesn't do if you put it somewhere up
                    JButton b1 = new JButton("Click here");
            public static void main(String[] args) {
                    ButtonAndImage buttonAndImage = new ButtonAndImage();	
            private class MyCanvas extends JPanel {   // extend JPanel
                     public void paintComponent(Graphics g) {       //override paintComponent from JPanel
                            super.paintComponent(g);        // this here is important for the desired looks
                            String text = "No go, girl.";
                            try {
         // change  argument two lines below for local image
                                   URL localUrl = MyCanvas.class.getClassLoader().getResource("images/paintball.jpg");   
                                   URL webUrl = new URL("");
                                   Image image = Toolkit.getDefaultToolkit().getImage(webUrl);
                                   g.drawImage(image, 0, 0, this);
                                   } catch (MalformedURLException ex) { }
                            g.fillRect(20, 20, 200, 40);
                            Font font = new Font("Garamond", Font.PLAIN, 30);
                            g.drawString("PAINTBALL ", 40, 50);
    Last edited by cronnin; 02-01-2017 at 06:16 AM.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: Can't get both Image and Button in JFrame window

    Uh, so you're new to Java and to programming. And you are already trying to write code with graphics and buttons and events. My unsolicited advice would be to focus on the basics of Java and programming concepts before you delve into the above. There is so much to learn before before doing any of that. And it will benefit you in the long run.

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 10
    Last Post: 09-04-2012, 08:12 AM
  2. Closing secondary window from button in primary window
    By KoncentriK in forum New To Java
    Replies: 4
    Last Post: 02-26-2012, 01:24 AM
  3. Make a button class that uses your button image.
    By eLancaster in forum New To Java
    Replies: 1
    Last Post: 04-26-2011, 11:32 AM
  4. Replies: 6
    Last Post: 11-22-2010, 11:55 AM
  5. Checking if a button was pressed in a Window.
    By Valkyrie in forum New To Java
    Replies: 2
    Last Post: 12-15-2009, 05:28 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts