如何在面板中添加图像?

时间:2012-03-16 11:15:14

标签: java image swing applet

我想在面板中添加一个图像和描述,但只要我在组合框中选择一年,描述就会出现在列表中,问题是图像没有显示在面板的下半部分。我想我的代码有些不对劲。有人可以帮我解决这个问题吗?

这是我到目前为止所尝试的:

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");
                    }
              }
            });
}

3 个答案:

答案 0 :(得分:3)

我用它来调整我的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;
    }

答案 1 :(得分:2)

首先,您在init()方法的开头有一个错误,这与您的实际问题无关。你有一个包含22个字符串的数组,并且你试图将一个值分配给第23个索引,这是错误的,除非你放弃它,否则你会收到错误。

对于您的实际问题,更改图像的值不会更改/更新标签。请尝试使用actionPerformed()方法中的代码段,但是您需要将标签设为最终变量或全局变量。

if (select == 0)
{
    try
    {
        label.setIcon(new ImageIcon(ImageIO.read(new File("chicken.gif"))));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

答案 2 :(得分:2)

您的代码中有很多错误。

  1. String Array描述的大小为22,您要为索引22添加一个值,这将导致ArrayIndexOutOfBoundsException
  2. 由您创建的ImageIcon没有任何内容,因此当您将其添加到JLabel时,它将不会显示任何内容,如预期的那样。
  3. BorderLayout对象有五个区域。这些区域由BorderLayout常量指定:即PAGE_STARTPAGE_ENDLINE_STARTLINE_ENDCENTER。但是你使用的是NORTH,EAST,WEST,SOUTH方法,这是旧的。
  4. 在这里,我稍微修改了你的代码,看看,你的图像现在是否来了。

    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);
                        }
                  }
                });
        }
    }
    

    使用带有ImageIO的{​​{1}}访问图片的更好方法,因为图片是URL,因此通过Application Resource而不是URL访问图片更为明智我的这篇文章中显示了FileAccess Images via ImageIO