Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.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
Don';我不知道是什么';我的代码有问题。JavaGUI_Java_Swing_Jframe_Jlabel_Jcomponent - Fatal编程技术网

Don';我不知道是什么';我的代码有问题。JavaGUI

Don';我不知道是什么';我的代码有问题。JavaGUI,java,swing,jframe,jlabel,jcomponent,Java,Swing,Jframe,Jlabel,Jcomponent,我正在尝试制作一个弹出图像的组合框。我得到这个错误: 注意:C:\Users\Kyle\Desktop\TUSEG\Program\ProductDemo.java使用未经检查或不安全的操作。注意:使用-Xlint重新编译:未选中以获取详细信息 不管怎么说,当它试图拉起一张图片时,我每次都会看到: 找不到文件:C:\Users\Kyle\Desktop\TUSEG\Program\images\microsoft\Xbox 360 Controller(PC)。jpg 找不到文件:C:\User

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

注意:C:\Users\Kyle\Desktop\TUSEG\Program\ProductDemo.java使用未经检查或不安全的操作。注意:使用-Xlint重新编译:未选中以获取详细信息

不管怎么说,当它试图拉起一张图片时,我每次都会看到:

找不到文件:C:\Users\Kyle\Desktop\TUSEG\Program\images\microsoft\Xbox 360 Controller(PC)。jpg
找不到文件: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();
        }
    });
}
}

您似乎混淆了基于
文件的路径,如

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兼容的构造函数。

您似乎混淆了基于
文件的路径,如

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兼容的构造函数。

决定是从文件系统还是从应用程序的类路径加载映像

如果来自文件系统,请使用文件IO加载图标,或使用文件名作为参数的构造函数:

ImageIcon icon = new ImageIcon("c:\\....jpg");
如果从类路径开始,则该路径是从类路径根开始的/分隔路径,并且图像应存储在与类相同的目录/jar中(或者存储在类路径中的另一个目录/jar中):


请参阅并决定是从文件系统还是从应用程序的类路径加载映像

如果来自文件系统,请使用文件IO加载图标,或使用文件名作为参数的构造函数:

ImageIcon icon = new ImageIcon("c:\\....jpg");
如果从类路径开始,则该路径是从类路径根开始的/分隔路径,并且图像应存储在与类相同的目录/jar中(或者存储在类路径中的另一个目录/jar中):


请参阅和

使用未经检查或不安全的操作。注意:使用-Xlint:unchecked重新编译以获取详细信息。
在使用Java 6 SDK编译代码时,我没有收到该警告。1) 你用的是什么版本?2) 将
-Xlint:unchecked
添加到编译选项时,会获得什么额外的输出?@AndrewThompson我不知道如何运行它(-Xlint:unchecked),而且我也在使用Java 6 SDK。。。我有点不懂编码。@KyleLawson:JAR文件创建:假设您的项目的目录结构类似于(C:\project),其中包含子目录作为类和src以及manifest.txt文件(主类:components.ProductDemo)。因为您正在创建一个包,所以在命令提示符下,转到classes文件夹,即C:\Project\classes,现在输入jar-cfm anyname,您想输入file.jar..\manifest.txt组件。这将创建您的jar文件。关于
使用未经检查或不安全的操作。注意:使用-Xlint:unchecked重新编译以获取详细信息。
在使用Java 6 SDK编译代码时,我没有收到该警告。1) 你用的是什么版本?2) 将
-Xlint:unchecked
添加到编译选项时,会获得什么额外的输出?@AndrewThompson我不知道如何运行它(-Xlint:unchecked),而且我也在使用Java 6 SDK。。。我有点不懂编码。@KyleLawson:JAR文件创建:假设您的项目的目录结构类似于(C:\project),其中包含子目录作为类和src以及manifest.txt文件(主类:components.ProductDemo)。因为您正在创建一个包,所以在命令提示符下,转到classes文件夹,即C:\Project\classes,现在输入jar-cfm anyname,您想输入file.jar..\manifest.txt组件。这将创建您的jar文件。RegardsWell,你能把我链接到如何把它做成一个.jar吗?我也想这么做,但我找不到任何相关信息。我还需要做些什么?我知道我需要我的主类清单,但除此之外我怎么做?!那么,你能把我和如何把它做成一个.jar联系起来吗?我也想这么做,但我找不到任何相关信息。我还需要做些什么?我知道我需要我的主类清单,但除此之外我怎么做?!