2013-07-16 84 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

這只是水平對齊!它不會將文字帶到底部!反正它似乎沒有改變文本的位置 –

相關問題