如何让我的带有图标的jlabel看起来像桌面图标?

时间:2013-07-16 21:38:01

标签: java swing jlabel

我有JLabel ImageIcon,现在我想让它们看起来像桌面图标,即顶部的图标和底部的文字。这是我的代码,但我并排获取它们

JLabel l=new JLabel("My Documents");
l.setIcon(new ImageIcon("mydocuments.png"));

怎么做?

2 个答案:

答案 0 :(得分:2)

这是你可以做到的,

JLabel l=new JLabel("My Documents");
l.setIcon(new ImageIcon("mydocuments.png"));

l.setHorizontalTextPosition(SwingConstants.CENTER);
l.setVerticalTextPosition(SwingConstants.BOTTOM);
l.addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent me)
            {
                if(me.getClickCount()==2)
                {
                    try
                    {
                        Desktop.getDesktop().open(new File(System.getProperty("user.home")+"\\My Documents"));
                    }catch(Exception e){}
                }
            }
        });

答案 1 :(得分:2)

尝试使用构造函数public JLabel(String text, Icon icon, int horizontalAlignment),其中horizontalAlignment可以是SwingConstants中的其中一个常量:LEFTCENTERRIGHTLEADINGTRAILING

您可以在此处查看完整的Javadoc:http://docs.oracle.com/javase/7/docs/api/javax/swing/JLabel.html#JLabel(java.lang.String,javax.swing.Icon,int)