簡體   English   中英

不知道我的代碼有什么問題。 Java GUI

[英]Don't know what's wrong with my code. Java GUI

我正在嘗試制作一個帶有圖像的組合框。 我收到此錯誤:

注意:C:\\ Users \\ Kyle \\ Desktop \\ TUSEG \\ Program \\ ProductDemo.java使用未經檢查或不安全的操作。 注意:使用-Xlint:unchecked重新編譯以獲取詳細信息。

無論如何,當它嘗試繪制一張照片時,每次都會得到:

找不到文件: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();
        }
    });
}
}

您似乎在混淆基於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兼容的構造函數。

確定是要從文件系統還是從應用程序的類路徑加載圖像。

如果從文件系統,請使用文件IO加載圖標,或使用以文件名作為參數的構造函數:

ImageIcon icon = new ImageIcon("c:\\....jpg");

如果來自類路徑,則該路徑是從類路徑的根開始的/分隔路徑,並且圖像應存儲在與您的類相同的目錄/ jar中(或存儲在類路徑中的另一個目錄/ jar中):

ImageIcon icon = new ImageIcon(ProductDemo.class.getResource("/path/to/image.jpg"));

參見http://docs.oracle.com/javase/6/docs/api/javax/swing/ImageIcon.htmlhttp://docs.oracle.com/javase/6/docs/api/java/lang/Class的.html#的getResource%28java.lang.String 29%

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM