java - GridBagLayout 中的单选按钮为 "Hiding"

标签 java swing jpanel gridbaglayout jradiobutton

请看下面的代码

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 按钮“女性”隐藏了它的一部分,一旦您移动光标,它就会完全显示出来。我猜这是因为它在“插图”中使用了负间距。

但是我这样做是为了减少 2 个单选按钮之间的间隙。男性在gridx = 2,女性在gridx = 3,这是按钮之间的巨大空间。所以我在插图中使用减号空间来减少空间,不幸的是,它是这样来的。

我尝试将 JLabel、maleRadio 和 femaleRadio 添加到具有 flowlayout 的单独 JPanel 中,并将其放入 gbc.gridx = 2; gbc.gridy = 3;.它通过将 gridy = 3 中的所有单元格匹配到新 JPanel 的宽度而使一切变得更糟。

请帮我缩小这两个 JRadio 按钮之间的间隙,不要有任何问题。谢谢。

最佳答案

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

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

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

问题出在这里:

    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);

这会给我们:

enter image description here

更新:

同样重要的是要注意 GridBagContsraints,例如 gridx 等从 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();
            }
        });
    }
}

关于java - GridBagLayout 中的单选按钮为 "Hiding",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14183743/

相关文章:

java - 如何将 MySQL 持久性管理器与 Jackrabbit 独立使用

java - 为什么 'this'可以作为Java中这里的参数?

java - 在 JPanel 中调整 JTextPane 的大小

java - 在自定义 JPanel 和 JTable 布局之间进行选择

java - 不小心删除了 h2 设置。如何找回它们?

java - map 信息 + Java : A way to decrypt coordinates in a DAT file?

java - 不一致的 Java Swing JFrame 大小

java - 为什么我的背景在重绘后会出现故障?

java - JPanel、JFrame、BoxLayout 问题

java - 如何将 jlabel 文本在 jpanel 内向左对齐