Изображение не отображается в Jlabel - PullRequest
1 голос
/ 16 мая 2019

Я хочу показать изображение в метке справа после выбора города в поле со списком, но изображение не отображается, хотя в синтаксисе кода нет ошибок и не отображается ошибка в пути к папке с изображениями после выполнения.

Любая помощь будет принята с благодарностью.

import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;

import java.awt.event.ActionListener;
import java.util.Hashtable;
import java.awt.event.ActionEvent;

import javax.swing.JLabel;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class App  {

private JFrame frame;
JLabel picture;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                App window = new App();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
private Hashtable subItems = new Hashtable();

public App() {
    initialize();

}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.getContentPane().setBackground(Color.DARK_GRAY);
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    String[] items = { "Select City", "Fes", "Sefrou", "Moulay Yaacoub" };
    JComboBox comboBox = new JComboBox(items);
    comboBox.setForeground(Color.BLACK);
    comboBox.setBackground(Color.WHITE);


    comboBox.setBounds(30, 32, 157, 20);
    frame.getContentPane().add(comboBox);

    String[] subItems1 = { "Select Fes", "1", "2", "3" };
    subItems.put(items[1], subItems1);

    String[] subItems2 = { "Select Sefrou", "1", "2", "3" };
    subItems.put(items[2], subItems2);

    String[] subItems3 = { "Select Moulay Yaacoub", "1", "2", "3" };
    subItems.put(items[3], subItems3);
    comboBox.setSelectedIndex(0);
    JComboBox comboBox_1 = new JComboBox(subItems1);

    comboBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String item = (String)comboBox.getSelectedItem();
            Object o = subItems.get( item );

            if (o == null)
            {
                comboBox_1.setModel( new DefaultComboBoxModel() );
            }
            else
            {
                comboBox_1.setModel( new DefaultComboBoxModel( (String[])o ) );
            }
        }
    });
    comboBox_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JComboBox cb = (JComboBox)e.getSource();
            String petName = (String)cb.getSelectedItem();
            updateLabel(petName);
        }
    });
    comboBox_1.setBounds(30, 63, 157, 20);
    frame.getContentPane().add(comboBox_1);

    JLabel Picture = new JLabel();
    updateLabel(subItems1[comboBox_1.getSelectedIndex()]);
    Picture.setBounds(236, 32, 188, 130);
    frame.getContentPane().add(Picture);

}

private void updateLabel(String name) {
    ImageIcon icon = createImageIcon("images/" + name + ".gif");
    picture.setIcon(icon);
    }


protected static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = test.class.getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL);
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }
}


}

код отредактирован после выполнения я получаю «java.lang.NullPointerException» из-за этой строки «picture.setIcon (icon);» есть идеи как исправить ошибку?

...