xueyue_juan 2012-04-11 09:43
浏览 1294
已采纳

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

我的代码如下:
JTabbedDemo.java

import javax.swing.*;
import java.awt.*;

public class JTabbedDemo
{
public static void main(String[] args) throws Exception
{

    //显示外观风格
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

    JFrame jframe=new JFrame("奖金状况");
    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jframe.setVisible(true);

    JTabbedPane tp=new JTabbedPane();//创建一个选项卡容器,将之添加到顶层容器内
    jframe.setContentPane(tp);

    JPanel panel1=new JPanel();
    JPanel panel2=new JPanel();
    JPanel panel3=new JPanel();
    JPanel panel4=new JPanel();
    JPanel panel5=new JPanel();

    //添加选项卡容器,并且设置其中每个选项卡的标签以及其是否可启用。
    tp.addTab("panel1",panel1);
    tp.setEnabledAt(0,true);
    tp.setTitleAt(0,"个人收入状况");

    tp.addTab("panel2",panel2);
    tp.setEnabledAt(1,true);
    tp.setTitleAt(1,"工资");

    tp.addTab("panel2",panel3);
    tp.setEnabledAt(2,true);
    tp.setTitleAt(2,"奖金");

    tp.addTab("panel3",panel4);
    tp.setEnabledAt(3,true);
    tp.setTitleAt(3,"津贴");

    tp.addTab("panel4",panel5);
    tp.setEnabledAt(4,true);
    tp.setTitleAt(4,"社保");

    //设置其大小以及其选项卡的位置方向
    tp.setPreferredSize(new Dimension(800,600));
    tp.setTabPlacement(JTabbedPane.TOP);

    //设置选项卡在容器内的显示形式
    tp.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    jframe.pack();

    //创建8个标签组件,将5个中间窗口设置为流布局,并且将标签组件分别放入到其中
    JLabel l1=new JLabel("工资状况");
    JLabel l2=new JLabel("3000元/月");
    JLabel l3=new JLabel("奖金状况");
    JLabel l4=new JLabel("1500元/月");
    JLabel l5=new JLabel("津贴状况");
    JLabel l6=new JLabel("500元/月");
    JLabel l7=new JLabel("社保状况");
    JLabel l8=new JLabel("200元/月");

    panel2.setLayout(new FlowLayout());
    panel3.setLayout(new FlowLayout());
    panel4.setLayout(new FlowLayout());
    panel5.setLayout(new FlowLayout());

    panel2.add(l1);
    panel2.add(l2);
    panel3.add(l3);
    panel3.add(l4);
    panel4.add(l5);
    panel4.add(l6);
    panel5.add(l7);
    panel5.add(l8);

    jframe.pack();

}

}

错误信息如下:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.basic.BasicTabbedPaneUI.calculateTabWidth(Unknown So
urce)
at javax.swing.plaf.basic.BasicTabbedPaneUI$TabbedPaneLayout.calculateTa
bRects(Unknown Source)
at javax.swing.plaf.basic.BasicTabbedPaneUI$TabbedPaneLayout.calculateLa
youtInfo(Unknown Source)
at javax.swing.plaf.basic.BasicTabbedPaneUI$TabbedPaneLayout.layoutConta
iner(Unknown Source)
at java.awt.Container.layout(Unknown Source)
at java.awt.Container.doLayout(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validate(Unknown Source)
at javax.swing.RepaintManager.validateInvalidComponents(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

  • 写回答

1条回答 默认 最新

  • qq415241704 2012-04-11 13:55
    关注

    tp.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    这句话有问题,你注释掉就没异常了,我对swing不是很了解,应该是你这种写法有问题,你看看吧,希望对你有帮助 :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数据库原理及应用上机练习题
  • ¥30 征集Python提取PDF文字属性的代码
  • ¥15 如何联系真正的开发者而非公司
  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?