Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JTabbedPane-带关闭按钮参考的选项卡_Java_Swing_Jtabbedpane - Fatal编程技术网

Java JTabbedPane-带关闭按钮参考的选项卡

Java JTabbedPane-带关闭按钮参考的选项卡,java,swing,jtabbedpane,Java,Swing,Jtabbedpane,我想用JAVA构建一个选项卡式界面,每个选项卡上都有关闭按钮。为此,我使用了以下类: 我的GUI上有一个按钮,可以创建一个新选项卡。假设我按了4次New Tab按钮,因此创建了4个选项卡: |选项卡0 |选项卡1 |选项卡2 |选项卡3 |(每个选项卡都包含一个关闭按钮) 现在,我决定关闭选项卡1,问题出现了,当我关闭中间选项卡时,所有索引都被重新排序,这意味着: |表0 |表2 |表3 |(表2将有索引1) 如果我现在尝试创建一个新选项卡,则会创建该选项卡,但新选项卡: |选项卡0 |选项卡2

我想用JAVA构建一个选项卡式界面,每个选项卡上都有关闭按钮。为此,我使用了以下类:

我的GUI上有一个按钮,可以创建一个新选项卡。假设我按了4次New Tab按钮,因此创建了4个选项卡:

|选项卡0 |选项卡1 |选项卡2 |选项卡3 |(每个选项卡都包含一个关闭按钮)

现在,我决定关闭选项卡1,问题出现了,当我关闭中间选项卡时,所有索引都被重新排序,这意味着:

|表0 |表2 |表3 |(表2将有索引1)

如果我现在尝试创建一个新选项卡,则会创建该选项卡,但新选项卡:

|选项卡0 |选项卡2 |选项卡3 |选项卡1 |(选项卡1没有关闭按钮)

如果我再次单击“新建”选项卡,我会得到:

|选项卡0 |选项卡2 |选项卡3 |选项卡1 |选项卡4 |(选项卡4很好,它有一个关闭按钮)

现在我决定关闭选项卡2,我得到:

|表0 |表3 |表1 |表4 |(表3现在将有索引1)

如果创建新选项卡:

|选项卡0 |选项卡3 |选项卡1 |选项卡4 |选项卡1 |(最后一个选项卡也没有关闭按钮)

我相信这是由索引引起的,我在stackoverflow上读到了一个类似的问题:一个可能的解决方案是:

向侦听器传递对选项卡项的引用,而不是其在选项卡窗格上的索引

我不知道该怎么做。如果有人有任何暗示,我会非常非常感激。 我需要为每个选项卡保留一个可靠的引用,因为每个选项卡都将打开一个文件,并且它将能够保存到一个文件,显然选项卡索引不可靠

编辑:我在代码中添加新选项卡的方式是:

...//main class
private final JTabbedPane pane = new JTabbedPane();
//I am using an array to store the tabs created
//I initialize the array with false. the idea was that when a new tab get created, one item in the array
//gets the true value. when the tab is closed, the array item (based on the index) is set back to false
arrTabList = new boolean[10];
        for(int i=0; i<10; i++){
            arrTabList[i] = false;
        }

...


public void newFile()  //this function opens a new tab
{
//parse the array to check the first item with false status
    for(int i=0; i<10; i++){
        if(!arrTabList[i]) {
            System.out.println("false");
            PanelCounter = i;
            break;
            }
    }
    newTab t = new newTab();   //object which contains the tab content (a bunch of graphical components, input fields mostly)
    pane.add("New Entry" + PanelCounter, t.createContentPane());   //adds the new tab to the main window
    pane.setTabComponentAt(PanelCounter, new ButtonTabComponent(pane, this));
    arrTabList[PanelCounter] = true;  //sets the item array to true
}

//when a tab is closed, this function is called in the listener:
public void decreaseCounter(int i)
{
        arrTabList[i] = false;  //sets the item array back to false
}
..//main类
专用最终JTabbedPane窗格=新JTabbedPane();
//我使用数组来存储创建的选项卡
//我用false初始化数组。其想法是,当创建一个新选项卡时,数组中的一项
//获取真实值。当选项卡关闭时,数组项(基于索引)被设置回false
arrtablelist=新布尔值[10];

