【发布时间】:2010-07-11 16:19:06
【问题描述】:
下面是在 JLabel Swing 组件中放置图标的 java Swing 代码。
package com.TheMcLeodgrp;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main extends JFrame {
public Main() throws HeadlessException {
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.LEFT));
Icon icon = new ImageIcon("myIcon.gif");
JLabel label1 = new JLabel("Full Name :", icon, JLabel.LEFT);
JLabel label2 = new JLabel("Address :", JLabel.LEFT);
label2.setIcon(new ImageIcon("myIcon.gif"));
getContentPane().add(label1);
getContentPane().add(label2);
}
public static void main(String[] args) {
new Main().setVisible(true);
}
}
这是一个简单的标签程序,其他一切正常。当我运行程序时,图标 (myIcon.gif) 不会出现在标签中。我从标准的 Eclipse IDE 运行它,并且 myIcon.gif 驻留在一个文件夹中 - 即; images/myIcon.gif. 似乎我在这里遗漏了一些简单的东西,但我不知道。我是否需要将图标放在应用程序的其他位置。
【问题讨论】: