setlocation of a JButton on a drawed JFrame

Discussion in 'Java' started by middaycrisis, Nov 11, 2007.

  1. middaycrisis

    middaycrisis

    Joined:
    Nov 11, 2007
    Messages:
    1
    Hi guys. I've experienced a little problem, when trying to change the location of a JButton, on a g.draw'ed JFrame. setLocation doesn't seem to work. It would be great if you could help me to move this button to where I want ;)

    Here's the code:

    Code:
    package gui;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
     
    public class MainFrame extends JFrame {
     JPanel panel, panel2;
     JButton btn, btn2;
     JScrollPane scrollPane;
     ImageIcon icon, icon2;
     Image image;
    
    public MainFrame(){
     
    icon = new ImageIcon("src/gui/bg.jpg");
    icon2 = new ImageIcon("src/gui/main_03.jpg");
     JPanel panel = new JPanel(){
     
      
      protected void paintComponent(Graphics g)
      {
      
       g.drawImage(icon.getImage(), 0, 0, null);
       super.paintComponent(g);
      }
     };
     
     panel.setOpaque( false );
     panel.setSize(500, 500) ;
     scrollPane = new JScrollPane( panel );
     getContentPane().add( scrollPane );
     
     
     
         ImageIcon img = new ImageIcon("src/gui/main_03.jpg");
         
         JButton btn = new JButton(img);
         btn.setLocation(230, 20);
         btn.setMargin(new Insets(1, 1, 1, 1));
         btn.setPreferredSize(new Dimension(256,85));
       
         panel.add(btn);
            
       
     
    }
    public static void main(String [] args)
    {
     MainFrame frame = new MainFrame();
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setSize(500, 500);
     frame.setVisible(true);
     frame.setTitle("test");
     
    }
    
    }
     
    
    Thanks for attention.
     
    middaycrisis, Nov 11, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jack

    setLocation does not work

    Jack, Nov 4, 2005, in forum: Java
    Replies:
    9
    Views:
    9,782
    Andrew Thompson
    Nov 5, 2005
  2. Jack

    setLocation (AWT)

    Jack, Nov 5, 2005, in forum: Java
    Replies:
    2
    Views:
    5,147
  3. arunsivaprakash@gmail.com

    Java Swings: Adding JButton to JFrame

    arunsivaprakash@gmail.com, Apr 12, 2006, in forum: Java
    Replies:
    2
    Views:
    22,690
    Bart Cremers
    Apr 12, 2006
  4. jakester
    Replies:
    3
    Views:
    1,880
    jakester
    Apr 3, 2007
  5. clusardi2k@aol.com
    Replies:
    2
    Views:
    433
    markspace
    Aug 27, 2012
Loading...

Share This Page