6
लीडरिंग ग्रिडबैगआउट, यहां मुद्दा यह है कि नाम लेबल और कंघॉक्स पैनल के शीर्ष पर दिखाई नहीं देता है, लेकिन मैंने अपना एंकर नॉर्थ पर सेट किया है। क्यूं कर ?जावा ग्रिडबैगलाइट एंकर
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class Test2 {
public Test2() {
JFrame frame = new JFrame();
frame.setTitle("test");
frame.getContentPane().setLayout(new GridLayout(1,2));
frame.setSize(800, 600);
JPanel panel1 = new JPanel();
panel1.setLayout(new GridBagLayout());
JLabel label = new JLabel("name");
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.NORTH;
gridBagConstraints.weightx = 0.0;
gridBagConstraints.weighty = 0.0;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
panel1.add(label, gridBagConstraints);
String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
JComboBox petList = new JComboBox(petStrings);
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.NORTH;
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 0.0;
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
panel1.add(petList, gridBagConstraints);
frame.getContentPane().add(panel1);
frame.getContentPane().add(new JPanel());
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Test2();
}
}
हाँ, तुम ठीक कह रहे:
weighty
बदलने के बाद परिणाम के बाद है। क्या आपका मतलब है, जब एंकर नॉर्थ पर सेट होता है, तो भारोत्तोलन 1.0 होना चाहिए? – user595234ठीक है, यदि आप घटक को कुछ "क्षेत्र" (घटक से बड़ा) देने के लिए चाहते हैं, तो आपको एक गैर-शून्य वजन की आवश्यकता है। (यानी, वजन 0.1 इस विशेष मामले में भी काम करेगा।) – aioobe
मैं हमेशा डबल मान को भ्रमित करता हूं, 1.0 और 0.5 के बीच क्या अंतर है? – user595234