开发者

how to add labels without them overlapping each other

I wanted to add 2 labels in my JDialog; one label will have animated gif ; other will have text. How to add these two so that they dont overlap? I don't want to hardcode their positions. I want the program to make the inherent adjustments.

Thanks in Advance

code:

     JLabel l2=new JLabel("");
     try {

          Image img = ImageIO.read(getClass().getResource("resources/wait_animated.gif"));
          ImageIcon imgnew=new ImageIcon("G:\\my java\\DesktopApplication1\\src\\desktopapplication1\\resources\\wait_animated.gif");
          l2.setIcon(imgnew);
          imgnew.setImageObserver(l2);
     }
     catch (IOException ex) {
     }
     l2.setLocation(300,300);
     JDialog d=new JDialog();
     JLabel l=new JLabel("Please Wait While Processing is Done...        ");

     JDesktopPane dp=new JDesktopPane();
 开发者_StackOverflow社区    dp.setPreferredSize(new Dimension(300,50));
     l.setPreferredSize(new Dimension(250,50));

     l2.setPreferredSize(new Dimension(20,20));

     d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
     d.setTitle("Wait dialog");
     d.add(l);
     d.add(l2);


Use a LayoutManager (such as FlowLayout) to arrange your labels. It’s hard to say without any more details.


You do realize that one label can have both text and an image, right? E.G.

import javax.swing.*;
import java.net.URL;

class AnimatedGifInLabelWithText {

    public static void main(String[] args) throws Exception {
        final URL url = new URL("http://pscode.org/media/starzoom-thumb.gif");
        Runnable r = new Runnable() {
            public void run() {
                ImageIcon ii = new ImageIcon(url);
                JLabel label = new JLabel("Zoom!", ii, SwingConstants.CENTER);
                JOptionPane.showMessageDialog(null, label);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