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

    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.

    ButtonAndImage.txt

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

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

    Look at https://docs.oracle.com/javase/8/doc...ax.swing.Icon-
    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
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    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
    Posts
    38
    Rep Power
    0

    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 java.net.MalformedURLException;
    import java.net.URL;
    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);
                    frame.setLocationRelativeTo(null);
                    frame.setResizable(false);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
                    canvas = new MyCanvas();
                    canvas.setBounds(100, 100, 200, 200);
                    canvas.setBackground(Color.ORANGE);
    
    // important, try to change it with frame.add(canvas); to see what's happening
                    frame.setContentPane(canvas);  
    
    // if you want to set your own bounds, you should bypass the default layout.  it doesn't do if you put it somewhere up
                    frame.setLayout(null);  
    
                    JButton b1 = new JButton("Click here");
                    b1.setBounds(450,390,180,120);
                    frame.add(b1);
    
                    frame.setVisible(true);	
            }
    
            public static void main(String[] args) {
                    ButtonAndImage buttonAndImage = new ButtonAndImage();	
            }
    
    
            private class MyCanvas extends JPanel {   // extend JPanel
                    @Override
                     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("https://s-media-cache-ak0.pinimg.com/originals/21/3e/39/213e3914224c3386334835b0a52a5412.jpg");
                                   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.setFont(font);
                            g.setColor(Color.WHITE);
                            g.drawString("PAINTBALL ", 40, 50);
                    }
            }	
    }
    Last edited by cronnin; 02-01-2017 at 07:16 AM.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    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.

    Regards,
    Jim
    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, 09: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, 02: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, 12:32 PM
  4. Replies: 6
    Last Post: 11-22-2010, 12:55 PM
  5. Checking if a button was pressed in a Window.
    By Valkyrie in forum New To Java
    Replies: 2
    Last Post: 12-15-2009, 06: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
  •