对于(inti=0;i我正在检查您的代码,我可以使用

pane.remove(pane.getSelectedComponent());

在actionPerformed方法中。

我正在检查您的代码,我可以使用

pane.remove(pane.getSelectedComponent());

在actionPerformed方法中。

错误在于调用

pane.setTabComponentAt(PanelCounter, new ButtonTabComponent(pane, this));
您不想将该按钮添加到选项卡索引
PanelCounter
,而是添加到刚刚创建的选项卡索引中。可以使用
getTabCount()
获取其索引,当然此时该索引太高,因此:

pane.setTabComponentAt(pane.getTabCount()-1, new ButtonTabComponent(pane, this));

错误在于调用

pane.setTabComponentAt(PanelCounter, new ButtonTabComponent(pane, this));
您不想将该按钮添加到选项卡索引
PanelCounter
,而是添加到刚刚创建的选项卡索引中。可以使用
getTabCount()
获取其索引,当然此时该索引太高,因此:

pane.setTabComponentAt(pane.getTabCount()-1, new ButtonTabComponent(pane, this));

在这里,我创建了JTabbedPane选项卡,每个选项卡都有一个关闭按钮。 单击“关闭”按钮时,仅相应的选项卡关闭

//This is where a tab is created in some other function of same class
jtp.addTab("Create",new JPanel());  //jtp is a global JTabbedPane variable
int index = jtp.indexOfTab("Create");
jtp.setTabComponentAt(index,createTabHead("Create"));   

public JPanel createTabHead(String title)
{
    final String st=title;
    JPanel pnlTab = new JPanel();
    pnlTab.setLayout(new BoxLayout(pnlTab,BoxLayout.LINE_AXIS));
    pnlTab.setOpaque(false);
    JButton btnClose = new JButton("x");
    JLabel lblTitle = new JLabel(title+"    ");
    btnClose.setBorderPainted(false);
    btnClose.setOpaque(false);

    btnClose.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
             int i; 
            for(i=0;i<=jtp.getTabCount()-1;i++)//To find current index of tab
            {
            if(st.equals(jtp.getTitleAt(i)))
                    break;
            }                   
               jtp.removeTabAt(i);
             }
            });

     pnlTab.add(lblTitle);
    pnlTab.add(btnClose);
    return pnlTab;
}
//这是在同一类的其他函数中创建选项卡的地方
addTab(“Create”,new JPanel());//jtp是一个全局JTabbedPane变量
int index=jtp.indexOfTab(“创建”);
jtp.setTabComponentAt(索引,createTabHead(“Create”);
公共JPanel createTabHead(字符串标题)
{
最后一个字符串st=标题;
JPanel pnlTab=新的JPanel();
pnlTab.setLayout(新的BoxLayout(pnlTab,BoxLayout.LINE_轴));
pnlTab.set不透明(假);
JButton btnClose=新JButton(“x”);
JLabel lblTitle=新的JLabel(标题+“”);
btnClose.exe(假);
btnClose.setOpaque(false);
btnClose.addActionListener(新的ActionListener(){
已执行的公共无效操作(操作事件e){
int i;

对于(i=0;i这里我创建JTabbedPane选项卡,每个选项卡都有一个关闭按钮。 单击“关闭”按钮时,仅相应的选项卡关闭

//This is where a tab is created in some other function of same class
jtp.addTab("Create",new JPanel());  //jtp is a global JTabbedPane variable
int index = jtp.indexOfTab("Create");
jtp.setTabComponentAt(index,createTabHead("Create"));   

public JPanel createTabHead(String title)
{
    final String st=title;
    JPanel pnlTab = new JPanel();
    pnlTab.setLayout(new BoxLayout(pnlTab,BoxLayout.LINE_AXIS));
    pnlTab.setOpaque(false);
    JButton btnClose = new JButton("x");
    JLabel lblTitle = new JLabel(title+"    ");
    btnClose.setBorderPainted(false);
    btnClose.setOpaque(false);

    btnClose.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
             int i; 
            for(i=0;i<=jtp.getTabCount()-1;i++)//To find current index of tab
            {
            if(st.equals(jtp.getTitleAt(i)))
                    break;
            }                   
               jtp.removeTabAt(i);
             }
            });

     pnlTab.add(lblTitle);
    pnlTab.add(btnClose);
    return pnlTab;
}
//这是在同一类的其他函数中创建选项卡的地方
addTab(“Create”,new JPanel());//jtp是一个全局JTabbedPane变量
int index=jtp.indexOfTab(“创建”);
jtp.setTabComponentAt(索引,createTabHead(“Create”);
公共JPanel createTabHead(字符串标题)
{
最后一个字符串st=标题;
JPanel pnlTab=新的JPanel();
pnlTab.setLayout(新的BoxLayout(pnlTab,BoxLayout.LINE_轴));
pnlTab.set不透明(假);
JButton btnClose=新JButton(“x”);
JLabel lblTitle=新的JLabel(标题+“”);
btnClose.exe(假);
btnClose.setOpaque(false);
btnClose.addActionListener(新的ActionListener(){
已执行的公共无效操作(操作事件e){
int i;

对于(i=0;如果引用的建议不适用于此处,
buttonabcomponent
不会在内部存储选项卡索引,但会根据需要进行查找。我怀疑错误可能是由于添加新选项卡的方式造成的;您是否可以为此显示代码?添加的代码。感谢您的输入引用的建议不适用于此处,
buttonabcomponent
不在内部存储选项卡索引,而是按需查找。我怀疑错误可能是由于您添加新选项卡的方式造成的;您可以显示该选项卡的代码吗?代码已添加。感谢您的输入,它会影响所选选项卡。我希望无论选项卡是否已选中,此功能都能正常工作。即使未选中选项卡,您也应该能够关闭它们。谢谢这会影响所选选项卡。我希望无论是否选择该选项卡,此功能都能正常工作。即使未选择选项卡,您也应该能够关闭它们。谢谢