3

Please have a look at the following code

package normal;

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

public class Form extends JFrame
{
    private JLabel heightLabel, weightLabel, waistLabel, neckLabel, hipsLabel,genderLabel,valuesLabel,bfPercentageLabel;
    private JLabel logoLabel; 

    private ImageIcon logo;

    private JTextField heightTxt, weightTxt, waistTxt, neckTxt, hipsTxt;

    private JRadioButton maleRadio, femaleRadio, inchesRadio, cmRadio;
    private ButtonGroup genderGroup, valuesGroup;

    private JComboBox percentageCombo;

    private JPanel centerPanel, northPanel, southPanel;


    public Form()
    {
        //Declaring instance variables  
        heightLabel = new JLabel("Height: ");
        weightLabel = new JLabel("Weight: ");
        waistLabel = new JLabel("Waist: ");
        neckLabel = new JLabel("Neck: ");
        hipsLabel = new JLabel("Hips: ");
        genderLabel = new JLabel("Gender: ");
        valuesLabel = new JLabel("Values in: ");


        logoLabel = new JLabel();
        logo = new ImageIcon(getClass().getResource("/images/calc_logo_final_2_edit.gif"));
        logoLabel.setIcon(logo);



        heightTxt = new JTextField(10);
        weightTxt = new JTextField(10);
        waistTxt = new JTextField(10);
        neckTxt = new JTextField(10);
        hipsTxt = new JTextField(10);

        maleRadio = new JRadioButton("Male");
        femaleRadio = new JRadioButton("Female");
        genderGroup = new ButtonGroup();
        genderGroup.add(maleRadio);
        genderGroup.add(femaleRadio);

        inchesRadio = new JRadioButton("Inches");
        cmRadio = new JRadioButton("Centimeters");
        valuesGroup = new ButtonGroup();
        valuesGroup.add(inchesRadio);
        valuesGroup.add(cmRadio);

        percentageCombo = new JComboBox();
        percentageCombo.addItem("No Value is Set");

        this.add(createNorthPanel(),"North");
        this.add(createCenterPanel(),"Center");
        this.setResizable(false);
        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }

    private JPanel createNorthPanel()
    {
        northPanel = new JPanel();
        northPanel.setLayout(new FlowLayout());

        northPanel.add(logoLabel);

        return northPanel;
    }

    private JPanel createCenterPanel()
    {
        centerPanel = new JPanel();

        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();

        centerPanel.setLayout(gbl);

        //creating a jpanel for gender radio buttons
        JPanel genderPanel = new JPanel();
        genderPanel.setLayout(new FlowLayout());
        genderPanel.add(genderLabel);
        genderPanel.add(maleRadio);
        genderPanel.add(femaleRadio);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        centerPanel.add(heightLabel,gbc);

        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        centerPanel.add(heightTxt,gbc);

        gbc.gridx = 3;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        centerPanel.add(weightLabel,gbc);

        gbc.gridx = 4;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        centerPanel.add(weightTxt,gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        centerPanel.add(waistLabel,gbc);

        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        centerPanel.add(waistTxt,gbc);

        gbc.gridx = 3;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        centerPanel.add(neckLabel,gbc);

        gbc.gridx = 4;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        centerPanel.add(neckTxt,gbc);

        gbc.gridx = 5;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        centerPanel.add(hipsLabel,gbc);

        gbc.gridx = 6;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        centerPanel.add(hipsTxt,gbc);

        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        centerPanel.add(genderLabel,gbc);

        gbc.gridx = 2;
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        centerPanel.add(maleRadio,gbc);

        gbc.gridx = 3;
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,-10,0,0);
        centerPanel.add(femaleRadio,gbc);

        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(50,5,0,0);
        centerPanel.add(valuesLabel,gbc);



        return centerPanel;


    }
}

As you can see, the JRadio button "female" is hiding a part of it, and once you move your cursor, it shows up completely. I guess this is happening because it is using minus spacing in "insets".

However I did it like that to reduce the gap between 2 radio buttons. Male is in gridx = 2, and female is in gridx = 3, which is a massive space between the buttons.So I used minus space in insets to reduce the space, unfortunately it came like this.

I tried adding the JLabel, maleRadio and femaleRadio into a seperate JPanel which is having flowlayout, and put it into gbc.gridx = 2; gbc.gridy = 3;. It made everything worst by matching all the cells in gridy = 3 into the width of new JPanel.

Please help me to reduce the gap between these 2 JRadio buttons, without having any issue. Thank you.

4

1 回答 1

4
  • 不要扩展JFrame而是创建一个实例并使用它。

  • 我还看到您将单选按钮添加到面板,但您没有添加面板,而是将单选按钮重新添加到centerpanel?选择一种方式失去另一种方式(尽管我认为这可能是在试图解决问题时发生的?)

  • 最重要的SSCCE是可编译的(通过正确的语法并且没有编译错误)和可运行的(通过main 方法并且没有运行时异常(除非这是问题:P)-就像您阅读图像一样-请找到一种包含资源的方法,即链接到带有徽标的 URL,或者使方法返回与徽标大小相同的简单图像,或者干脆将其省略)。

