2014-06-18 8 views
5

मैं निम्नलिखित जीयूआई बनाने के लिए कोशिश कर रहा हूँ:GridBagLayout काम नहीं कर रहा Gridding

enter image description here

लेकिन जीयूआई है कि मैं कर रहा है:

enter image description here

क्या मेरी ग्रिड लगता है:

छवि: THE GRIDLAYOUT FOR THIS

मुझे समझ में नहीं आ रहा है कि मुझे यह आउटपुट क्यों मिल रहा है क्योंकि मैंने कोड की सहायता के लिए एक आरेख तैयार किया है और ऐसा लगता है कि यह काम करता है।

विधि addComp जोड़ता इनपुट इनपुट में इनपुट घटक को किसी दिए गए (x, y) स्थिति और किसी दिए गए घटक चौड़ाई और ऊंचाई पर जोड़ता है।

कोड:

import javax.swing.*; 
import java.awt.*; 

public class GUIError extends JFrame { 

    //initialise all the components 
    JPanel mainPanel = new JPanel(); 
    JTextField txtDisplay = new JTextField(); 
    JButton btnA = new JButton("A"); 
    JButton btnB = new JButton("B"); 
    JButton btnC = new JButton("C"); 
    JButton btnD = new JButton("D"); 
    JButton btnE = new JButton("E"); 
    JButton btnF = new JButton("F"); 
    JButton btnWA = new JButton("WA"); 
    JButton btnWB = new JButton("WB"); 
    JButton btnWC = new JButton("WC"); 
    JButton btnWD = new JButton("WD"); 

    private void addComp(JPanel panel, JComponent comp, int xPos, int yPos, int compWidth, int compHeight) { 

     GridBagConstraints gridConstraints = new GridBagConstraints(); 

     gridConstraints.gridx = xPos; 
     gridConstraints.gridy = yPos; 
     gridConstraints.gridwidth = compWidth; 
     gridConstraints.gridheight = compHeight; 
     gridConstraints.weightx = 0.5; 
     gridConstraints.weighty = 0.5; 
     gridConstraints.insets = new Insets(5, 5, 5, 5); 
     gridConstraints.anchor = GridBagConstraints.CENTER; 
     gridConstraints.fill = GridBagConstraints.BOTH; 

     panel.add(comp, gridConstraints); 

    } 


    public static void main(String[] args) { 
     try { 
      for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } 
     catch (Exception e) { } 

     SwingUtilities.invokeLater(new Runnable() { 

      public void run() { 

       // create frame 
       JFrame frame = new JFrame("Calculator"); 
       frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       frame.setLocationRelativeTo(null); 
       Container c = frame.getContentPane(); 

       // create GUI within frame    
       new GUIError(c); 

       // finish frame definition 
       frame.pack(); 
       frame.setResizable(false); 
       frame.setVisible(true); 

      } 

     }); 
    } 

    public GUIError(Container cont) { 

     cont.setPreferredSize(new Dimension(610, 250)); 

     // parent panel containes every other panel 
     mainPanel.setLayout(new GridBagLayout()); 

     // text display 
     txtDisplay.setEditable(false); 
     addComp(mainPanel, txtDisplay, 0, 0, 12, 2); // width 16, height 2 


     addComp(mainPanel, btnA, 0, 2, 2, 1); 
     addComp(mainPanel, btnB, 2, 2, 2, 1); 
     addComp(mainPanel, btnC, 4, 2, 2, 1); 
     addComp(mainPanel, btnD, 6, 2, 2, 1); 
     addComp(mainPanel, btnE, 8, 2, 2, 1); 
     addComp(mainPanel, btnF, 10, 2, 2, 1); 
     addComp(mainPanel, btnWA, 0, 3, 3, 1); 
     addComp(mainPanel, btnWB, 3, 3, 3, 1); 
     addComp(mainPanel, btnWC, 6, 3, 3, 1); 
     addComp(mainPanel, btnWD, 9, 3, 3, 1); 

     cont.add(mainPanel); 
    } 
} 
+1

जीबीसी कॉलम आधारित है, प्रश्न के लिए +1 और एसएससीसीई/एमसीवीई, कारण मिगलाउट (ई.आई.) विकसित किए गए हैं – mKorbel

+0

