JTabbedPane组件何时获得其大小?

时间:2013-05-17 18:48:11

标签: java swing jpanel jtabbedpane

当我将JPanel添加到JTabbedPane以计算其他值时,我需要知道add(Component)的大小。如果我调用Component方法,JTabbedPane tabbedPane = new JTabbedPane(); JPanel panel = new JPanel(); panel.setBackGround(Color.RED); // pretty colors! tabbedPane.add(panel); int newTabIndex = tabbedPane.indexOfComponent(panel); tabbedPane.setSelectedIndex(newTabIndex); 何时获得其大小?

例如,

JTabbedPane tabbedPane = new JTabbedPane();
JPanel panel = new JPanel();
panel.setBackGround(Color.RED);
tabbedPane.add(panel);
panel.setVisible(true); // NEW LINE


int newTabIndex = tabbedPane.indexOfComponent(panel);
tabbedPane.setSelectedIndex(newTabIndex);

提前谢谢!

修改 @mKorbel建议将JPanel的可见性设置为true:

JPanel

不幸的是,当我将此更改应用于我自己的程序时,{{1}}的宽度和高度仍然分别为0和0。我还能尝试什么?

3 个答案:

答案 0 :(得分:3)

JTabbedPane在连接到能够铺设它的容器之前不会调整大小。

Window#pack的工作是确定子组件的首选大小,并根据布局管理器的要求调整它们的大小。

仅在此时,组件(以及您的JTabbedPane)的大小。

也可以调整组件的大小以响应容器层次结构中的更改,但基本过程是相同的。 JTabbedPane的父容器将决定何时需要更新内容并执行新的布局操作,根据需要调整组件大小

您的所有示例都没有显示标签窗格已添加到父容器...

更新了简单测试...

以下示例基本上演示了何时调整组件的大小。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestTabSize {

    public static void main(String[] args) {
        new TestTabSize();
    }

    public TestTabSize() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JPanel panel = new JPanel() {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(200, 200);
                    }
                };

                panel.addComponentListener(new ComponentAdapter() {
                    @Override
                    public void componentResized(ComponentEvent e) {
                        System.out.println("Resized to " + e.getComponent().getSize());
                    }

                    @Override
                    public void componentMoved(ComponentEvent e) {
                        System.out.println("Moved to " + e.getComponent().getLocation());
                    }
                });

                System.out.println("Before been added = " + panel.getSize());

                JTabbedPane tabbedPane = new JTabbedPane();
                tabbedPane.addTab("test", panel);

                System.out.println("Before been added to tab = " + panel.getSize());

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                System.out.println("Before been added to frame = " + panel.getSize());
                frame.add(tabbedPane);
                System.out.println("After been added to frame = " + panel.getSize());
                frame.pack();
                System.out.println("After been packed = " + panel.getSize());
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
                System.out.println("After been visible = " + panel.getSize());
            }
        });
    }

}

<强>输出...

从输出中可以看出,直到帧为packed,没有任何大小

Before been added = java.awt.Dimension[width=0,height=0]
Before been added to tab = java.awt.Dimension[width=0,height=0]
Before been added to frame = java.awt.Dimension[width=0,height=0]
After been added to frame = java.awt.Dimension[width=0,height=0]
After been packed = java.awt.Dimension[width=200,height=200]
After been visible = java.awt.Dimension[width=200,height=200]
Resized to java.awt.Dimension[width=200,height=200]
Moved to java.awt.Point[x=11,y=33]

答案 1 :(得分:1)

  

组件何时获得其大小?

  1. JFrame.pack()之后(不是您的问题)

  2. GUI中
  3. isVisible()已经存在(您的问题)

答案 2 :(得分:0)

JTabbedPane的大小取决于添加到其中的最大组件的大小。在您的情况下,必须使用JPanel.setPreferredSize()方法设置JPanel的大小。这将确保正确呈现JPanel