0

Hello,

Probably my question is very simple but it is my first java program ever and I don't know what to do.

I want to create a combobox that open images. Just that!

But I get 2 errors, 1. Run out of memory 2. unchecked call to JComboBox(E[]) in line ''images = new JComboBox( names );''

The first error I tried to fix it in eclipse with -Xmx1024m command and now the program runs through eclipse platform but when I try to compile with javac command I get the error 2. Finally when I create a .jar file from eclipse and I run, it doesn't load all images just the first 80 of 310.

Is there another way to write my code in order to load all images and don't have to use so much memory?

I would appreciate if someone of you could just correct my code...

Thank you very much, Wishmaster

My code is this:

package test;  

import java.awt.*;  
import java.awt.event.*;  
import java.awt.FlowLayout;  
import java.awt.event.ItemListener;  
import java.awt.event.ItemEvent;  
import javax.swing.JFrame;  
import javax.swing.JLabel;  
import javax.swing.JComboBox;  
import javax.swing.Icon;  
import javax.swing.ImageIcon;  

public class ComboBoxTest extends JFrame {  
private JComboBox images;  
private JLabel label;  
private String names[] =  
{  "1.P 1.png",  
   "2.P -1.png",   ....so on to 300 images };  

private Icon icons[] =  
new Icon[] {    new ImageIcon( names[ 0] ),  
        new ImageIcon( names[ 1 ] ), ....so on  };  
 };  

public ComboBoxTest() {  
super( "Testing JComboBox" );  

Container c = getContentPane();  
c.setLayout( new FlowLayout() );  

images = new JComboBox( names );  
images.setMaximumRowCount( 10 );  

images.addItemListener(  
new ItemListener() {  
public void itemStateChanged( ItemEvent e ) {  
label.setIcon(  
icons[ images.getSelectedIndex() ] );  
}  
}  
);  

c.add( images );  

label = new JLabel( icons[ 0 ] );  
c.add( label);  
setVisible(true);  
setSize( 500, 500 );  
}  


public static void main( String args[] )  
{  
ComboBoxTest app = new ComboBoxTest();  

app.addWindowListener(  
new WindowAdapter() {  
public void windowClosing( WindowEvent e )  
{  
System.exit( 0 );  
}  
}  
);  
}  
}  
4

1 回答 1

0

如果我理解正确。

  1. 不要在开始时创建 imageicon 。在项目侦听器中创建唯一的特定图像。

    images.addItemListener(  
    new ItemListener() {  
    public void itemStateChanged( ItemEvent e ) {  
    Icon ico=new ImageIcon(toString(images.getSelectedItem()));
    label.setIcon(ico);  
    }  
    }  
    );  
    

不需要这个:

private Icon icons[] =  
new Icon[] {    new ImageIcon( names[ 0] ),  
        new ImageIcon( names[ 1 ] ), ....so on  };  
 };  
于 2013-02-20T14:40:30.240 回答