ऐसा नहीं लगता कि आप इसे केवल 1 पैनल के साथ प्राप्त कर सकते हैं। जहां तक ​​मुझे पता है, 'ग्रिडविड्थ' आधा ग्रिड निर्दिष्ट नहीं कर सकता है। मुझे लगता है कि सबसे अच्छा विकल्प या तो एक अलग लेआउट का उपयोग करना होगा, या अपने घटकों को अलग-अलग पैनलों में अलग करना होगा (मुख्य पैनल में शीर्ष पर एक फ़ील्ड और नीचे 2 पैनल (साइड साइड) शामिल हैं। प्रत्येक उप-पैनल में 2 पैनल होते हैं: 3 के लिए एक छोटे बटन, इसके नीचे 2 बड़े बटन वाले) –

+0

मैंने ग्रिडविड्थ का उपयोग करके आधा ग्रिड निर्दिष्ट नहीं किया है। बटन की पहली पंक्ति ग्रिडविड्थ = 2 है और बटन की तीसरी पंक्ति ग्रिडविड्थ = 3. है, मुझे समझ में नहीं आता कि नीचे 3 छोटे और 2 बड़े बटनों का क्या मतलब है? मैंने जिस ग्रिड को भी लिंक किया है, उस ग्रिड को प्रदर्शित करता है जिसे मैंने इस स्थिति में उपयोग किया है। बटन की दूसरी पंक्ति सभी समान आकार हैं, बटन की तीसरी पंक्ति सभी समान आकार हैं। मुझे समझ में नहीं आता कि यह GridBagLayout के साथ क्यों काम नहीं करेगा। – user3749872

उत्तर

5

यह आसानी से GridBagLayout का उपयोग कर के रूप में इस उदाहरण नीचे प्रस्तुत में देखा द्वारा प्राप्त किया जा सकता है। आपको बस ROW प्रत्येक के लिए दो JPanel प्रत्येक को लेने की आवश्यकता है। दोनों का उपयोग कर GridBagLayout, और पहली पंक्ति के घटक जोड़ने weightx = 0.16 प्रत्येक JButton के बाद से साथ इतना क्षेत्र लेने के लिए लगता है, और दूसरी पंक्ति weightx = 0.25 के बाद से के लिए, इस क्षेत्र में हर JButton के कब्जे में है एक्स दिशा

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class GridBagExample { 
    private static final int SIZE = 10; 
    private JButton[] buttons; 
    private GridBagConstraints gbc; 

    public GridBagExample() { 
     buttons = new JButton[SIZE]; 
     gbc = new GridBagConstraints(); 
     gbc.insets = new Insets(5, 5, 5, 5); 
    } 

    private void createAndDisplayGUI() { 
     JFrame frame = new JFrame("Grid Game"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

     JPanel contentPane = new JPanel(); 
     contentPane.setLayout(new GridLayout(2, 1, 5, 5)); 
     contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 

     JPanel topPanel = new JPanel(); 
     JTextField textField = new JTextField(10); 
     //topPanel.add(textField); 

     JPanel buttonPanel = new JPanel(new GridLayout(2, 1, 5, 5)); 
     JPanel headerPanel = new JPanel(new GridBagLayout()); 
     buttons[0] = new JButton("A"); 
     addComp(headerPanel, buttons[0], 0, 0, 0.16, 1.0, 1, 1, GridBagConstraints.BOTH);   
     buttons[1] = new JButton("B"); 
     addComp(headerPanel, buttons[1], 1, 0, 0.16, 1.0, 1, 1, GridBagConstraints.BOTH); 
     buttons[2] = new JButton("C"); 
     addComp(headerPanel, buttons[2], 2, 0, 0.16, 1.0, 1, 1, GridBagConstraints.BOTH); 
     buttons[3] = new JButton("D"); 
     addComp(headerPanel, buttons[3], 3, 0, 0.16, 1.0, 1, 1, GridBagConstraints.BOTH); 
     buttons[4] = new JButton("E"); 
     addComp(headerPanel, buttons[4], 4, 0, 0.16, 1.0, 1, 1, GridBagConstraints.BOTH); 
     buttons[5] = new JButton("F"); 
     addComp(headerPanel, buttons[5], 5, 0, 0.16, 1.0, 1, 1, GridBagConstraints.BOTH); 

     JPanel footerPanel = new JPanel(new GridBagLayout()); 
     buttons[6] = new JButton("WA"); 
     addComp(footerPanel, buttons[6], 0, 0, 0.25, 1.0, 1, 1, GridBagConstraints.BOTH); 
     buttons[7] = new JButton("WB"); 
     addComp(footerPanel, buttons[7], 1, 0, 0.25, 1.0, 1, 1, GridBagConstraints.BOTH); 
     buttons[8] = new JButton("WC"); 
     addComp(footerPanel, buttons[8], 2, 0, 0.25, 1.0, 1, 1, GridBagConstraints.BOTH); 
     buttons[9] = new JButton("WD"); 
     addComp(footerPanel, buttons[9], 3, 0, 0.25, 1.0, 1, 1, GridBagConstraints.BOTH); 
     buttonPanel.add(headerPanel); 
     buttonPanel.add(footerPanel); 

     contentPane.add(textField); 
     contentPane.add(buttonPanel); 

     frame.setContentPane(contentPane); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    private void addComp(JPanel panel, JComponent comp, int x, int y, 
            double wx, double wy, int gw, int gh, int fill) { 
     gbc.gridx = x; 
     gbc.gridy = y; 
     gbc.weightx = wx; 
     gbc.weighty = wy; 
     gbc.gridwidth = gw; 
     gbc.gridheight = gh; 
     gbc.fill = fill; 

     panel.add(comp, gbc); 
    } 

    public static void main(String... args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       new GridBagExample().createAndDisplayGUI(); 
      } 
     }); 
    } 
} 

