求助 这个程序哪错了

兔子侠客 2011-11-28 02:18:59
import java.awt.*;
import java.net.*;

import javax.swing.*;
import javax.swing.event.*;

public class jabletest extends JFrame {

public static void main(String args[]) {
jabletest frame = new jabletest();
frame.setVisible(true);
}

public jabletest() {
super();
getContentPane().setFocusCycleRoot(true);
setTitle("选项卡面板");
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTabbedPane tabbedPane = new JTabbedPane();
// 设置选项卡标签的布局方式
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
tabbedPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
// 获得被选中选项卡的索引
int selectedIndex = tabbedPane.getSelectedIndex();
// 获得指定索引的选项卡标签
String title = tabbedPane.getTitleAt(selectedIndex);
System.out.println(title);
}
});
getContentPane().add(tabbedPane, BorderLayout.CENTER);
URL resource = jabletest.class.getResource("/tab.JPG");
ImageIcon imageIcon = new ImageIcon(resource);
final JLabel tabLabelA = new JLabel();
tabLabelA.setText("选项卡A");
// 将标签组件添加到选项卡中
tabbedPane.addTab("选项卡A", imageIcon, tabLabelA, "点击查看选项卡A");
final JLabel tabLabelB = new JLabel();
tabLabelB.setText("选项卡B");
tabbedPane.addTab("选项卡B", imageIcon, tabLabelB, "点击查看选项卡B");
final JLabel tabLabelC = new JLabel();
tabLabelC.setText("选项卡C");
tabbedPane.addTab("选项卡C", imageIcon, tabLabelC, "点击查看选项卡C");
tabbedPane.setSelectedIndex(2); // 设置索引为2的选项卡被选中
tabbedPane.setEnabledAt(0, false); // 设置索引为0的选项卡不可用
}
}


这个是提示的异常
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
at jabletest.<init>(jabletest.java:34)
at jabletest.main(jabletest.java:10)



...全文
154 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zmsgz 2011-11-29
  • 打赏
  • 举报
回复
URL resource = jabletest.class.getResource("/tab.JPG");
这行程序有问题,请确定tab.jpg的位置,或者将tab.jpg的位置放在和你这个JAVA程序想的文件夹面,然后此行改成:
URL resource = jabletest.class.getResource("tab.JPG");
a619225471 2011-11-28
  • 打赏
  • 举报
回复
空指针
24K純帥 2011-11-28
  • 打赏
  • 举报
回复
估计是图片位置不对
jhson 2011-11-28
  • 打赏
  • 举报
回复
URL resource = jabletest.class.getResource("/tab.JPG");
ImageIcon imageIcon = new ImageIcon(resource);

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