问题在这里:

    gbc.gridx = 3;
    gbc.gridy = 3;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.insets = new Insets(15, -10, 0, 0);
    centerPanel.add(femaleRadio, gbc);

-10 绝对不应该存在(可能是拼写错误?),因为这会导致它重叠,或者在这种情况下重叠另一个组件,而不是使用大于或等于 0 的任何值:

    gbc.gridx = 3;
    gbc.gridy = 3;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.insets = new Insets(15, 10, 0, 0);
    centerPanel.add(femaleRadio, gbc);

这会给我们:

在此处输入图像描述

更新:

同样重要的是要注意GridBagContsraintsgridxetc 从 0 而不是 1 开始。

+1 对@Gagandeeps balis 评论重用已设置且在 中相同的值GridBagConstraints,这是您的代码,其中包含所有讨论的修复:

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

class Form {

    private JLabel heightLabel, weightLabel, waistLabel, neckLabel, hipsLabel, genderLabel, valuesLabel, bfPercentageLabel;
    private JLabel logoLabel;
    private ImageIcon logo;
    private JTextField heightTxt, weightTxt, waistTxt, neckTxt, hipsTxt;
    private JRadioButton maleRadio, femaleRadio, inchesRadio, cmRadio;
    private ButtonGroup genderGroup, valuesGroup;
    private JComboBox percentageCombo;
    private JPanel centerPanel, northPanel, southPanel;

    public Form() {
        //Declaring instance variables  
        heightLabel = new JLabel("Height: ");
        weightLabel = new JLabel("Weight: ");
        waistLabel = new JLabel("Waist: ");
        neckLabel = new JLabel("Neck: ");
        hipsLabel = new JLabel("Hips: ");
        genderLabel = new JLabel("Gender: ");
        valuesLabel = new JLabel("Values in: ");

        logoLabel = new JLabel();
        //logo = new ImageIcon(getClass().getResource("/images/calc_logo_final_2_edit.gif"));
        //logoLabel.setIcon(logo);

        heightTxt = new JTextField(10);
        weightTxt = new JTextField(10);
        waistTxt = new JTextField(10);
        neckTxt = new JTextField(10);
        hipsTxt = new JTextField(10);

        maleRadio = new JRadioButton("Male");
        femaleRadio = new JRadioButton("Female");
        genderGroup = new ButtonGroup();
        genderGroup.add(maleRadio);
        genderGroup.add(femaleRadio);

        inchesRadio = new JRadioButton("Inches");
        cmRadio = new JRadioButton("Centimeters");
        valuesGroup = new ButtonGroup();
        valuesGroup.add(inchesRadio);
        valuesGroup.add(cmRadio);

        percentageCombo = new JComboBox();
        percentageCombo.addItem("No Value is Set");

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createNorthPanel(), "North");
        frame.add(createCenterPanel(), "Center");
        frame.setResizable(false);
        frame.pack();
        frame.setVisible(true);


    }

    private JPanel createNorthPanel() {
        northPanel = new JPanel();
        northPanel.setLayout(new FlowLayout());
        northPanel.add(logoLabel);

        return northPanel;
    }

    private JPanel createCenterPanel() {
        centerPanel = new JPanel(new GridBagLayout());

        GridBagLayout gbl = new GridBagLayout();
        centerPanel.setLayout(gbl);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15, 5, 0, 0);

        gbc.gridx = 0;
        gbc.gridy = 0;
        centerPanel.add(heightLabel, gbc);
        gbc.gridx = 1;
        centerPanel.add(heightTxt, gbc);
        gbc.gridx = 2;
        centerPanel.add(weightLabel, gbc);
        gbc.gridx = 3;
        centerPanel.add(weightTxt, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        centerPanel.add(waistLabel, gbc);
        gbc.gridx = 1;
        centerPanel.add(waistTxt, gbc);
        gbc.gridx = 2;
        centerPanel.add(neckLabel, gbc);
        gbc.gridx = 3;
        centerPanel.add(neckTxt, gbc);
        gbc.gridx = 4;
        centerPanel.add(hipsLabel, gbc);
        gbc.gridx = 5;
        centerPanel.add(hipsTxt, gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        centerPanel.add(genderLabel, gbc);
        gbc.gridx = 1;
        centerPanel.add(maleRadio, gbc);
        gbc.gridx = 2;
        centerPanel.add(femaleRadio, gbc);

        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.insets = new Insets(50, 5, 0, 0);
        centerPanel.add(valuesLabel, gbc);

        return centerPanel;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Form();
            }
        });
    }
}
于 2013-01-06T16:39:03.177 回答