gpt4 book ai didi

java - ButtonTabComponent 在 JTabbedPane 中添加 AquaTabbedPaneCopyFromBasicUI$TabContainer

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:15:06 25 4
gpt4 key购买 nike

我主要是尝试在 jtabbedpane 的每个选项卡中添加一堆 JPanel,它自己运行良好。但是当按照 Javatutorial 向每个选项卡添加关闭按钮时,它会在第一次(也是第一次)添加按钮之后将这个奇怪的 tabContainer 添加到 jtabbedpane。我意识到 Aqua 与 mac 相关,但问题仍然出现在 linux 和 windows 上。

我基本上在做:

JTabbedPane pane;

this.add("channel",new JTextArea("texttexttext"));

ButtonTabComponent ctb = new ButtonTabComponent(pane);

this.setTabComponentAt(pane.indexOfTab("channel"),ctb);

第一个选项卡也可以完美运行,但由于 AquaTabbedPaneCopyFromBasicUI$TabContainer,所有接下来添加的选项卡都被推送了一个索引,如屏幕截图所示。因此,当我试图让它们离开选项卡 Pane 以更新一些信息时(检查下面链接的代码)我无法转换它,因为从 indexOfTab(Channel) 返回的索引蜂鸣是 aqua 的索引..

调试截图和jtabbedPane数组:http://server.westman.no/free/Skjermbilde%202011-11-18%20kl.%2012.32.02.png

更糟糕的是我无法删除选项卡 Pane 中的索引 2,我只是得到 outOfBoundsException,但我可以使用 getComponent(2),(那是我第一次发现错误的地方,因为我可以 getComponent(title) 并且它会尝试返回 Aqua... 然后我不能将它转换为一个 singletab 对象)

这是一个超现实的问题,可能看起来像一些 youHaveToHaveDoneSomethingElseWrong,但我已经处理了一个星期,在让几个人看过之后,我离解决方案还差得很远。 (当然,这可能是一个 gotDamnUrADumbAss 问题……)

希望这对某人有意义!

有问题的代码:选项卡处理程序:http://apps.netcrawlr.net/p/pastebin.php?show=41

如果您想查看其他引用内容,请发表评论。

sscce:

    package jtabbedtest;

import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;

/**
*
* @author hallvardwestman
*/
public class Jtabbedtest {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here


JFrame jf = new JFrame();
JTabbedPane jt = new JTabbedPane();
//debugChecks for whats in jtabbedpane
Object[] o = jt.getComponents();
jf.add(jt);

jt.addTab("a",new JTextArea("a"));
int tabIndex = jt.indexOfTab("a");

ButtonTabComponent ctb = new ButtonTabComponent(jt);
jt.setTabComponentAt(tabIndex, ctb);
/*
* adding closebutton
*/
//debugChecks for whats in jtabbedpane
o = jt.getComponents();
jf.setVisible(true);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}
}

and just put this : http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TabComponentsDemoProject/src/components/ButtonTabComponent.java in a new file called ButtonTabComponent

最佳答案

目前我也在开发使用 JTabbedPane 的应用程序,一切对我来说都很好,添加、删除选定的选项卡、其他选项卡和所有选项卡。

首先,您应该 - 不是必须 - 提供整数变量来保存您已有的选项卡的总和,假设它称为 tabCount

检查选项卡是否存在于选项卡 Pane 中的代码:

private boolean isTabExist(String title) {
for (int i = 0; i < tabCount; i++) {
if (jTabbedPane1.getTitleAt(i).equalsIgnoreCase(title)) {
jTabbedPane1.setSelectedIndex(i);
return true;
}
}
return false;
}

插入新标签的代码:(在我的应用中,如果该标签已经添加,则不会再次添加)

if (!isTabExist("My Tab")) {
jTabbedPane1.insertTab("My Tab", null, yourCustomPanel, "My tab", tabCount);
jTabbedPane1.setSelectedIndex(tabCount);
tabCount = jTabbedPane1.getTabCount();
}

删除选定的标签(在任何索引中)

jTabbedPane1.remove(jTabbedPane1.getSelectedIndex());  // remove selected tab
tabCount = jTabbedPane1.getTabCount();

尝试上面的代码并告诉我如果您在删除某些索引处的选项卡时仍然遇到问题...

关于java - ButtonTabComponent 在 JTabbedPane 中添加 AquaTabbedPaneCopyFromBasicUI$TabContainer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8182059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com