2013-07-16 77 views
0

我有一个JLabelImageIcon,现在我想让它们看起来很像桌面图标,即顶部的图标和底部的文本。这是我的代码,但我将它们排列在一起如何让我的jlabel图标看起来像桌面图标?

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

如何做到这一点?

回答

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){} 
       } 
      } 
     }); 
+0

你能写出完整的代码?我甚至想要它的功能。 –

+0

什么功能? – user12458

+0

当我双击图标,我应该可以打开我的文档 –

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)

+0

这只是水平对齐!它不会将文字带到底部!反正它似乎没有改变文本的位置 –

相关问题