उत्पादन: GRIDBAGLAYOUT:

gridbagexample

एक ही आउटपुट के रूप में नीचे दिए गए उदाहरण में उद्धृत, GridLayout द्वारा प्राप्त किया जा सकता है, तो GridBagLayout इस मामले में एक आवश्यकता नहीं है:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class GridBagExample { 
    private static final int SIZE = 10; 
    private JButton[] buttons; 

    public GridBagExample() { 
     buttons = new JButton[SIZE]; 
    } 

    private void createAndDisplayGUI() { 
     JFrame frame = new JFrame("Grid Game"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

     JPanel contentPane = new JPanel(); 
     contentPane.setLayout(new GridLayout(2, 1, 5, 5)); 
     contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 

     JPanel topPanel = new JPanel(); 
     JTextArea tArea = new JTextArea(5, 30); 
     JScrollPane scroller = new JScrollPane(); 
     scroller.setViewportView(tArea); 
     topPanel.add(scroller); 

     JPanel buttonPanel = new JPanel(new GridLayout(2, 1, 5, 5)); 
     JPanel headerPanel = new JPanel(new GridLayout(1, 0, 5, 5)); 
     buttons[0] = new JButton("A"); 
     headerPanel.add(buttons[0]); 
     buttons[1] = new JButton("B"); 
     headerPanel.add(buttons[1]); 
     buttons[2] = new JButton("C"); 
     headerPanel.add(buttons[2]); 
     buttons[3] = new JButton("D"); 
     headerPanel.add(buttons[3]); 
     buttons[4] = new JButton("E"); 
     headerPanel.add(buttons[4]); 
     buttons[5] = new JButton("F"); 
     headerPanel.add(buttons[5]); 
     JPanel footerPanel = new JPanel(new GridLayout(1, 0, 5, 5)); 
     buttons[6] = new JButton("WA"); 
     footerPanel.add(buttons[6]); 
     buttons[7] = new JButton("WB"); 
     footerPanel.add(buttons[7]); 
     buttons[8] = new JButton("WC"); 
     footerPanel.add(buttons[8]); 
     buttons[9] = new JButton("WD"); 
     footerPanel.add(buttons[9]); 
     buttonPanel.add(headerPanel); 
     buttonPanel.add(footerPanel); 

     contentPane.add(topPanel); 
     contentPane.add(buttonPanel); 

     frame.setContentPane(contentPane); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String... args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       new GridBagExample().createAndDisplayGUI(); 
      } 
     }); 
    } 
} 

उत्पादन: GRIDLAYOUT: GridBagExample

5

आम तौर पर मैं अलग पैनल बनाउंगा, लेकिन हे, जो चुनौती पसंद नहीं करते?

समस्या यह है कि कुछ कॉलम जीबीएल के लिए अच्छी तरह से परिभाषित नहीं हैं (इसकी चौड़ाई को परिभाषित करने वाला कोई भी घटक नहीं है), इसलिए बटन डब्ल्यूए को पता नहीं है कि आधा बटन बी, आदि को कवर करने के लिए कितना दूर जाना है।

तो, मैंने आवश्यक वर्गों को बनाने वाले कॉलम में कुछ शून्य-चौड़ाई वाले स्पैसर जोड़े।

