java - 按钮导航以在 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)
解决方案
推荐阅读
- transition - __layout 中的苗条过渡
- android-layout - android studio中的仪表板控制台布局
- firebase - 编写 Firebase Firestore 规则时出现问题
- sql - HANA:如何在已编入索引的列上添加唯一约束?
- c - C API 使用 tz 数据库获取 DST 的开始和结束日期
- kirby - Kirby CMS:如何将块返回为 HTML?
- python-3.x - 正则表达式提取 Excel 单元格引用
- reactjs - 在 useEffect 中,如何在运行后续逻辑之前先正确更新状态?
- angular - Angular 材质 CdkDragPreview 输入
- python - 如何在 MultiHeadAttention 中为 TpyTorch 的 3d attn_mask 参数准备数据