且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

添加新标签后更新JTabbedPane

更新时间:2023-12-01 18:08:22

您在构造函数中添加标签的代码如下:

Your code for adding a tab in the constructor is the following:

JPanel tab2 = new JPanel();
tab2.add(table);

tab2.setVisible(true);

center.add("Table Viewer", tab2);

// I'm thinking some sort of listener needs to be active, so it knows I'm adding a new
// tab, but I'm not sure how this actually works.
center.addPropertyChangeListener("foregroud", null);
center.repaint();

有2个错误和很多不必要的行.错误是:

There are 2 errors and a lot of unnecessary lines. The errors are:

  • center.add("Table Viewer", tab2);使用的是Container类的add函数.当您想使用center.addTab("Table Viewer", tab2);.

  • center.add("Table Viewer", tab2); is using the add function of the Container class. When you wanted to use center.addTab("Table Viewer", tab2);.

只是为了清除@peeskillet指出的内容,没有"foregroud"属性,也没有"forground"(根据您的评论),而是"foreground"属性.

Just to clear up what @peeskillet was pointing out, there is not a "foregroud" property, nor a "forground" (as per your comment), but a "foreground" property.

现在您需要做的只是以下事情:

Now what you need to do is just the following:

JPanel tab2 = buildTableViewerTab();
center.addTab("Table Viewer", tab2);

其中buildTableViewerTab()(返回JPanel)是创建所需的JPanel所需的代码.只需创建该组件并将其正确添加到tabbedPane中即可.

Where buildTableViewerTab() (returning a JPanel) is the code necessary to create the JPanel that you desire. Just create the component and add it to the tabbedPane properly.

在这里展示此代码的工作方式是一个演示此功能的简单可执行应用程序.同样,@ peeskillet在他的第二条评论中问您的是同样的示例,但是以您自己的方式进行,并且您的代码演示了您遇到的错误.尽管这样做,您可能会找到它们.

To show how this code works here is a simple executable application demonstrating this functionality. Again, what @peeskillet was asking you in his second comment is to do this same example but in your own way and with your code demonstrating the errors you were encountering. Although doing this you probably would have found them.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

public class AddTabsExample
{
    public static final void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new AddTabsExample();
            }
        });
    }

    public AddTabsExample()
    {
        JFrame frame = new JFrame("Tab adder frame");

        final JTabbedPane tabbedPane = new JTabbedPane();

        frame.add(tabbedPane);

        JButton addButton = new JButton("Add tab");
        addButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent arg0)
            {
                JPanel newTabComponent = new JPanel();
                int tabCount = tabbedPane.getTabCount();
                newTabComponent.add(new JLabel("I'm tab " + tabCount));
                tabbedPane.addTab("Tab " +tabCount, newTabComponent);
            }
        });
        frame.add(addButton, BorderLayout.SOUTH);

        addButton.doClick(); //add the first tab

        frame.setSize(800, 300);//frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
    }
}

执行结果: