2

I'm using Swing and I'm trying to add some picture in program.

field = new JFormattedTextField(formatter);

ImageIcon icon = new ImageIcon("background.png"),
          icon1 = new ImageIcon("1.png");

JLabel background = new JLabel(icon); 
JLabel firstIcon = new JLabel(icon1);

JPanel center = new JPanel(new GridLayout(0, 1));


    public void initComponents() {
          this.getContentPane().add(center, BorderLayout.CENTER);

center.add(background);

field.setBounds(50,50);
background.add(field);
background.add(fristIcon);
}

With this code is everything working, but when I try to add the same picture "background.add(fristIcon);" again I don't see image added first. Every new image is deleting last icon.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user3648179
  • 35
  • 1
  • 5

3 Answers3

2

background is a JLabel, and you usually don't add one JLabel to another one. But if you do have to do this, be sure to give the JLabel that is acting as a container a decent layout manager so that it can display add components in a smart way. JLabels by default have no layout (null layout), and any component added would need to specify its size and location to be shown. And while you could do this -- specify the bounds of all components added, I'm going suggest that you not do this as this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain. Instead you will want to study and learn the layout managers and then nest JPanels or other components, each using its own layout manager to create pleasing and complex GUI's that look good on all OS's.

Consider just using a basic FlowLayout to see what I mean:

background.setLayout(new FlowLayout());

Note you

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • If I set background with FlowLayout it will works as my new panel? The coordinate (0,0) will start at the beginning of picture? – user3648179 May 17 '14 at 18:28
0

I want to add icon over icon.

Here is the code that is using Graphics.drawImage() to draw a image in existing Graphics of the JLabel within overridden paintComponent() method.

For more info read inline comments.

Sample code:

// back ground image
URL url1 = new URL(
        "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQu3FFM1kR-aeYFjJIqebgusVZNM6uG-a0F4Z_0IPopEqfSZwzBBA");
final BufferedImage bg = ImageIO.read(url1);

// foreground image
URL url2 = new URL("https://cdn1.iconfinder.com/data/icons/supermariopack/Mario.png");
final BufferedImage fg = ImageIO.read(url2);

// re size the image
final BufferedImage scaled = new BufferedImage(fg.getWidth() / 2, fg.getHeight() / 2,
        BufferedImage.TYPE_INT_RGB);
Graphics g = scaled.getGraphics();
g.drawImage(fg, 0, 0, scaled.getWidth(), scaled.getHeight(), null);
g.dispose();

// create a JLabel with back ground image
final JLabel label = new JLabel(new ImageIcon(bg)) {
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        // draw the foreground image
        g.drawImage(scaled, 50, 50, null);
    }
};

JOptionPane.showMessageDialog(null, label);

The above code is modified code of this post How to draw an image over another image?.

Screenshot:

enter image description here

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
-1

You can't add several times the same component.

I suggest you proceed like this:

for(int i=0;i<N;i++){
   JLabel img=new JLabel(icon1);
   switch(i){
   case 0:
      img.setBounds(x,y,w,h);
      break;
   case 1:
      img.setBounds(x,y,w,h);
      break;
   default:
      break;
   }
   background.add();
}

N equals the number of icon you want to display

But JLabel is not recommended as container. For that I suggest you use a JPanel as a container for your labels

TheNawaKer
  • 194
  • 1
  • 1
  • 10
  • I used JPanel but there I added JLabel background and then I have problem, when I add other thing on the same JPanel it moves left or right. – user3648179 May 17 '14 at 19:22
  • this topic should answer your question: [Centering a JLabel on a JPanel](http://stackoverflow.com/questions/7180198/centering-a-jlabel-on-a-jpanel) – TheNawaKer May 17 '14 at 19:45
  • But if I use "for" where I can set the place where image should be placed? – user3648179 May 17 '14 at 19:45
  • you can use the variable i to manage the different elements (look at my answer ;) ) – TheNawaKer May 17 '14 at 19:52
  • 1
    `"But JLabel is not a container"` -- Are you 100% sure of this statement? If you check the JLabel API you'll see that it inherits from Container, meaning that it will function as a container just fine. Have you tried to use it as a container in a GUI? I have, many times, and as expected, if done right, it works just fine. By done right, that means as with most containers a proper layout should be given. Sorry, but for the benefit of future visitors to this site, I have to give your answer a -1 vote until you correct the offending statement. Please notify me once your correction has been done. – Hovercraft Full Of Eels May 17 '14 at 21:10
  • @HovercraftFullOfEels Indeed, I misspoke. What I meant to say is that it's not an object which we have to use as container knowing that there is a JPanel which was created for it. Thank you for having rectified me. – TheNawaKer May 18 '14 at 14:42