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

时间:2009-05-27 17:07:08

标签: java swing jtabbedpane

如何选择一个标签,就像点击一个按钮一样? 我用Google搜索并查看了所有操作,但只有这么多... :(

有人知道吗?

提前致谢!

5 个答案:

答案 0 :(得分:13)

在按钮上添加一个动作侦听器,该按钮在JTabbedPane上调用setSelectedComponent或setSelectedIndex。

答案 1 :(得分:1)

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

答案 2 :(得分:1)

如果你的jtabbedpane的名字是mytabbedpane,它就像这样:

mytabbedpane.getSelectedIndex();

返回该选项卡的int(0,1 .. n)或

mytabbedpane.getSelectedComponent();

返回标签名称的字符串(“Firts标签”,“第二个标签”,......)。

如果你想对布尔逻辑使用“getSelectedComponent()”,你应该写一些类似的东西:

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

和“getSelectedIndex()”当然是:

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

答案 3 :(得分:1)

双击按钮,输入以下代码

import QtQuick 2.11
import QtQuick.Window 2.11
import QtQuick.Controls 2.4
import QtQuick.Controls.Styles 1.4
import QtQuick.Controls 2.4
import QtQuick.Layouts 1.3


Window {
    id: bigWin
    objectName: 'bigWin'
    visible: true
    width: 640
    height: 480
    color: "#000000"
    title: qsTr("Hello World")

    Rectangle{
        id:subtitlesBar
        color:"black"
        border.width: 3
        border.color: "#ba2121"
        anchors.top: parent.top
        anchors.topMargin: 0
        anchors.bottom: barRect.top
        anchors.bottomMargin: 5
        anchors.right: parent.right
        anchors.left: parent.left

        RowLayout {
            id: nativeSubtitles
            visible: true
            anchors.left: subtitlesBar.left
            anchors.right: subtitlesBar.right
            height: childrenRect.height
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 10



            Rectangle{
                id:theBoss
                Layout.fillWidth: true
                Layout.fillHeight:  true
                Layout.rightMargin:  50
                Layout.leftMargin: 50
                Layout.maximumWidth:  nativeSubtitles.width
                color:"transparent"
                height: childrenRect.height

                Label {
                    id: nativeSubs

                    // Monitor the width
                    onWidthChanged:  {

                        if(width > parent.width -50 )
                            width=parent.width -50
                    }
                    onTextChanged:if (width<=parent.width -50) width=undefined
                    text: inPut.text
                    anchors.horizontalCenter: parent.horizontalCenter
                    wrapMode: Text.WrapAtWordBoundaryOrAnywhere
                    font.pixelSize: 24
                    horizontalAlignment: Text.AlignHCenter
                    opacity: 1
                    background: Rectangle {
                        id : orgnOne
                        color: "orange"
                        opacity: 0.5
                        width: theBoss.childrenRect.width
                        height: theBoss.childrenRect.height
                    }
                }
            }
        }
    }

    Rectangle {
        id: barRect
        height: 2
        color: "#ffffff"
        anchors.bottom: inPut.top
        anchors.bottomMargin: 0
        anchors.left: parent.left
        anchors.leftMargin: 0
        anchors.right: parent.right
        anchors.rightMargin: 0
    }

    TextInput{
        id:inPut
        anchors.bottom: parent.bottom
        height: 60
        color: "#2d9604"
        font.pointSize: 14
        anchors.left: parent.left
        anchors.leftMargin: 0
        anchors.right: parent.right
        anchors.rightMargin: 0

    }

}

标签从0到N从左到右顺序

答案 4 :(得分:0)

试试这段代码:

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