Results 1 to 3 of 3
  1. #1
    rajkobie is offline Member
    Join Date
    Nov 2010
    Posts
    54
    Rep Power
    0

    Default How can I place two JButtons in the middle of the JFrame?

    Can someone advice me on how to insert to JButtons with pictures in the middle of the screen?

    Also, how can i completely cover a JButton with an Image?

    I would like pseudocode advice or code examples. Thank you.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    To give a JButton an image, create a BufferedImage with your image in it (ImageIO.read(...) can do this for you), then create an ImageIcon from the BufferedImage, and then set the JButton's icon with your ImageIcon:

    Java Code:
    BufferedImage myImage = ImageIO.read("myImages/image01.jpg");
    ImageIcon icon = new ImageIcon(myImage);
    JButton myButton = new JButton(icon);
    To have two buttons in the middle of a GUI will require using layout managers. To learn how to use these, please look here: Laying Out Components in a Container

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    To completely cover the button with the image you can also add:

    Java Code:
    button.setBorderPainted( false );

Similar Threads

  1. Help with JButtons into JFrame
    By g6pd in forum New To Java
    Replies: 5
    Last Post: 03-10-2011, 04:49 AM
  2. JFrame: JButtons in a grid
    By jackal in forum New To Java
    Replies: 2
    Last Post: 06-09-2010, 07:56 PM
  3. Find the Middle of a JFrame??
    By bdizzle in forum AWT / Swing
    Replies: 9
    Last Post: 02-21-2010, 08:39 PM
  4. Replies: 0
    Last Post: 03-10-2009, 09:55 AM
  5. Replies: 5
    Last Post: 03-09-2009, 03:09 AM

Posting Permissions

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