首页 > 解决方案 > 按钮导航以在 jlabel 中显示图像

问题描述

我正在尝试创建一个按钮导航以在 jlabel 中显示图像。但是当我运行程序时,我得到一个空指针异常。我尝试了很多,但我无法解决这个问题。这是幻灯片库的代码

public class gallery extends javax.swing.JFrame {
    public gallery() {
        initComponents();
         showimage(position);
    }
    int position=0;
public String[] takeimage()
{
    File f=new File(getClass().getResource("/gallery").getFile());
    String[] Imageslist=f.list();
    return Imageslist;
}
public void showimage(int index)
{
    String[] Imageslist=takeimage();
    String img=Imageslist[index];
    ImageIcon icon=new ImageIcon(getClass().getResource("/gallery/"+img));
    Image image=icon.getImage().getScaledInstance(mainscreen.getWidth(), mainscreen.getHeight(), Image.SCALE_SMOOTH);
    mainscreen.setIcon(new ImageIcon(image));
}
private void nextMousePressed(java.awt.event.MouseEvent evt) {                                  
      
        new Thread();
        try {
            Thread.sleep(300);
        } catch (InterruptedException ex) {
            Logger.getLogger(gallery.class.getName()).log(Level.SEVERE, null, ex);
        }
        int p=this.mainscreen.getX();
        
        if(p>-1)
        {
          Animacion.Animacion.mover_izquierda(900, 200, 1, 2, mainscreen);
        }
        position=position+1;
        if(position>=takeimage().length)
        {
            position=takeimage().length-1;
        }
        showimage(position);
    }                              
private void previousMousePressed(java.awt.event.MouseEvent evt) {                                      
        // TODO add your handling code here:
        new Thread();
        try {
            Thread.sleep(300);
        } catch (InterruptedException ex) {
            Logger.getLogger(gallery.class.getName()).log(Level.SEVERE, null, ex);
        }
        int p=this.mainscreen.getX();
        
        if(p>-1)
        {
          Animacion.Animacion.mover_izquierda(900, 200, 1, 2, mainscreen);
        }
          position=position-1;
        if(position<0)
        {
            position=0;
        }
        showimage(position);
    }      

这是我每次运行程序时都面临的错误

 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at college_management.gallery.showimage(gallery.java:79)
    at college_management.gallery.<init>(gallery.java:25)
    at college_management.gallery$3.run(gallery.java:158)
    
    

标签: javanetbeans

解决方案


推荐阅读