如何通过点击按钮来选择一个标签页?
我已经谷歌搜索并查看了所有的操作,但是有太多了... :(
有人知道吗?
提前感谢!
给按钮添加一个动作监听器,调用JTabbedPane的setSelectedComponent或setSelectedIndex方法。
JTabbedPane.setSelectedIndex(1);
mytabbedpane.getSelectedIndex();
该函数返回该选项卡的整数值(0、1..n)或
mytabbedpane.getSelectedComponent();
返回选项卡的名称字符串(“第一个选项卡”,“第二个选项卡”等)。
如果您想使用“getSelectedComponent()”进行布尔逻辑运算,应编写类似以下内容的代码:
if (mytabbedpane.getSelectedComponent().equals("First tab")) {
//code here
}
而对于 "getSelectedIndex()",当然是:
if (mytabbedpane.getSelectedIndex() == 0) {
//code here
}
尝试这段代码:
tabbedPane.addTab(tabName, component);
int count = tabbedPane.getTabCount();
tabbedPane.setSelectedIndex(count-1);