0

我的代码:

public class UserDialog extends JDialog {

    public UserDialog() {
    add(createForm(), BorderLayout.CENTER);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    setLocation(400, 100);
    pack();
    setVisible(true);
}

    public JPanel createForm() {
    JPanel panel = new JPanel();
    ImageIcon image = new ImageIcon("Check.png");

    okBtn = new JButton("Ok");
    cancelBtn = new JButton("Cancel");
    tempBtn = new JLabel();
    fNameLbl = new JLabel("First Name");
    fNamePicLbl = new JLabel(image);     // add icon to jlable
    lNameLbl = new JLabel("Last Name");
    lNamePicLbl = new JLabel();
    genderLbl = new JLabel("Gender");

    maleRb = new JRadioButton("Male");
    femaleRb = new JRadioButton("Female");
    temp3 = new JLabel();
    group = new ButtonGroup();
    group.add(maleRb);
    group.add(femaleRb);

    fNameTf = new JTextField(10);
    lNameTf = new JTextField(10);

    panel.add(fNameLbl);
    panel.add(fNameTf);
    panel.add(fNamePicLbl);
    panel.add(lNameLbl);
    panel.add(lNameTf);
    panel.add(lNamePicLbl);
    panel.add(genderLbl);
    JPanel radioPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    radioPanel.add(maleRb);
    radioPanel.add(femaleRb);
    panel.add(radioPanel);
    panel.add(temp3);
    panel.add(okBtn);
    okBtn.addActionListener(this);
    panel.add(cancelBtn);
    cancelBtn.addActionListener(this);
    panel.add(tempBtn);

    panel.setLayout(new SpringLayout());
    SpringUtilities.makeCompactGrid(panel, 4, 3, 50, 10, 80, 60);
    return panel;
}

当我运行程序时,图标不显示。

4

1 回答 1

1

Check.png 的路径似乎有问题。更正路径并出现图标。如果图标存储在资源文件夹图标下,则路径应如下所示。

ImageIcon image = new ImageIcon("icons/Check.png");
于 2013-10-16T10:16:22.393 回答