编程语言
首页 > 编程语言> > java-JRadio按钮在GridBagLayout中“隐藏”

java-JRadio按钮在GridBagLayout中“隐藏”

作者:互联网

请看下面的代码

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;


    }
}

如您所见,JRadio按钮“ female”隐藏了它的一部分,一旦您移动了光标,它就会完全显示出来.我想这是因为它在“插入”中使用了负间距.

但是我这样做是为了减小两个单选按钮之间的间距.男性在gridx = 2中,而女性在gridx = 3中,这是按钮之间的巨大空间.因此,我在插图中使用了减号来减小空间,不幸的是,它是这样产生的.

我尝试将JLabel,maleRadio和femaleRadio添加到具有流布局的单独JPanel中,然后将其放入gbc.gridx = 2; gbc.gridy = 3;.通过将gridy = 3中的所有单元格匹配到新JPanel的宽度,使一切变得最糟.

请帮助我减少这两个JRadio按钮之间的距离,而不会出现任何问题.谢谢.

解决方法:

>不要扩展JFrame而是创建一个实例并使用它.
>另外,我看到您将单选按钮添加到面板中,但是您没有添加面板,而是将单选按钮重新添加到中面板?选择一种方法输掉另一种方法(尽管我认为这可能是在试图解决问题时发生的?)
>最重要的是SSCCE是可编译的(通过正确的语法,没有编译错误)和可运行的(通过主方法,没有运行时异常(除非是问题: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);

这将给我们:

更新:

同样重要的是要注意GridBagContsraints(例如gridx等)从0开始而不是1.

 @Gagandeeps balis 1对重复使用已设置的值和在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();
            }
        });
    }
}

标签:jradiobutton,gridbaglayout,swing,jpanel,java
来源: https://codeday.me/bug/20191031/1975683.html