सभी एक पैनल में:

enter image description here

import java.awt.*; 
import javax.swing.*; 

public class GridBagDemo3 implements Runnable 
{ 
    private JPanel panel; 

    public static void main(String[] args) 
    { 
    SwingUtilities.invokeLater(new GridBagDemo3()); 
    } 

    public void run() 
    { 
    JTextArea text = new JTextArea(10,10); 
    JButton btnA = new JButton("A"); 
    JButton btnB = new JButton("B"); 
    JButton btnC = new JButton("C"); 
    JButton btnD = new JButton("D"); 
    JButton btnE = new JButton("E"); 
    JButton btnF = new JButton("F"); 
    JButton btnWA = new JButton("WA"); 
    JButton btnWB = new JButton("WB"); 
    JButton btnWC = new JButton("WC"); 
    JButton btnWD = new JButton("WD"); 

    panel = new JPanel(new GridBagLayout()); 

    add(text, 0,0, 12,1); 
    add(btnA, 0,1, 2,1); 
    add(btnB, 2,1, 2,1); 
    add(btnC, 4,1, 2,1); 
    add(btnD, 6,1, 2,1); 
    add(btnE, 8,1, 2,1); 
    add(btnF, 10,1, 2,1); 
    add(btnWA, 0,2, 3,1); 
    add(btnWB, 3,2, 3,1); 
    add(btnWC, 6,2, 3,1); 
    add(btnWD, 9,2, 3,1); 

    // SPACERS: define the 2 columns that button B spans 
    //   so that WA and WB can split B, and same for button E 
    add(null, 2,2, 1,1); 
    add(null, 3,2, 1,1); 
    add(null, 8,2, 1,1); 
    add(null, 9,2, 1,1); 

    JFrame frame = new JFrame("Grig Bag"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().add(panel); 
    frame.pack(); 
    frame.setVisible(true); 
    } 

    private void add(Component comp, int x, int y, int colspan, int rowspan) 
    { 
    GridBagConstraints gbc = new GridBagConstraints(); 
    gbc.gridx = x; 
    gbc.gridy = y; 
    gbc.gridwidth = colspan; 
    gbc.gridheight = rowspan; 
    gbc.weighty = .1; 
    gbc.anchor = GridBagConstraints.CENTER; 
    gbc.fill = GridBagConstraints.BOTH; 
    gbc.insets = new Insets(5,5,5,5); 

    if (comp != null) 
    { 
     gbc.weightx = 1; 
    } 
    else 
    { 
     comp = Box.createHorizontalGlue(); 
     gbc.fill = GridBagConstraints.NONE; 
     gbc.weightx = 0.1; 
     gbc.weighty = 0; 
    } 

    panel.add(comp, gbc); 
    } 
} 
3

अगर आप अभी भी अपने त्रुटि को समझने के लिए रुचि रखते हैं: ग्रिड 'लेआउट FlowLayout नहीं है, तो आप बस घटक नहीं जोड़ सकते हैं और यह वाला ऑब्जेक्ट खींचें उसी पल प्रोग्राम इसे पढ़ता है। ग्रिड के मामले में, वर्चुअल मशीन पहले सभी ऑब्जेक्ट्स को पढ़ती है और ग्रिड बनाने के बजाय, कॉलम की मात्रा उन पंक्तियों के आधार पर निर्धारित की जाती है जिनमें कॉलम की सबसे बड़ी संख्या होती है। तो, 12 कॉलम को प्राथमिकता देने के लिए आपको अच्छा विचार था (इस प्रकार आप नए "पैनल" और अतिरिक्त सिस्टम संसाधनों के निर्माण से बचें), लेकिन इसे सही करने के लिए आपको बस खाली रिक्त स्थान (केवल खाली नहीं बल्कि शून्य आकार के साथ) जोड़ना चाहिए था पंक्तियों में से एक

// text display 
    txtDisplay.setEditable(false); 
    addComp(mainPanel, txtDisplay, 0, 0, 12, 2); // width 16, height 2 



