जब मैं अपने घटक को संरेखित करने का प्रयास करता हूं तो यह या तो बाएं तरफ या दाएं तरफ जाता है।ग्रिडबैगलाउट का उपयोग कर जेपीनल में घटक केंद्र को कैसे संरेखित करें?
तो मैं सिर्फ इस समस्या से छुटकारा पाने के लिए समाधान चाहता हूं, और यह भी बताएं कि पैनल का आकार 400 x 350 पिक्सेल के रूप में कैसे सेट करें।
यहाँ मेरी कोड है .... titleLabel
और ResultLabel
केंद्र
public TimeGui() {
layout = new GridBagLayout();
setSize(400, 350); //**Its not working**
setBackground(Color.LIGHT_GRAY);
setBorder(BorderFactory.createLineBorder(Color.BLACK));
setBorder(new TitledBorder(new EtchedBorder(), "Time Conversion"));
setLayout(layout);
layoutConstraints = new GridBagConstraints();
textField1 = new JTextField(10);
textField2 = new JTextField(10);
String[] names1 = {"Seconds", "Minutes", "Hours", "Days", "Weeks"};
comboBox1 = new JComboBox<>(names1);
comboBox2 = new JComboBox<>(names1);
titleLabel = new JLabel("Time Conversion Unit", JLabel.CENTER);
resultLabel = new JLabel("Result Label");
equalLabel = new JLabel("=");
convertButton = new JButton("Convert");
layoutConstraints.fill = GridBagConstraints.HORIZONTAL;
Insets inset = new Insets(10, 10, 10, 10);
layoutConstraints.anchor = GridBagConstraints.CENTER;
addComponent(titleLabel, 0, 0, 2, 2, inset); // I tried (0,1,2,2)
addComponent(comboBox1, 3, 0, 2, 3, inset);
addComponent(comboBox2, 3, 2, 2, 3, inset);
addComponent(textField1, 6, 0, 1, 2, inset);
addComponent(equalLabel, 6, 1, 1, 2, inset);
addComponent(textField2, 6, 2, 1, 2, inset);
addComponent(resultLabel, 8, 1, 2, 1, inset);
addComponent(convertButton, 10, 0, 2, 2, inset);
}
private void addComponent(Component component, int row,
int column, int width, int height, Insets inset1) {
layoutConstraints.gridx = column;
layoutConstraints.gridy = row;
layoutConstraints.gridwidth = width;
layoutConstraints.gridheight = height;
layoutConstraints.insets = inset1;
layout.setConstraints(component, layoutConstraints);
add(component);
}
}
छवि की अनुपस्थिति में, यह बुद्धिमान होगा, अगर कम से कम आप एक [___SSCCE___] (http://sscce.org/) प्रदान कर सकते हैं, ताकि कोई इसे बिना किसी प्रकार के किसी भी प्रकार के चला सके। बस अपनी छवि को किसी भी साइट पर अपलोड करें, जैसे [** imgur **] (http://imgur.com/), [** ड्रॉपबॉक्स **] (https://www.dropbox.com/home) या [* * 4shared **] (http://www.4shared.com/)। उच्च विशेषाधिकार वाला कोई भी व्यक्ति आपके प्रश्न –
में जोड़ देगा ठीक है बस मुझे बताएं कि पैनल का आकार कैसे सेट करें। – user1874936