Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
package three; //这段程序主要是创建一个JTabbedPane面板,为每一个标签设置一个名称 import javax.swing.*; import java.awt.*; public class test7 { public static void main(String[] args) { try {//显示外观风格 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(Exception e){ e.printStackTrace(); } JFrame frame = new JFrame ("资金状况"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.setVisible (true);//默认为false JTabbedPane tp=new JTabbedPane();//创建一个选项卡容器,将之添加到顶层容器内 frame.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 ("panel3", panel3); tp.setEnabledAt (2, true); tp.setTitleAt (2,"奖金"); tp.addTab ("panel4", panel4); tp.setEnabledAt(3,true); tp.setTitleAt(3,"津贴"); tp.addTab ("panel5", panel5); tp.setEnabledAt(4,true); tp.setTitleAt(4,"社保"); ///设置其大小以及其选项卡的位置方向 tp.setPreferredSize (new Dimension (500,200)); tp.setTabPlacement (JTabbedPane.TOP); ///设置选项卡在容器内的显示形式 tp.setTabLayoutPolicy (JTabbedPane.SCROLL_TAB_LAYOUT); frame.pack(); } } Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.plaf.basic.BasicTabbedPaneUI.rotateInsets(BasicTabbedPaneUI.java:1920) at javax.swing.plaf.basic.BasicTabbedPaneUI.getTabAreaInsets(BasicTabbedPaneUI.java:1579) at javax.swing.plaf.basic.BasicTabbedPaneUI$TabbedPaneScrollLayout.calculateTabRects(BasicTabbedPaneUI.java:2792) at javax.swing.plaf.basic.BasicTabbedPaneUI$TabbedPaneLayout.calculateLayoutInfo(BasicTabbedPaneUI.java:2263) at javax.swing.plaf.basic.BasicTabbedPaneUI$TabbedPaneScrollLayout.layoutContainer(BasicTabbedPaneUI.java:2596) at java.awt.Container.layout(Container.java:1401) at java.awt.Container.doLayout(Container.java:1390) at java.awt.Container.validateTree(Container.java:1473) at java.awt.Container.validateTree(Container.java:1480) at java.awt.Container.validateTree(Container.java:1480) at java.awt.Container.validate(Container.java:1448) at javax.swing.RepaintManager.validateInvalidComponents(RepaintManager.java:379) at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:109) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEvent(EventQueue.java:461) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149) at java.awt.EventDispatchThread.run(EventDispatchThread.java:110) 初次接触swing 。。完全看不懂这个异常从哪里出来的。。 求讲解。~
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
真的是这种不起眼的错误,多谢回答
你把frame.setVisible(true)这句放到最后一行试试看,估计是因为Panel组件还未初始化你就显示了frame,造成内部空引用的问题。
你把frame.setVisible(true)这句放到最后一行试试看,估计是因为Panel组件还未初始化你就显示了frame,造成内部空引用的问题。