java - 不知道我的代码有什么问题。 Java图形用户界面

标签 java swing jframe jlabel jcomponent

我正在尝试制作一个弹出图像的组合框。我收到此错误:

Note: C:\Users\Kyle\Desktop\TUSEG\Program\ProductDemo.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.

无论如何,当它尝试提取图片时,我每次都会得到:

Couldn't find file: C:\Users\Kyle\Desktop\TUSEG\Program\images\microsoft\Xbox 360 Controller (PC).jpg
Couldn't find file: C:\Users\Kyle\Desktop\TUSEG\Program\images\microsoft\Wireless Laser Mouse 5000.jpg

路径绝对是正确的。我不确定我的问题是什么。如果有人可以看一下并帮助我吗?

    package components;

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;

    public class ProductDemo extends JPanel
                      implements ActionListener {
JLabel picture;

public ProductDemo() {
    super(new BorderLayout());

    String pMS[] = new String[23];
    pMS[0] = ("LifeChat LX-3000");
    pMS[1] = ("LifeChat ZX-6000");
    pMS[2] = ("Wireless Notebook Presenter 8000");
    pMS[3] = ("Arc Mouse");
    pMS[4] = ("Bluetooth Notebook Mouse 5000");
    pMS[5] = ("Explorer Mouse");
    pMS[6] = ("Explorer Mini Mouse");
    pMS[7] = ("Sidewinder X8 Mouse");
    pMS[8] = ("Wireless Laser Mouse 5000");
    pMS[9] = ("Wireless Mobile Mouse 3000");
    pMS[10] = ("Wireless Mobile Mouse 6000");
    pMS[11] = ("Arc Keyboard");
    pMS[12] = ("Bluetooth Mobile Keyboard 6000");
    pMS[13] = ("Sidewinder X4 Keyboard");
    pMS[14] = ("Sidewinder X6 Keyboard");
    pMS[15] = ("Ergonomic Desktop 7000");
    pMS[16] = ("Wireless Desktop 3000");
    pMS[17] = ("Wireless Laser Desktop 6000 v2.0");
    pMS[18] = ("Wireless Media Desktop 1000");
    pMS[19] = ("Windows Server 2008 Enterprise");
    pMS[20] = ("Notebook Cooling Base");
    pMS[21] = ("Xbox 360 Controller (PC)");
    pMS[22] = ("Xbox 360 Controller");
    Arrays.sort(pMS);

    //Indices start at 0, so 4 specifies the last index of the product.
    JComboBox msList = new JComboBox(pMS);
    msList.setSelectedIndex(22);
    msList.addActionListener(this);

    //Set up the picture.
    picture = new JLabel();
    picture.setFont(picture.getFont().deriveFont(Font.ITALIC));
    picture.setHorizontalAlignment(JLabel.CENTER);
    updateLabel(pMS[msList.getSelectedIndex()]);
    picture.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));

    //height + width
    picture.setPreferredSize(new Dimension(100, 100));

    //Lays out the demo.
    add(msList, BorderLayout.PAGE_START);
    add(picture, BorderLayout.PAGE_END);
    setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}

/** Listens to the combo box. */
public void actionPerformed(ActionEvent e) {
    JComboBox cb = (JComboBox)e.getSource();
    String pMS = (String)cb.getSelectedItem();
    updateLabel(pMS);
}

protected void updateLabel(String name) {
    ImageIcon icon = createImageIcon("C:\\Users\\Kyle\\Desktop\\TUSEG\\Program\\images\\microsoft\\" + name + ".jpg");
    picture.setIcon(icon);
    if (icon != null) {
        picture.setText(null);
    }
    else {
        picture.setText("Image not found");
    }
}

/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = ProductDemo.class.getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL);
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }
}

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("ProductDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    JComponent newContentPane = new ProductDemo();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}
}

最佳答案

您似乎混淆了基于 File 的路径,例如..

C:\Users\Kyle\Desktop\TUSEG\Program\images\microsoft\Xbox 360 Controller (PC).jpg

..在getResource(String)中使用相对引用,例如:

"images/microsoft/Xbox 360 Controller (PC).jpg"

getResource() 方法需要一个使用正斜杠的字符串,该字符串相对于应用程序的运行时类路径(因此 images 目录等)。通常会添加到 Jar 中)。为了确保它可以在任何包的类中工作,请在字符串前加上 / 前缀。

"/images/microsoft/Xbox 360 Controller (PC).jpg"

getResource() 方法将返回 URL,因此请务必使用 URL 兼容的构造函数。

关于java - 不知道我的代码有什么问题。 Java图形用户界面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8653015/

相关文章:

java - 鼠标滚轮监听器在 jscrollbar 中不起作用

java - 在有限的空间内显示图像的特定部分?

java - ContextConfiguration 抛出 java.io.FileNotFoundException :

java - 在 java shebang 脚本中加载库

java - 如何根据属性的特定值对 JTable 的行进行着色

Java GUI 框架。选择什么? Swing、SWT、AWT、SwingX、JGoodies、JavaFX、Apache Pivot?

java - 如何改变 JasperViewer 的外观和感觉?

java - 在Java中设置对话框的顶部对话框

java - 在第二个 Activity 中显示按钮CheckAnswer

java - 2D 游戏中的绘图问题