2012-03-16 62 views
2

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

这是我到目前为止已经试过:

所有的
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"); 
        } 
       } 
      }); 
} 
+0

你是如何运行的代码?你在检查Java控制台吗?为什么要编写一个applet而不是一个框架?我大概可以猜出它为什么会失败,但这对你更具启发性。顺便说一句,用''DefaultComboBoxModel''(http://docs.oracle.com/javase/7/docs/api/javax/swing/DefaultComboBoxModel.html)替换'String []'并且循环到'addElement() '22次。 – 2012-03-16 11:31:05

+0

在你的'ActionListener'尝试使用'label.setIcon(new ImageIcon(“chicken.gif”))'我不确定创建一个新的图像是否足够。 (如果你这样做的话,标签应该在类中声明不在init()中) – 2012-03-16 11:39:48

回答

2

首先,你必须在init()方法,这是不符合实际的问题相关的开始就是个错误。你有22个字符串的数组,你试图给第23个索引分配一个值,这是错误的,除非你放弃它,否则你会得到一个错误。

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

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

是的,它现在出现了,但问题太大了。我该如何调整它?我需要制作油漆方法吗? – sack 2012-03-16 11:53:43

+0

不,您可以使用文件数组设置为图像图标,并将所有if-else子句包括到单个try-catch中。 – Juvanis 2012-03-16 11:56:44

+0

我的意思是图像太大 – sack 2012-03-16 11:59:52

2

在代码中有很多事情你做错了。

  1. String Array描述的大小为22,您要添加值的指数22,这将导致ArrayIndexOutOfBoundsException
  2. 您创建的ImageIcon没有任何内容,因此当您将其添加到JLabel时,它将不会显示任何内容,如预期的那样。
  3. BorderLayout对象有五个区域。这些区域由BorderLayout常量指定:即PAGE_START,PAGE_END,LINE_START,LINE_ENDCENTER。但是你正在使用NORTH,EAST,WEST,SOUTH的方法,这是旧的。

这里我修改了一下你的代码,看看,你的图片是否现在到来。

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的图像的URL,因为图像是Application Resource,所以它是更明智通过URL一个File,而不是访问它显示在这个职位我的:Access Images via ImageIO

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

+1,Wololololulu,这看起来很神奇:-) – 2012-03-16 12:49:40

+1

+1这是重新调整图像的正确答案。谢谢 – YumYumYum 2012-07-17 19:32:46