0

我有从 JPanel 扩展的 CPanel 扩展的VTreePanel类。该类有一个JSplitPane对象,该对象分为两个区域:左和右。左侧包含树形菜单选择对象。在右侧,它包含JTabbedPane对象。VTreePanel 类是这样的:

public final class VTreePanel extends CPanel
    implements ActionListener
{
    private JSplitPane centerSplitPane = new JSplitPane();

    private JTabbedPane tabbedPane;

    ...

    // GET method for the tabbedPane    
    public JTabbedPane getTabbedPane() {
        return tabbedPane;
    }

    // Constructor
    public VTreePanel(int WindowNo, boolean hasBar, boolean editable)
    {

        ...

        tabbedPane = new JTabbedPane();
        centerSplitPane.add(treePart, JSplitPane.LEFT);
        centerSplitPane.add(tabbedPane, JSplitPane.RIGHT);  // Look at this

        ...
    }

}

在构造函数中,我将树选择 (treePart) 和 JTabbedPane 对象 ( tabbedPane ) 添加到 JSplitPane 对象 (centerSplitPane) 中。我还没有在 tabbedPane 中添加任何选项卡。看下面的截图:

http://i45.tinypic.com/2v3j0nl.jpg

那么当用户单击其中一个菜单时如何添加选项卡?

我有AMenu类,它实现了 PropertyChangeListener,当用户单击菜单时触发 propertyChange 方法:

public final class AMenu extends CFrame
    implements ActionListener, PropertyChangeListener, ChangeListener
{

    private VTreePanel treePanel = null;    // this is the VTreePanel object

    ...

    public void propertyChange(PropertyChangeEvent e)
    {
        ...

        // Here I pass the VTreePanel object as parameter to AMenuStartItem thread object
        (new AMenuStartItem(cmd, true, sta, this, treePanel)).start();
    }

}

您可以看到我有 VTreePanel 对象 ( treePanel ),并且我将 VTreePanel 对象作为参数传递给AMenuStartItem线程。AMenuStartItem 包含执行在 JTabbedPane 中添加 Tab 的逻辑(请记住,JTabbedPane 对象 (tabbedPane) 在 VTreePanel 中)。

这是AMenuStartItem线程类:

public class AMenuStartItem extends Thread implements ActionListener
{
    private VTreePanel m_vtreePanel;

    public AMenuStartItem (int ID, boolean isMenu, String name, AMenu menu, VTreePanel vtreepanel)
    {
        ...

        m_vtreePanel = vtreepanel;  // save the VTreePanel object
    }

    // The thread method that executed when thread is started
    public void run()
    {
        ...

        startWindow(0, cmd);

        ...
    }

    private void startWindow(int AD_Workbench_ID, int AD_Window_ID)
    {
        ...

        // Here I perform adding new tab
        m_vtreePanel.getTabbedPane().addTab(frame.getTitle(), frame.getAPanel());

        ...
    }

}

因此,getTabbedPane()返回了 JTabbedPane 对象并执行了 addTab() 方法,但根本没有显示任何选项卡。

有谁知道如何解决这个问题?

4

1 回答 1

1

与 UI 的所有交互都必须通过事件调度线程完成,没有例外......

public class AMenuStartItem extends Thread implements ActionListener
{
    private VTreePanel m_vtreePanel;

    public AMenuStartItem (int ID, boolean isMenu, String name, AMenu menu, VTreePanel vtreepanel)
    {
        ...

        m_vtreePanel = vtreepanel;  // save the VTreePanel object
    }

    // The thread method that executed when thread is started
    public void run()
    {
        ...

        startWindow(0, cmd);

        ...
    }

    private void startWindow(final int AD_Workbench_ID, final int AD_Window_ID)
    {
        ...

        if (EventQueue.isDispatchingThread()) {
            // This is safe, we're in the EDT
            m_vtreePanel.getTabbedPane().addTab(frame.getTitle(), frame.getAPanel());
            m_vtreePanel.getTabbedPane().invalidate();
            m_vtreePanel.getTabbedPane().repaint();
        } else {
            // This is unsafe, we need to resync with the EDT
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    // Here I perform adding new tab
                    m_vtreePanel.getTabbedPane().addTab(frame.getTitle(), frame.getAPanel());
                    m_vtreePanel.getTabbedPane().invalidate();
                    m_vtreePanel.getTabbedPane().repaint();

                }
            });
        }

        ...
    }

}
于 2012-10-18T04:37:16.893 回答