ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-10-31 12:01:24  阅读:214  来源: 互联网

标签:jradiobutton gridbaglayout swing jpanel java


请看下面的代码

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有