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)
------解决方案--------------------
URL resource = jabletest.class.getResource("/tab.JPG");
ImageIcon imageIcon = new ImageIcon(resource);
------解决方案--------------------
估计是图片位置不对
------解决方案--------------------
空指针
------解决方案--------------------
URL resource = jabletest.class.getResource("/tab.JPG");
这行程序有问题,请确定tab.jpg的位置,或者将tab.jpg的位置放在和你这个JAVA程序想的文件夹面,然后此行改成:
URL resource = jabletest.class.getResource("tab.JPG");