我会切入追逐,我有一个NullPointerException,我无法想出一种方法来解决它。原因是一个ImageIcon数组,在我希望在我的代码中使用它时,它显然是空的。我无法真正掌握代码如何在此处获得的顺序,因此我无法修复异常。
下面是一个应该在超级语句之后填充数组的类。
package mySharkAttacks;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class mySharkAttacksPanel extends JPanel {
String[] regionStrings = {"WorldWide", "USA", "Australia", "Bahamas", "Cayman Islands"};
ImageIcon[] regionImages = new ImageIcon[regionStrings.length];
public mySharkAttacksPanel() {
super(new BorderLayout());
//Load the pet regionImages and create an array of indexes.
//regionImages = new ImageIcon[regionStrings.length];
Integer[] intArray = new Integer[regionStrings.length];
for (int i = 0; i < regionStrings.length; i++) {
intArray[i] = new Integer(i);
regionImages[i] = createImageIcon("regionImages/" + regionStrings[i] + ".gif");
if (regionImages[i] != null) {
regionImages[i].setDescription(regionStrings[i]);
}
}
//Create the combo box.
JComboBox regionList = new JComboBox(intArray);
//ComboBoxRenderer renderer= new ComboBoxRenderer();
//renderer.setPreferredSize(new Dimension(200, 130));
regionList.setRenderer(new ComboBoxRenderer(regionImages, regionStrings));
regionList.setMaximumRowCount(3);
//Lay out the demo.
add(regionList, BorderLayout.PAGE_START);
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}
/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = mySharkAttacksPanel.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.
*/
static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("mySharkAttacks");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new mySharkAttacksPanel();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
}
这是应该使用此ImageIcon数组的类,我将其解析为参数。
package mySharkAttacks;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComboBoxRenderer extends JLabel implements ListCellRenderer {
private Font normalFont;
ImageIcon[] localCurrentImage;
String[] localCurrentString;
public ComboBoxRenderer(ImageIcon[] currentImage, String[] currentString) {
setOpaque(true);
setHorizontalAlignment(CENTER);
setVerticalAlignment(CENTER);
for (int i = 0; i < currentImage.length; i++)
{
localCurrentImage[i] = currentImage[i];
}
for (int i = 0; i < currentString.length; i++)
{
localCurrentString[i] = currentString[i];
}
}
/*
* This method finds the image and text corresponding
* to the selected value and returns the label, set up
* to display the text and image.
*/
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
//Get the selected index. (The index param isn't
//always valid, so just use the value.)
int selectedIndex = ((Integer)value).intValue();
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
//Set the icon and text. If icon was null, say so.
ImageIcon icon = localCurrentImage[selectedIndex];
String pet = localCurrentString[selectedIndex];
setIcon(icon);
if (icon != null) {
setText(pet);
setFont(list.getFont());
} else {
setNormalFont(pet + " (no image available)",
list.getFont());
}
return this;
}
//Set the font and text when no image was found.
protected void setNormalFont(String uhOhText, Font normalFont) {
if (normalFont == null) { //lazily create this font
normalFont = normalFont.deriveFont(Font.ITALIC);
}
setFont(normalFont);
setText(uhOhText);
}
}
很抱歉这段代码,但我怀疑我只能用文字来形象化。