JTabbedPane组件何时获得其大小? [英] When does a JTabbedPane component get its size?
问题描述
将JPanel
的大小添加到JTabbedPane
中以计算其他值时,我需要知道它的大小.如果我调用add(Component)
方法,什么时候Component
会得到它的大小?
I need to know the size of a JPanel
when I add it to a JTabbedPane
to compute other values. If I call the add(Component)
method, when does the Component
get its size?
例如,
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);
提前谢谢!
修改 @mKorbel建议将JPanel的可见性设置为true:
Edit @mKorbel suggested setting the visibility of the JPanel to true:
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);
不幸的是,当我将此更改应用于自己的程序时,JPanel
的宽度和高度分别为0和0.我还能尝试什么?
Unfortunately, when I applied this change to my own program, the JPanel
still had a width and height of 0 and 0, respectively. What else can I try?
推荐答案
JTabbedPane
在调整大小之前,除非将其附加到能够放置它的容器上.
JTabbedPane
won't be sized until it is attached to a container that is capable of laying it out.
Window#pack
的工作是确定子组件的首选大小,并根据布局管理器的要求确定子组件的大小.
Window#pack
s job is to determine the preferred size of the child components and size them based on the requirements of the layout manager.
目前仅对组件(和您的JTabbedPane
)进行大小调整.
Only at this time would a component (and your JTabbedPane
) be sized.
也可以根据容器层次结构的更改来调整组件的大小,但是基本过程是相同的. JTabbedPane
父容器将决定何时需要更新其内容并执行新的布局操作,并根据需要调整组件的大小
Components may be resized in response to changes in the container hierarchy as well, but the basic process is the same. The JTabbedPane
s parent container will decide when it needs to update it's contents and perform a new layout operation, resizing the components based on it's needs
您的所有示例均未显示该选项卡式窗格已添加到父容器中...
None of your examples show the tabbed pane been added to a parent container...
下面的示例基本上演示了何时调整组件的大小.
The following example basically demonstrates when a component would be resized.
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
为止,没有任何尺寸
As you can see from the output, until the frame is packed
, nothing is sized
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]
这篇关于JTabbedPane组件何时获得其大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!