我有一個JLabel
與ImageIcon
,現在我想讓它們看起來很像桌面圖標,即頂部的圖標和底部的文本。這是我的代碼,但我將它們排列在一起如何讓我的jlabel圖標看起來像桌面圖標?
JLabel l=new JLabel("My Documents");
l.setIcon(new ImageIcon("mydocuments.png"));
如何做到這一點?
我有一個JLabel
與ImageIcon
,現在我想讓它們看起來很像桌面圖標,即頂部的圖標和底部的文本。這是我的代碼,但我將它們排列在一起如何讓我的jlabel圖標看起來像桌面圖標?
JLabel l=new JLabel("My Documents");
l.setIcon(new ImageIcon("mydocuments.png"));
如何做到這一點?
這是你如何能做到這,
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){}
}
}
});
嘗試使用構造public JLabel(String text, Icon icon, int horizontalAlignment)
其中horizontalAlignment
可這些常量的SwingConstants
任何一個:LEFT
,CENTER
,RIGHT
,LEADING
或TRAILING
。
你可以在這裏查看完整的Javadoc:http://docs.oracle.com/javase/7/docs/api/javax/swing/JLabel.html#JLabel(java.lang.String,javax.swing.Icon,INT)
這只是水平對齊!它不會將文字帶到底部!反正它似乎沒有改變文本的位置 –
你能寫出完整的代碼?我甚至想要它的功能。 –
什麼功能? – user12458
當我雙擊圖標,我應該可以打開我的文檔 –