2013-10-17 94 views
0

我有疑问。我不知道我的程序不工作。添加图像当选择jlist项目

import br.com.operacao.Paga; 

public class Tela extends JPanel{ 
    JLabel image; 
    public Tela() { 

    this.setLayout(new GridBagLayout()); 


    GridBagConstraints c = new GridBagConstraints(); 
    c.fill = GridBagConstraints.BOTH; 
    c.insets = new Insets(5, 5, 5, 5); 


    String labels[] = { "Coca-Cola", "Fanta Laranja", "Fanta-Uva", 
    "Sprite"}; 
    final JList<String> list = new JList<String>(labels); 
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
    list.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); 
    list.setSelectedIndex(0); 


    JScrollPane pane = new JScrollPane(); 
    pane.getViewport().add(list); 
    JPanel firstpanel = new JPanel(); 
    firstpanel.add(pane); 
    firstpanel.setBackground(Color.BLACK); 
    c.gridx = 0; 
    c.gridy = 0; 
    c.gridwidth = 1; 
    c.gridheight = 1; 
    c.weightx = c.weighty = 0.0; 
    this.add(firstpanel,c); 

    image = new JLabel(); 
    image.setFont(image.getFont().deriveFont(Font.ITALIC)); 
    image.setHorizontalAlignment(JLabel.CENTER); 
    updateLabel(labels[list.getSelectedIndex()]); //there is a error here but why? 
    image.setBorder(BorderFactory.createEmptyBorder(10,0,0,0)); 

    c.gridx = 1; 
    c.gridy = 0; 
    c.gridwidth = 1; 
    c.gridheight = 1; 
    c.weightx = c.weighty = 0.0; 
    this.add(image, c); 

    JScrollPane js = new JScrollPane(); 
    js.setPreferredSize(new Dimension(110,110)); 
    c.gridx = 0; 
    c.gridy = 2; 
    c.gridwidth = 3; 
    c.gridheight = 1; 
    this.add(js, c); 

    final JButton comprar = new JButton("Comprar"); 
    comprar.setEnabled(false); 
    list.addListSelectionListener(new ListSelectionListener() { 

     public void valueChanged(ListSelectionEvent e) { 
      int selections[] = list.getSelectedIndices(); 
      //String selectedValue = list.getSelectedValue(); 
      Object selectionValues[] = list.getSelectedValues(); 
      for (int i = 0, n = selections.length; i < n; i++) { 
       if (i == 0) { 
      System.out.println("Value" + selectionValues[i]); 
       }} 
      comprar.setEnabled(true); 

     } 
    }); 
    c.gridx = 0; 
    c.gridy = 4; 
    c.gridwidth = 1; 
    c.gridheight = 1; 
    this.add(comprar, c); 
    comprar.addActionListener(new Paga()); 

    final JButton confirma = new JButton("Confirmar"); 
    confirma.setEnabled(false); 
    c.gridx = 1; 
    c.gridy = 4; 
    c.gridwidth = 1; 
    c.gridheight = 1; 
    this.add(confirma,c); 


    } 

    public void actionPerformed(ActionEvent e) { 

     JList<String> jl = (JList<String>)e.getSource(); 
     int refriName[] = jl.getSelectedIndices(); 
     updateLabel(refriName); //what is this error? 
    } 
    protected void updateLabel(String name) { 
     ImageIcon icon = createImageIcon("images/" + name + ".jpg"); 
     image.setIcon(icon); 
     image.setToolTipText("A drawing of a " + name.toLowerCase()); 
     if (icon != null) { 
      image.setText(null); 
     } else { 
      image.setText("Image not found"); 
     } 
    } 

    /** Returns an ImageIcon, or null if the path was invalid.*/ 
    protected static ImageIcon createImageIcon(String path) { 
     java.net.URL imgURL = Tela.class.getResource(path); 
     if (imgURL != null) { 
      return new ImageIcon(imgURL); 
     } else { 
      System.err.println("Couldn't find file: " + path); 
      return null; 
     } 
    } 

} 

我需要在我的GUI我JList中添加图像相对于选定的项目......但我不知道什么在我implemments问题... 在我的项目的Java有一个文件夹图像和以下图像。

This is my Program

+0

@JungJoo在类型的方法updateLabel(字符串)特拉不适用于参数(INT [])'公共无效的actionPerformed(ActionEvent的发送){ \t \t \t \t JList的 JL =(JList的) e.getSource(); \t int refriName [] = jl.getSelectedIndices(); \t updateLabel(refriName); \t}' –

+0

1)为了更好地提供帮助,请发布[SSCCE](http://sscce.org/)。 2)获取图像的一种方法是通过热链接到[本答案](http://stackoverflow.com/a/19209651/418556)中看到的图像。 –

回答

2

当你打电话......

updateLabel(labels[list.getSelectedIndex()]); 

从构造方法中,很可能没有什么选择还,这意味着list.getSelectedIndex()将返回-1不是一个有效的数组索引...

更好的解决方案可能是将选定的索引传递给updateLabel方法,并允许它执行一些完整性检查(>= 0 && < lables.length)例如

+0

我该如何实施它? –

+0

1-更新'updateLabel'的方法签名来取代'String'而不是'String' 2-让'labels'成为一个实例字段3-您的理智检查并根据需要更新标签 – MadProgrammer

+0

看我的代码: '公共无效的actionPerformed(ActionEvent的发送){ \t \t \t \t \t \t JList的 JL =(JList的)e.getSource(); \t String refriName = jl.getSelectedValue(); \t updateLabel(refriName); \t}'我认为问题在这里......你知道吗? –