朋友们,刚开始学习java,遇到了一个例程,在eclipse里调试的,不知道该怎么改,帮帮忙。
程序如下:
package jcomponent;//LabelDemo.java: Use label to display images
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;public class LabelDemo extends JFrame implements ActionListener
{
// Declare an ImageIcon array. There are total 52 images
private ImageIcon[] imageIcon = new ImageIcon[52]; // The current image index
private int currentIndex = 0; // Buttons for browsing images
private JButton jbtPrior, jbtNext; // Label for displaying images
private JLabel jlblImageViewer = new JLabel(); final int TOTAL_NUMBER_OF_IMAGES = 52; // Main Method
public static void main(String[] args)
{
LabelDemo frame = new LabelDemo();
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
} // Default Constructor
public LabelDemo()
{
setTitle("Label Demo"); // Load images into imageIcon array
for (int i=1; i<=52; i++)
{
imageIcon[i-1] = new ImageIcon(
this.getClass().getResource("images\\L1.gif"/*"images" + "/L" + i + ".gif"*/));
} // Show the first image
jlblImageViewer.setIcon(imageIcon[currentIndex]); // Set center alignment
jlblImageViewer.setHorizontalAlignment(SwingConstants.CENTER);
//jlblImageViewer.setVerticalAlignment(SwingConstants.CENTER); // Panel jpButtons to hold two buttons for browsing images
JPanel jpButtons = new JPanel();
jpButtons.add(jbtPrior = new JButton());
jbtPrior.setIcon(new ImageIcon(
this.getClass().getResource("images/left.gif")));
jpButtons.add(jbtNext = new JButton());
jbtNext.setIcon(new ImageIcon(
this.getClass().getResource("images/right.gif"))); // Add jpButton and the label to the frame
getContentPane().add(jlblImageViewer, BorderLayout.CENTER);
getContentPane().add(jpButtons, BorderLayout.SOUTH); // Register listeners
jbtPrior.addActionListener(this);
jbtNext.addActionListener(this);
} // Handle ActionEvent from buttons
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == jbtPrior)
{
// Make sure index is nonnegative
if (currentIndex == 0) currentIndex = TOTAL_NUMBER_OF_IMAGES;
currentIndex = (currentIndex - 1)%TOTAL_NUMBER_OF_IMAGES;
jlblImageViewer.setIcon(imageIcon[currentIndex]);
}
else if (e.getSource() == jbtNext)
{
currentIndex = (currentIndex + 1)%TOTAL_NUMBER_OF_IMAGES;
jlblImageViewer.setIcon(imageIcon[currentIndex]);
}
}
}错误提示如下:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at jcomponent.LabelDemo.<init>(LabelDemo.java:43)
at jcomponent.LabelDemo.main(LabelDemo.java:28)
这个例外怎么处理,该怎么写?
程序如下:
package jcomponent;//LabelDemo.java: Use label to display images
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;public class LabelDemo extends JFrame implements ActionListener
{
// Declare an ImageIcon array. There are total 52 images
private ImageIcon[] imageIcon = new ImageIcon[52]; // The current image index
private int currentIndex = 0; // Buttons for browsing images
private JButton jbtPrior, jbtNext; // Label for displaying images
private JLabel jlblImageViewer = new JLabel(); final int TOTAL_NUMBER_OF_IMAGES = 52; // Main Method
public static void main(String[] args)
{
LabelDemo frame = new LabelDemo();
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
} // Default Constructor
public LabelDemo()
{
setTitle("Label Demo"); // Load images into imageIcon array
for (int i=1; i<=52; i++)
{
imageIcon[i-1] = new ImageIcon(
this.getClass().getResource("images\\L1.gif"/*"images" + "/L" + i + ".gif"*/));
} // Show the first image
jlblImageViewer.setIcon(imageIcon[currentIndex]); // Set center alignment
jlblImageViewer.setHorizontalAlignment(SwingConstants.CENTER);
//jlblImageViewer.setVerticalAlignment(SwingConstants.CENTER); // Panel jpButtons to hold two buttons for browsing images
JPanel jpButtons = new JPanel();
jpButtons.add(jbtPrior = new JButton());
jbtPrior.setIcon(new ImageIcon(
this.getClass().getResource("images/left.gif")));
jpButtons.add(jbtNext = new JButton());
jbtNext.setIcon(new ImageIcon(
this.getClass().getResource("images/right.gif"))); // Add jpButton and the label to the frame
getContentPane().add(jlblImageViewer, BorderLayout.CENTER);
getContentPane().add(jpButtons, BorderLayout.SOUTH); // Register listeners
jbtPrior.addActionListener(this);
jbtNext.addActionListener(this);
} // Handle ActionEvent from buttons
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == jbtPrior)
{
// Make sure index is nonnegative
if (currentIndex == 0) currentIndex = TOTAL_NUMBER_OF_IMAGES;
currentIndex = (currentIndex - 1)%TOTAL_NUMBER_OF_IMAGES;
jlblImageViewer.setIcon(imageIcon[currentIndex]);
}
else if (e.getSource() == jbtNext)
{
currentIndex = (currentIndex + 1)%TOTAL_NUMBER_OF_IMAGES;
jlblImageViewer.setIcon(imageIcon[currentIndex]);
}
}
}错误提示如下:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at jcomponent.LabelDemo.<init>(LabelDemo.java:43)
at jcomponent.LabelDemo.main(LabelDemo.java:28)
这个例外怎么处理,该怎么写?
解决方案 »
- 这类代码是干什么的
- Swing事件派发线程的疑惑
- 怪异问题,请大家看看!
- getActionCommand() 和getSource()的区别^
- java调用存储过程返回值始终为0?查询分析器中运行没错,在java中调用就出错了,到底哪出错了?
- 超菜鸟的一个算术题,帮忙看看.谢谢
- java中能否获取自己所在的进程ID(进程表中的)以及如何判断自己的运行方式
- java.util.date和java.sql.date的区别与转换?
- jb7里没有没象delphi或vb那样的datagrid控件?
- 如何编写自己的Java Component
- 一个关于IF和while得问题
- 当resin服务器启动时,每10钟执行java程序
可能是找不到这个文件images\\L1.gif
"文件路径"); 试试看,我没有试,不知道对错~
this.getClass().getResource("images/right.gif");都直接改成“文件路径”,去掉this.getClass().getResource("")
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*; public class LabelDemo extends JFrame implements ActionListener
{
// Declare an ImageIcon array. There are total 52 images
private ImageIcon[] imageIcon = new ImageIcon[52]; // The current image index
private int currentIndex = 0; // Buttons for browsing images
private JButton jbtPrior, jbtNext; // Label for displaying images
private JLabel jlblImageViewer = new JLabel(); final int TOTAL_NUMBER_OF_IMAGES = 52; // Main Method
public static void main(String[] args)
{
LabelDemo frame = new LabelDemo();
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
} // Default Constructor
public LabelDemo()
{
setTitle("Label Demo"); // Load images into imageIcon array
for (int i=1; i <=52; i++)
{
imageIcon[i-1] = new ImageIcon(
"D:\\L1.gif");
} // Show the first image
jlblImageViewer.setIcon(imageIcon[currentIndex]); // Set center alignment
jlblImageViewer.setHorizontalAlignment(SwingConstants.CENTER);
//jlblImageViewer.setVerticalAlignment(SwingConstants.CENTER); // Panel jpButtons to hold two buttons for browsing images
JPanel jpButtons = new JPanel();
jpButtons.add(jbtPrior = new JButton());
jbtPrior.setIcon(new ImageIcon(
"D:\\L1.gif"));
jpButtons.add(jbtNext = new JButton());
jbtNext.setIcon(new ImageIcon(
"D:\\L1.gif")); // Add jpButton and the label to the frame
getContentPane().add(jlblImageViewer, BorderLayout.CENTER);
getContentPane().add(jpButtons, BorderLayout.SOUTH); // Register listeners
jbtPrior.addActionListener(this);
jbtNext.addActionListener(this);
} // Handle ActionEvent from buttons
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == jbtPrior)
{
// Make sure index is nonnegative
if (currentIndex == 0) currentIndex = TOTAL_NUMBER_OF_IMAGES;
currentIndex = (currentIndex - 1)%TOTAL_NUMBER_OF_IMAGES;
jlblImageViewer.setIcon(imageIcon[currentIndex]);
}
else if (e.getSource() == jbtNext)
{
currentIndex = (currentIndex + 1)%TOTAL_NUMBER_OF_IMAGES;
jlblImageViewer.setIcon(imageIcon[currentIndex]);
}
}
}
你只要确保D盘下有个L1.gif的图片就好了D:\\L1.gif
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;@SuppressWarnings("serial")
public class LabelDemo extends JFrame implements ActionListener {
// Declare an ImageIcon array. There are total 52 images
private ImageIcon[] imageIcon = new ImageIcon[52]; // The current image index
private int currentIndex = 0; // Buttons for browsing images
private JButton jbtPrior, jbtNext; // Label for displaying images
private JLabel jlblImageViewer = new JLabel(); final int TOTAL_NUMBER_OF_IMAGES = 52; // Main Method
public static void main(String[] args) {
LabelDemo frame = new LabelDemo();
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
} // Default Constructor
public LabelDemo() {
setTitle("Label Demo"); // Load images into imageIcon array
for (int i = 1; i <= 52; i++) {
imageIcon[i - 1] = new ImageIcon("D:\\L1.gif");
} // Show the first image
jlblImageViewer.setIcon(imageIcon[currentIndex]); // Set center alignment
jlblImageViewer.setHorizontalAlignment(SwingConstants.CENTER);
// jlblImageViewer.setVerticalAlignment(SwingConstants.CENTER); // Panel jpButtons to hold two buttons for browsing images
JPanel jpButtons = new JPanel();
jpButtons.add(jbtPrior = new JButton());
jbtPrior.setIcon(new ImageIcon("D:\\L1.gif"));
jpButtons.add(jbtNext = new JButton());
jbtNext.setIcon(new ImageIcon("D:\\L1.gif")); // Add jpButton and the label to the frame
getContentPane().add(jlblImageViewer, BorderLayout.CENTER);
getContentPane().add(jpButtons, BorderLayout.SOUTH); // Register listeners
jbtPrior.addActionListener(this);
jbtNext.addActionListener(this);
} // Handle ActionEvent from buttons
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jbtPrior) {
// Make sure index is nonnegative
if (currentIndex == 0)
currentIndex = TOTAL_NUMBER_OF_IMAGES;
currentIndex = (currentIndex - 1) % TOTAL_NUMBER_OF_IMAGES;
jlblImageViewer.setIcon(imageIcon[currentIndex]);
} else if (e.getSource() == jbtNext) {
currentIndex = (currentIndex + 1) % TOTAL_NUMBER_OF_IMAGES;
jlblImageViewer.setIcon(imageIcon[currentIndex]);
}
}
}