
jtabbedpane 类中的 add() 和 addtab() 方法
jtabbedpane 类提供两种添加选项卡的方法:add() 和 addtab()。
这两种方法的主要区别在于它们的返回值:
- add():返回添加到选项卡中的组件的索引。
- addtab():返回所创建的选项卡的索引。
示例
以下示例展示了 add() 和 addtab() 方法之间的区别:
import javax.swing.jtabbedpane;
public class jtabbedpanedemo {
public static void main(string[] args) {
jtabbedpane tabbedpane = new jtabbedpane();
// 使用 add() 方法添加组件
component component1 = new jlabel("标签 1");
int componentindex = tabbedpane.add("标签 1", component1);
// 使用 addtab() 方法添加选项卡
component component2 = new jlabel("标签 2");
int tabindex = tabbedpane.addtab("标签 2", component2);
system.out.println("添加的组件索引:" + componentindex);
system.out.println("创建的选项卡索引:" + tabindex);
}
}输出结果:
添加的组件索引:0 创建的选项卡索引:1
由此可见,add() 方法返回组件索引(0),而 addtab() 方法返回选项卡索引(1)。










