आप LayoutManager ही एक भी घटक (दोनों अक्ष या सिर्फ एक) केंद्र के लिए बना सकते हैं। यह वह है जो धुरी दोनों पर करता है, आप आसानी से इसे लंबवत या क्षैतिज केंद्रित करने के लिए बदल सकते हैं।
वर्तमान कार्यान्वयन लेआउट पहला दृश्य बच्चे, आप बदल सकते हैं और वह भी ...
public class CentreLayout implements LayoutManager, java.io.Serializable {
public void addLayoutComponent(String name, Component comp) {
}
public void removeLayoutComponent(Component comp) {
}
public Dimension preferredLayoutSize(Container target) {
return target.getPreferredSize();
}
public Dimension minimumLayoutSize(Container target) {
return target.getMinimumSize();
}
public void layoutContainer(Container target) {
synchronized (target.getTreeLock()) {
Insets insets = target.getInsets();
Dimension size = target.getSize();
int w = size.width - (insets.left + insets.right);
int h = size.height - (insets.top + insets.bottom);
int count = target.getComponentCount();
for (int i = 0; i < count; i++) {
Component m = target.getComponent(i);
if (m.isVisible()) {
Dimension d = m.getPreferredSize();
m.setBounds((w - d.width)/2, (h - d.height)/2, d.width, d.height);
break;
}
}
}
}
}
स्रोत
2009-05-23 02:51:42
मैं हमेशा सोचा है कि इसे कैसे किया, अंत में मैं छोड़ दिया और बजाय MigLayout इस्तेमाल किया। http://madbean.com/anim/totallygridbag/ :) – gencoreoperative
यह मैं क्या gridbag के बारे में सोच रहा है। – qben
GridBagLayout मुश्किल में विफल रहता है जब कंटेनर पसंदीदा आकार से छोटा (लेकिन न्यूनतम आकार से बड़ा) बच्चे की है: –