Results 1 to 4 of 4
Thread: How to add Images to JPanels?
- 12-07-2007, 03:43 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 15
- Rep Power
- 0
How to add Images to JPanels?
Have tried a couple of methods but its not working...
Here is one that I have tried:
panel = new JPanel(new ImageIcon("path"));
I just need a simple code, similar to the one I tried........
Thnx much!!!
- 12-07-2007, 06:26 PM #2
If you do not want the label to expand to fill the center section you can tryJava Code:String path = "path_to_image"; JPanel panel = new JPanel(new BorderLayout()); JLabel label = new JLabel(new ImageIcon(path)); label.setHorizontalAlignment(JLabel.CENTER); panel.add(label); // default center section
Java Code:JPanel panel = new JPanel(new GridBagLayout()); panel.add(label, new GridBagConstraints());
- 12-08-2007, 11:52 AM #3
Member
- Join Date
- Dec 2007
- Posts
- 15
- Rep Power
- 0
Um...
What if the layout of my panel is set to null? I have components added to the panel. I need the panel to have an image....
If I used that...will the label cover the components? By the way, I have a label in the panel....Last edited by Soda; 12-08-2007 at 12:20 PM.
- 12-08-2007, 05:54 PM #4
In working with null layout you can add the label and call setBounds on it to position it. Use the image width and height for the bounds width and height. You can always set an etched border on it (temporarily, during development) to see its size.
The other option is to draw the image on the panel:
Java Code:class Pseudo extends JPanel { BufferedImage image; Pseudo(BufferedImage image) { this.image = image; // or load it in this class setLayout(null); add components... } protected void paintComponent(Graphics g) { super.paintComponent(g); int x = int y = g.drawImage(image, x, y, this); } }
Similar Threads
-
Comparing Images
By shaungoater in forum Advanced JavaReplies: 0Last Post: 03-17-2008, 10:38 AM -
Problems with JPanels and displaying
By Mastergeek666 in forum AWT / SwingReplies: 1Last Post: 01-19-2008, 12:32 AM -
Can't synchronize multiple JPanels in a JFrame
By vassil_zorev in forum AWT / SwingReplies: 0Last Post: 12-30-2007, 04:22 PM -
Help with images...
By toby in forum Java AppletsReplies: 1Last Post: 08-04-2007, 05:25 AM -
Images in JSP
By Daniel in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 06-05-2007, 06:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks