简体   繁体   中英

How to adjust size of JPanel in GridBagLayout?

I am sure this question could be a duplicate, but still I am posting since I am not able to find a solution / correct the error in my code. A part of my Java GUI uses GridBagLayout. This layout will have 3 components, 2 radio buttons will be on the top (placed side by side) and the rest space should have a JPanel (starting from the next row below the radio buttons, till the end of the space available). I looked at different examples, in the forum as well as outside, but cannot solve the issue.

With the following code, my GUI section looks something like this:

在此处输入图片说明

ImageDisplay = new JPanel(new GridBagLayout());
        GridBagConstraints g = new GridBagConstraints();
        g.insets = new Insets(5, 5, 5, 5); // insets for all components


        rawImage = new JRadioButton("Raw", true);
        peakPickedImage = new JRadioButton("Peak picked");
        radioButtonGroup.add(rawImage);
        radioButtonGroup.add(peakPickedImage);

        g.fill = GridBagConstraints.HORIZONTAL;
        g.gridx = 0;
        g.gridy = 0;
        g.gridwidth = 1;
        g.gridheight = 1;

        ImageDisplay.add(rawImage, g);

        g.gridx = 1;
        g.gridy = 0;
        g.gridwidth = 1;
        g.gridheight = 1;
        g.weightx = 0;
        g.weighty = 0;

        ImageDisplay.add(peakPickedImage, g);

        JPanel imagePanel = new JPanel();
        g.gridx = 0;
     //   g.gridy = 0;
        g.weightx = 1.0;
        g.weighty = 0.75;
        g.gridwidth = 3;
        g.gridheight = 3;
       // g.fill = GridBagConstraints.BOTH;

      //  g.fill = GridBagConstraints.SOUTH;

        imagePanel.setBorder(BorderFactory.createEtchedBorder());

        ImageDisplay.add(imagePanel, g);

and, on uncommenting the

g.fill = GridBagConstraints.BOTH;

I get the JPanel, which includes both the radio buttons within it. How to solve this issue?

Try it like this :

        JPanel ImageDisplay = new JPanel(new GridBagLayout());
    GridBagConstraints g = new GridBagConstraints();
    g.insets = new Insets(5, 5, 5, 5); // insets for all components
    g.weightx = 0.0;
    g.weighty = 0.0;

    JRadioButton rawImage = new JRadioButton("Raw", true);
    JRadioButton peakPickedImage = new JRadioButton("Peak picked");
    ButtonGroup radioButtonGroup = new ButtonGroup();
    radioButtonGroup.add(rawImage);
    radioButtonGroup.add(peakPickedImage);

    g.fill = GridBagConstraints.HORIZONTAL;
    g.gridx = 0;
    g.gridy = 0;
    g.gridwidth = 1;
    g.gridheight = 1;

    ImageDisplay.add(rawImage, g);

    g.gridx = 1;
    g.gridy = 0;

    ImageDisplay.add(peakPickedImage, g);

    JPanel imagePanel = new JPanel();
    g.gridx = 0;
    g.gridy = 1;
    g.gridwidth = 2;
    g.weightx = 1.0; // fill the rest of the space
    g.weighty = 1.0;
    g.fill = GridBagConstraints.BOTH;

    imagePanel.setBorder(BorderFactory.createEtchedBorder());

    ImageDisplay.add(imagePanel, g);

Best regards, Zoran

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM