Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使我的带有图标的jlabel看起来像桌面图标?_Java_Swing_Jlabel - Fatal编程技术网

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

Java 如何使我的带有图标的jlabel看起来像桌面图标?,java,swing,jlabel,Java,Swing,Jlabel,我有一个JLabel和一个ImageIcon,现在我想让它们看起来非常像一个桌面图标,即顶部的图标和底部的文本。这是我的代码,但我会把它们放在一起 JLabel l=new JLabel("My Documents"); l.setIcon(new ImageIcon("mydocuments.png")); 如何做到这一点?这就是你可以做到的 JLabel l=new JLabel("My Documents"); l.setIcon(new ImageIcon("mydocuments.p

我有一个
JLabel
和一个
ImageIcon
,现在我想让它们看起来非常像一个桌面图标,即顶部的图标和底部的文本。这是我的代码,但我会把它们放在一起

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

这就是你能做到的

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(字符串文本、图标图标、int horizontalAlignment)
其中
horizontalAlignment
可以是
SwingConstants
中的以下常量之一:
前导
尾随


您可以在此处查看完整的Javadoc:,javax.swing.Icon,int)

尝试使用构造函数
public JLabel(字符串文本,图标图标,int horizontalAlignment)
其中
horizontalAlignment
可以是
SwingConstants
中的以下常量之一:
中心
前导
尾随


您可以在这里查看完整的Javadoc:,javax.swing.Icon,int)

您可以编写完整的代码吗?我甚至想要它的功能。当我双击图标时,我应该能够打开我的文档。你能写完整的代码吗?我甚至想要它的功能。当我双击图标时,我应该能够打开我的文档,这只是水平对齐!它不会将文本置于底部!!无论如何,它似乎没有改变文本位置,这只是水平对齐!它不会将文本置于底部!!无论如何,它似乎没有改变文本位置