JTabbedPane 中使用单个 JPanel 的多个选项卡?

发布于 2024-12-05 11:12:39 字数 506 浏览 0 评论 0原文

是否可以对 JTabbedPane 中的多个选项卡项使用单个 JPanel

EG:

JTabbedPanel tabs=new JTabbePanel();

JPanel panel=new JPanel();
JButton but=new JButton("TEXT");
but.addActionlistener(this);
panel.add(but)

tabs.add("First",panel);
tabs.add("Second",panel);

JTabbedPane 中添加一个 ActionListener 以通知程序选项卡更改(将 cur_tab 更改为选项卡编号)

public void actionPerformed(..)
{ System.out.println("Now in "+cur_tab); }

Is it possible to use a single JPanel for multiple tab items in JTabbedPane?

EG:

JTabbedPanel tabs=new JTabbePanel();

JPanel panel=new JPanel();
JButton but=new JButton("TEXT");
but.addActionlistener(this);
panel.add(but)

tabs.add("First",panel);
tabs.add("Second",panel);

An ActionListener is added to the JTabbedPane to notify the program of tab changes (change cur_tab to tab number)

public void actionPerformed(..)
{ System.out.println("Now in "+cur_tab); }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

数理化全能战士 2024-12-12 11:12:39

同一个组件不能用于多个选项卡

此处

The same component cannot be used for several tabs

taken from here

陈年往事 2024-12-12 11:12:39

听起来您需要 JPanel 的子类。只需创建一个处理复杂布局的抽象类,并让子类创建所需的必要 GUI 元素。

如果您只是插入一个文本框、按钮等,您甚至可能不需要每个选项卡的子类。只需创建基类的多个实例并添加您需要的组件即可。

新面板和其他 GUI 项目相对便宜。 Swing GUI 中的性能问题更可能来自事件处理或触发过多的事件,而不是其复杂程度或组件数量。让事情更容易维护和理解,然后再担心性能。

Sounds like you need a subclass of JPanel. Just create an abstract class that handles the complex layout and have the subclasses create the necessary GUI elements that are required.

If you are just inserting a single text box, button, etc, you may not even need subclasses per tab. Just create multiple instances of the base class and add the component you need.

New panels and other GUI items are relatively cheap. Performance issues in a Swing GUI are more likely to come from event handling or firing too many events rather than how complex or how many components it has. Make things easier to maintain and understand, then worry about performance.

蒲公英的约定 2024-12-12 11:12:39

如果您将创建返回 JPanel 的类,那么是的,这是可能的

,但请注意:

存在两个 Tab 不能包含相同组件架构的长期错误,我的意思是架构例如,一个 Tab 包含 JPanel + JTextField + JButton,但第二个 JPanel 必须包含另一个 JComponent 的数字或类型 >

不幸的是(没什么特别的)BugsDatabase 此时无法访问

if you'll create class that returns JPanel then yes that's possible

but Notice:

there are long time Bug that two Tabs can't contains same component schemas, with schemas I means for example one Tab contains JPanel + JTextField + JButton, but then second JPanel must contains another Numbers or Type of JComponents

unfortunatelly (nothing special) BugsDatabase isn't accesible in this moment

两相知 2024-12-12 11:12:39

我有一个带有内存密集型组件的面板,我只想创建它的一个实例,但使用有吸引力的 JTabbedPane 更改它的行为。

我通过为每个选项卡创建空面板以及包含我的(单个)复杂组件的第三个面板来完成此操作。在 JTabbedPane 的 StateChange 事件中,我从前两个面板中删除第三个面板,并将其添加到新选择的面板中。

有点 hacky,但效果很好。

I had a panel with memory-intensive components on it which I only wanted to create one instance of, but change the behaviour of it using the attractive JTabbedPane.

I did it by creating empty panels for each tab, and a third panel that contains my (single) complicated components. On the StateChange event for the JTabbedPane, I remove the third panel from whichever of the first two it was in, and add it to whichever one is newly selected.

Bit hacky, but it works fine.

以歌曲疗慰 2024-12-12 11:12:39

您错了。
只需在函数 initComponents() 的第一个选项卡上设置一个面板,如下所示:

 p.add("1", MainPanel);

然后使用:

p.add("2", p.getTabComponentAt(0));

使用此方法,您将在 2 个选项卡上拥有相同的组件。

您可以使用 StateChanged 事件来更改此选项卡中的操作。
例如:

JTabbedPane p = (JTabbedPane)Tabbar;
int idx = p.getSelectedIndex();
   if(idx==0){
     Do something...
    }
   if(idx==1){
     Do something different...
    }

You are wrong.
Just set a panel on the first tab in function initComponents() like that:

 p.add("1", MainPanel);

Then use:

p.add("2", p.getTabComponentAt(0));

Using this metode you will have the same component on 2 tabs.

You can use StateChanged Event to change actions in this tabs.
For example:

JTabbedPane p = (JTabbedPane)Tabbar;
int idx = p.getSelectedIndex();
   if(idx==0){
     Do something...
    }
   if(idx==1){
     Do something different...
    }
情丝乱 2024-12-12 11:12:39

下面将允许您将具有不同标题的相同组件添加到 JTabbedPane:

JTabbedPane tabbedPane = new JTabbedPane()
    {
      boolean adding = false;
      @Override
      public void removeTabAt(int index)
      {
        if(!adding)
        {
          super.removeTabAt(index);
        }
      }

      @Override
      public void insertTab(String title, Icon icon, Component component, String tip, int index)
      {
        adding = true;
        super.insertTab(title, icon, component, tip, index);
        adding = false;
      }
    };

The following will allow you to add the same component with different titles to a JTabbedPane:

JTabbedPane tabbedPane = new JTabbedPane()
    {
      boolean adding = false;
      @Override
      public void removeTabAt(int index)
      {
        if(!adding)
        {
          super.removeTabAt(index);
        }
      }

      @Override
      public void insertTab(String title, Icon icon, Component component, String tip, int index)
      {
        adding = true;
        super.insertTab(title, icon, component, tip, index);
        adding = false;
      }
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文