问题
I want to add an image in the panel together with the description but the description appear only in the list whenever I choose a year in my combobox, the problem is that the image didn't show in the lower part of the panel. I guess somethings wrong with my code. Could someone help me about this?
This is what I've tried so far :
public class Main extends JApplet
{
private String[] description;
private JList list = new JList();
private DefaultListModel defaultListModel = new DefaultListModel();
private JComboBox c = new JComboBox();
private JButton b = new JButton("Ok");
private ImageIcon image;
public void init()
{
try
{
description = new String[22];
description[0] = "1990";
description[1] = "1991";
description[2] = "1992";
description[3] = "1993";
description[4] = "1994";
description[5] = "1995";
description[6] = "1996";
description[7] = "1997";
description[8] = "1998";
description[9] = "1999";
description[10] = "2000";
description[11] = "2001";
description[12] = "2002";
description[13] = "2003";
description[14] = "2004";
description[15] = "2005";
description[16] = "2006";
description[17] = "2007";
description[18] = "2008";
description[19] = "2009";
description[20] = "2010";
description[21] = "2011";
description[22] = "2012";
}
catch (ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
}
c = new JComboBox(description);
list = new JList(defaultListModel);
list.setBorder(BorderFactory.createLineBorder(Color.black, 1));
b.setText("<html><b><u>Click</click></b></html>");
list.setFont(new Font("Garamond", Font.BOLD, 17));
list.setForeground(Color.BLUE);
JLabel label = new JLabel(image);
JPanel down = new JPanel();
down.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
down.add(label);
JPanel panel = new JPanel();
panel.add(c);
panel.add(b);
Container cp = getContentPane();
cp.add(list, BorderLayout.CENTER);
cp.add(panel, BorderLayout.NORTH);
cp.add(down, BorderLayout.SOUTH);
this.setVisible(true);
b.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent
event)
{
int select;
select = c.getSelectedIndex();
defaultListModel.clear();
if (select == 0)
{
defaultListModel.addElement("the year of 1990");
image = new ImageIcon("chicken.gif");
}
}
});
}
回答1:
First of all, you have a mistake at the beginning of init()
method, which is not related with your actual question. You have an array of 22 strings and you are trying to assign a value to 23rd index, that's wrong, you will get an error unless you abandon it.
For your actual question, changing the value of an image doesn't change/update the label. Try the code snippet below in actionPerformed()
method, however you need to make your label a final or a global variable.
if (select == 0)
{
try
{
label.setIcon(new ImageIcon(ImageIO.read(new File("chicken.gif"))));
} catch (IOException e) {
e.printStackTrace();
}
}
回答2:
There are a number of things you are doing wrong in your code.
- The size of the
String Array
description is 22, and you are adding a value to the index 22, this will lead toArrayIndexOutOfBoundsException
. - The ImageIcon created by you has nothing it's null, so when you add it to
JLabel
, it will be showing nothing, as expected. - A BorderLayout object has five areas. These areas are specified by the BorderLayout constants: Namely
PAGE_START
,PAGE_END
,LINE_START
,LINE_END
andCENTER
. But you are using NORTH, EAST, WEST, SOUTH approach, this is old.
Here I had Modified your code a bit, have a look, is your image coming or not now.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main extends JApplet
{
private String[] description;
private JList list = new JList();
private DefaultListModel defaultListModel = new DefaultListModel();
private JComboBox c = new JComboBox();
private JButton b = new JButton("Ok");
private ImageIcon image;
private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
public void init()
{
try
{
description = new String[22];
description[0] = "1990";
description[1] = "1991";
description[2] = "1992";
description[3] = "1993";
description[4] = "1994";
description[5] = "1995";
description[6] = "1996";
description[7] = "1997";
description[8] = "1998";
description[9] = "1999";
description[10] = "2000";
description[11] = "2001";
description[12] = "2002";
description[13] = "2003";
description[14] = "2004";
description[15] = "2005";
description[16] = "2006";
description[17] = "2007";
description[18] = "2008";
description[19] = "2009";
description[20] = "2010";
description[21] = "2011";
//description[22] = "2012";
}
catch (ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
}
c = new JComboBox(description);
list = new JList(defaultListModel);
list.setBorder(BorderFactory.createLineBorder(Color.black, 1));
b.setText("<html><b><u>Click</click></b></html>");
list.setFont(new Font("Garamond", Font.BOLD, 17));
list.setForeground(Color.BLUE);
final JLabel label = new JLabel(image);
JPanel down = new JPanel();
down.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
down.add(label);
JPanel panel = new JPanel();
panel.add(c);
panel.add(b);
Container cp = getContentPane();
cp.add(list, BorderLayout.CENTER);
cp.add(panel, BorderLayout.PAGE_START);
cp.add(down, BorderLayout.PAGE_END);
this.setVisible(true);
b.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent
event)
{
int select;
select = c.getSelectedIndex();
defaultListModel.clear();
if (select == 0)
{
defaultListModel.addElement("the year of 1990");
label.setIcon(infoIcon);
}
else
{
label.setIcon(null);
}
}
});
}
}
A better approach to access an Image using ImageIO
with URL
, since image is an Application Resource
, so it's much wiser to access it via a URL
instead of a File
is shown in this post of mine : Access Images via ImageIO
回答3:
I use this to resize my ImageIcon :
if (select == 0)
{
defaultListModel.addElement("the year of 1990");
image = new ImageIcon("chicken.gif")
label.setIcon(new ImageIcon(getScaledImage(image.getImage(), 32, 32))))
}
....
/**
* Resizes an image using a Graphics2D object backed by a BufferedImage.
* @param srcImg - source image to scale
* @param w - desired width
* @param h - desired height
* @return - the new resized image
*/
private Image getScaledImage(Image srcImg, int w, int h){
BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TRANSLUCENT);
Graphics2D g2 = resizedImg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(srcImg, 0, 0, w, h, null);
g2.dispose();
return resizedImg;
}
来源:https://stackoverflow.com/questions/9736261/how-to-add-image-in-the-panel