我的代码如下:
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)