Java:“添加选项卡按钮”对于 JTabbedPane

发布于 2024-08-16 05:07:50 字数 136 浏览 5 评论 0原文

是否可以像 Firefox 中那样向选项卡式窗格添加按钮。

在此处输入图像描述

加号按钮就是我想要的。

谢谢

Is it possible to add a button to a tabbed pane like in firefox.

enter image description here

The plus-button is what I want.

Thanks

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

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

发布评论

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

评论(4

仅一夜美梦 2024-08-23 05:07:50

我认为您应该能够通过构建自己的 JTabbedPaneUI 并使用 setUIJTabbedPane 上设置它来管理它。

您的 ComponentUI 具有获取可访问子级的方法。如果您指定一个 JButton 和一个 JLabel 那么您就可以开始工作了。

不过我自己还没有尝试过。这是“您自己承担风险”:)

I think you should be able to manage it by building your own JTabbedPaneUI and setting it on the JTabbedPane using setUI.

Your ComponentUI has methods to get a hold of the accessible children. If you specify a JButton and a JLabel then you may be in business.

I haven't attempted this myself though. This is "at your own risk" :)

ぶ宁プ宁ぶ 2024-08-23 05:07:50

您可以尝试以下操作:

public static void main (String[] args) {
    JFrame parent = new JFrame ();

    final JTabbedPane pane = new JTabbedPane ();
    pane.addTab ("test", null);
    FlowLayout f = new FlowLayout (FlowLayout.CENTER, 5, 0);

    // Make a small JPanel with the layout and make it non-opaque
    JPanel pnlTab = new JPanel (f);
    pnlTab.setOpaque (false);
    // Create a JButton for adding the tabs
    JButton addTab = new JButton ("+");
    addTab.setOpaque (false); //
    addTab.setBorder (null);
    addTab.setContentAreaFilled (false);
    addTab.setFocusPainted (false);

    addTab.setFocusable (false);

    pnlTab.add (addTab);

    pane.setTabComponentAt (pane.getTabCount () - 1, pnlTab);

    ActionListener listener = new ActionListener () {
        @Override
        public void actionPerformed (ActionEvent e) {
            String title = "Tab " + String.valueOf (pane.getTabCount () - 1);
            pane.addTab (title, new JLabel (title));
        }
    };
    addTab.setFocusable (false);
    addTab.addActionListener (listener);
    pane.setVisible (true);

    parent.add (pane);
    parent.setSize (new Dimension (400, 200));
    parent.setVisible (true);
}

Screenshot

You can try this:

public static void main (String[] args) {
    JFrame parent = new JFrame ();

    final JTabbedPane pane = new JTabbedPane ();
    pane.addTab ("test", null);
    FlowLayout f = new FlowLayout (FlowLayout.CENTER, 5, 0);

    // Make a small JPanel with the layout and make it non-opaque
    JPanel pnlTab = new JPanel (f);
    pnlTab.setOpaque (false);
    // Create a JButton for adding the tabs
    JButton addTab = new JButton ("+");
    addTab.setOpaque (false); //
    addTab.setBorder (null);
    addTab.setContentAreaFilled (false);
    addTab.setFocusPainted (false);

    addTab.setFocusable (false);

    pnlTab.add (addTab);

    pane.setTabComponentAt (pane.getTabCount () - 1, pnlTab);

    ActionListener listener = new ActionListener () {
        @Override
        public void actionPerformed (ActionEvent e) {
            String title = "Tab " + String.valueOf (pane.getTabCount () - 1);
            pane.addTab (title, new JLabel (title));
        }
    };
    addTab.setFocusable (false);
    addTab.addActionListener (listener);
    pane.setVisible (true);

    parent.add (pane);
    parent.setSize (new Dimension (400, 200));
    parent.setVisible (true);
}

Screenshot

习ぎ惯性依靠 2024-08-23 05:07:50

在类的默认构造函数和创建方法中编写以下代码,

    JPanel panel = new JPanel();
    tabbedPane.addTab("Welcome", null, panel, null);
    tabbedPane.addTab(" + ", null, panel1, null);

    tabbedPane.addChangeListener(new ChangeListener()
    {
        public void stateChanged(ChangeEvent evt)
        {
            JTabbedPane tabbedPane = (JTabbedPane)evt.getSource();

            if(tabbedPane.getSelectedIndex() == tabbedPane.indexOfTab(" + "))
            {
                createTab();
            }
        }
    });

在主类启动时声明并初始化int tab2 = 2;。它的工作。

private void createTab()
{
    tabbedPane.addTab("New Tab",new Panel());
    tabbedPane.addTab(" + ",null,panel1,null);
    tabbedPane.setSelectedIndex(tab2);
    tab2++;
}

Write Following Code in Default Constructor Of Class

    JPanel panel = new JPanel();
    tabbedPane.addTab("Welcome", null, panel, null);
    tabbedPane.addTab(" + ", null, panel1, null);

    tabbedPane.addChangeListener(new ChangeListener()
    {
        public void stateChanged(ChangeEvent evt)
        {
            JTabbedPane tabbedPane = (JTabbedPane)evt.getSource();

            if(tabbedPane.getSelectedIndex() == tabbedPane.indexOfTab(" + "))
            {
                createTab();
            }
        }
    });

And Create Method to declare and initialized int tab2 = 2; at Starting of main class. Its Worked.

private void createTab()
{
    tabbedPane.addTab("New Tab",new Panel());
    tabbedPane.addTab(" + ",null,panel1,null);
    tabbedPane.setSelectedIndex(tab2);
    tab2++;
}
不醒的梦 2024-08-23 05:07:50

我尝试了几种解决方案并采用了这个:

import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

public class TestTab {

    public static void main(String[] args) {
    JFrame parent = new JFrame();

    final JTabbedPane tabEntity = new JTabbedPane();
    tabEntity.addTab("Details", null, new JScrollPane());
    tabEntity.addTab("Context", null, new JScrollPane());
    tabEntity.addTab("", null, new JScrollPane());

    addButtonToTab(tabEntity);

    parent.add(tabEntity);
    parent.setSize(new Dimension(400, 200));
    parent.setVisible(true);
    }

    public static void addButtonToTab(final JTabbedPane tabEntity) {
    tabEntity.setTabComponentAt(tabEntity.getTabCount() - 1, new JButton(
        "+"));
    }
}

所以你有:

选项卡附近的按钮示例

I have tried several solutions and came with this one:

import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

public class TestTab {

    public static void main(String[] args) {
    JFrame parent = new JFrame();

    final JTabbedPane tabEntity = new JTabbedPane();
    tabEntity.addTab("Details", null, new JScrollPane());
    tabEntity.addTab("Context", null, new JScrollPane());
    tabEntity.addTab("", null, new JScrollPane());

    addButtonToTab(tabEntity);

    parent.add(tabEntity);
    parent.setSize(new Dimension(400, 200));
    parent.setVisible(true);
    }

    public static void addButtonToTab(final JTabbedPane tabEntity) {
    tabEntity.setTabComponentAt(tabEntity.getTabCount() - 1, new JButton(
        "+"));
    }
}

So you have:

Button near tab example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文