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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/916948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:01:56  来源:igfitidea点击:

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

javaswingjtabbedpane

提问by Relequestual

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... :(

如何选择一个选项卡,就像通过单击按钮单击它一样?我用谷歌搜索并查看了所有操作,但只有这么多...... :(

Anyone know off hand?

有人知道吗?

Thanks in advance!

提前致谢!

采纳答案by Yishai

Add an action listener to the button that calls setSelectedComponent, or setSelectedIndex on the JTabbedPane.

向调用 setSelectedComponent 或 JTabbedPane 上的 setSelectedIndex 的按钮添加一个动作侦听器。

回答by Michael Myers

I'm not sure what you mean about the button, but you might be looking for setSelectedComponentor setSelectedIndex.

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

回答by Dimitrios K.

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",...).

它返回选项卡名称的字符串(“第一个选项卡”,“第二个选项卡”,...)。

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
}

回答by Neo

Try this code:

试试这个代码:

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

回答by Khaliq Izrail Haxor

Double click on button, put the follwing code

双击按钮,输入以下代码

JTabbedPane.setSelectedIndex(1);

Tabs starts from 0 to N left to right order

制表符从 0 到 N 从左到右的顺序