    //now go with empty spaces 
    for(int i=0;i<12;i++){ 
     GridBagConstraints gridConstraints = new GridBagConstraints(); 
     gridConstraints.gridx = i; //12 times "i" 
     gridConstraints.gridy = 1; //you left y=1 empty, so why don't use it 
     gridConstraints.gridwidth = 1; //one column = one space in grid 
     gridConstraints.gridheight = 0; //we don't want it to occupied any space 
     gridConstraints.weightx = 0.5; 
     gridConstraints.weighty = 0.5; 
     gridConstraints.insets = new Insets(0, 0, 0, 0); //same reason, we don't want it to occupied any space 
     gridConstraints.anchor = GridBagConstraints.CENTER; 
     gridConstraints.fill = GridBagConstraints.BOTH ; 

     mainPanel.add(Box.createHorizontalGlue(), gridConstraints); 
    } 


    addComp(mainPanel, btnA, 0, 2, 2, 1); 
    addComp(mainPanel, btnB, 2, 2, 2, 1); 
    addComp(mainPanel, btnC, 4, 2, 2, 1); 
    addComp(mainPanel, btnD, 6, 2, 2, 1); 
    addComp(mainPanel, btnE, 8, 2, 2, 1); 
    addComp(mainPanel, btnF, 10, 2, 2, 1); 
    addComp(mainPanel, btnWA, 0, 3, 3, 1); 
    addComp(mainPanel, btnWB, 3, 3, 3, 1); 
    addComp(mainPanel, btnWC, 6, 3, 3, 1); 
    addComp(mainPanel, btnWD, 9, 3, 3, 1); 
4

@Matthieu लिखा था -

could you elaborate on GBC are column based? It sounds like an explanation of a problem I noticed...

मुझे लगता है कि इस तरह से GridBagLayout बनाया गया है, मैं हमेशा JLabels (सीमाएं मैट्रिक्स के रूप में :-) बिना उपयोग कर रहा हूँ ..

enter image description here

import java.awt.*; 
import javax.swing.*; 

public class CopyTextNorthPanel extends JPanel { 

    private static final long serialVersionUID = 1L; 
    private JLabel hidelLabel; 
    private JLabel firstLabel; 
    private JTextField firstText; 

    public CopyTextNorthPanel() { 

     setLayout(new GridBagLayout()); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); 
     for (int k = 0; k < 50; k++) { 
      hidelLabel = new JLabel("  "); 
      hidelLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); 
      gbc.fill = GridBagConstraints.HORIZONTAL; 
      gbc.weightx = 0.5; 
      gbc.weighty = 0.5; 
      gbc.gridx = k; 
      gbc.gridy = 0; 
      add(hidelLabel, gbc); 
     } 
     for (int k = 0; k < 5; k++) { 
      firstLabel = new JLabel("Testing Label : ", SwingConstants.RIGHT); 
      firstLabel.setFont(new Font("Serif", Font.BOLD, 20)); 
      firstLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); 
      gbc.fill = GridBagConstraints.HORIZONTAL; 
      gbc.insets = new Insets(0, 0, 5, 0); 
      gbc.gridx = 0; 
      gbc.gridwidth = 8; 
      gbc.gridy = k + 1; 
      add(firstLabel, gbc); 
     } 
     for (int k = 0; k < 5; k++) { 
      firstText = new JTextField("Testing TextField"); 
      firstText.setFont(new Font("Serif", Font.BOLD, 20)); 
      firstText.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); 
      gbc.fill = GridBagConstraints.HORIZONTAL; 
      gbc.insets = new Insets(0, 0, 5, 0); 
      gbc.gridx = 9; 
      gbc.gridwidth = k + 8; 
      gbc.gridy = k + 1; 
      add(firstText, gbc); 
     } 
     for (int k = 0; k < 5; k++) { 
      firstLabel = new JLabel("Testing Label : ", SwingConstants.RIGHT); 
      firstLabel.setFont(new Font("Serif", Font.BOLD, 20)); 
      firstLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); 
      gbc.fill = GridBagConstraints.HORIZONTAL; 
      gbc.insets = new Insets(0, 0, 5, 0); 
      gbc.gridx = 20 + k; 
      gbc.gridwidth = 8; 
      gbc.gridy = k + 1; 
      add(firstLabel, gbc); 
     } 
     for (int k = 0; k < 5; k++) { 
      firstText = new JTextField("Testing TextField"); 
      firstText.setFont(new Font("Serif", Font.BOLD, 20)); 
      firstText.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); 
      gbc.fill = GridBagConstraints.HORIZONTAL; 
      gbc.insets = new Insets(0, 0, 5, 0); 
      gbc.gridx = 29 + k; 
      gbc.gridwidth = 21 - k; 
      gbc.gridy = k + 1; 
      add(firstText, gbc); 
     } 
    } 
} 
संबंधित मुद्दे