Help for displaying an image on my application.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chanshaw
    New Member
    • Nov 2008
    • 67

    Help for displaying an image on my application.

    Alright so basically I have a logo named "DirSync.jp g" and I want to display it on the top portion of my application window in a container (either JLabel or JPanel which ever is normally recommended)

    Code:
    public class MainFrame extends JFrame 
    {  
       public MainFrame()
       {
           super();
           this.setDefaultCloseOperation(EXIT_ON_CLOSE);
           this.setSize(500,500);
           this.add(getMainPanel());
           this.setVisible(true);
       }
       
       private JPanel getMainPanel()
       {
           JPanel panel = new JPanel();
           panel.setBackground(getLuckyPointColor());
           panel.setLayout(new FlowLayout());
           panel.add(getTitlePanel());
           return panel;
       }
    }
    I want the getTitlePanel to return me a panel with my image as the background could someone add what I need to do here I've searched around and can't find anything of use... thanks.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    The normal steps are:

    1) get the URL of your file/resource (see the Class class);
    2) get the ImageIcon given the URL;
    3) feed the ImageIcon to a JLabel;
    4) put your JLabel in a JPanel somewhere.

    Alternatively you can change steps 1) and 2) by getting an ImageIcon given the
    name of a file directly.

    kind regards,

    Jos

    Comment

    Working...