简体   繁体   English

Java JTabbedPane,如何从按钮中选择一个选项卡?

[英]Java JTabbedPane, how can I select a tab from a button?

How can I select a tab as if it was clicked by clicking on a button? 如何通过单击按钮选择一个标签,就好像它被点击一样? I have googled and looked at all the actions but there are just just so many... :( 我用Google搜索并查看了所有操作,但只有这么多...... :(

Anyone know off hand? 有人知道吗?

Thanks in advance! 提前致谢!

向调用setSelectedComponent的按钮添加动作侦听器,或者在JTabbedPane上调用setSelectedIndex。

If your jtabbedpane's name is mytabbedpane it goes like this: 如果你的jtabbedpane的名字是mytabbedpane,它是这样的:

mytabbedpane.getSelectedIndex();

which returns the int of that tab (0,1 .. n) or 返回该选项卡的int(0,1 .. n)或

mytabbedpane.getSelectedComponent();

which returns the String of the tab's name ("Firts tab","Second tab",...). 返回选项卡名称的字符串(“Firts选项卡”,“第二个选项卡”,...)。

If you want to use the "getSelectedComponent()" for boolean logic you should write something like: 如果你想对布尔逻辑使用“getSelectedComponent()”,你应该写如下:

if (mytabbedpane.getSelectedComponent().equals("First tab")) {
     //code here
}

and for the "getSelectedIndex()" one is of course: 对于“getSelectedIndex()”,当然是:

if (mytabbedpane.getSelectedIndex() == 0) {
     //code here
}

Double click on button, put the follwing code 双击按钮,输入以下代码

JTabbedPane.setSelectedIndex(1);

Tabs starts from 0 to N left to right order 标签从0到N从左到右开始

我不确定你对这个按钮的意思,但你可能正在寻找setSelectedComponentsetSelectedIndex

Try this code: 试试这段代码:

tabbedPane.addTab(tabName, component);
int count = tabbedPane.getTabCount();
tabbedPane.setSelectedIndex(count-1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM