按下toggleButton时,JButton会改变大小

时间:2015-06-16 23:32:52

标签: java swing user-interface resize calculator

我的GUI存在问题,当我按下属于某个面板的切换按钮时,面板按钮会改变尺寸并标记更改位置。我附上了一些截图,以显示操作中的错误。我相信它是因为按钮共享相同的面板,但我不确定它是否可以固定,以便按钮大小保持静态,标签位置也是如此。

我的代码在下面(抱歉它非常混乱!我有很多嵌套的BorderLayout JPanels,这可能是问题所在,但对我来说这是获得正确布局的最简单方法。

package v2;

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; 
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;

public class BmrCalcv2 extends JFrame {

    // Frames and main panels
    static JFrame mainFrame;
    static JPanel mainPanel;
    static JPanel combinedGAHWpanel; // combines genderPanel, agePanel, heightPanel, weightPanel - this
                                     // is a BorderLayout, whereas
                                     // gender/agePanel are FlowLayouts.
    static JPanel weightHeightPanel; // Combines weight and height panel into out flowlayout panel, which is then combined into the above borderlayout panel

    // Image components
    static JPanel imgPanel;
    private JLabel imgLabel;

    // Menu-bar components
    static JMenuBar menuBar;
    static JMenu saveMenu, optionMenu, helpMenu;

    // Age components
    static JPanel agePanel;
    private JLabel ageLabel;
    private JLabel yearsLabel;
    private JTextField ageTextField;

    // Gender components
    static JPanel genderPanel;
    private JLabel genderLabel;
    private JRadioButton genderMale;
    private JRadioButton genderFemale;


    // Height components
    static JPanel heightPanel;
    private JLabel heightLabel;
    private JTextField heightCMField;
    private JLabel heightFTLabel;
    private JLabel heightINCHLabel;
    private JTextField heightFTField;
    private JTextField heightINCHField;
    private JToggleButton cmButton;
    private JToggleButton feetButton;


    // Weight components
    static JPanel weightPanel;
    private JLabel weightLabel;
    private JTextField weightField;
    private JToggleButton kgButton;
    private JToggleButton lbButton;


    // TDEE and BMR Components
    static JPanel tdeePanel;
    static JPanel tdeeBMRPanel;
    static JPanel activityLevelPanel;
    static JPanel bmrTDEEValuesPanel;
    static JPanel bmrValuePanel;
    static JPanel tdeeValuePanel;
    private JLabel tdeeQuestionLabel;
    private JLabel activityLevelLabel;
    private JComboBox activityLevelBox;
    private JRadioButton tdeeYes;
    private JRadioButton tdeeNo;
    private JLabel bmrLabel;
    private JLabel tdeeLabel;




    // Default values for gender/weight/height

    String[] activityLevels = {"Sedentary", "Lightly Active", "Moderately Active", "Very Active", "Extra Active"};
    String genderSelection = "M";   
    String weightSelection = "kg";
    String heightSelection = "cm";
    String tdeeSelection = "no";




    public BmrCalcv2(String title) {

        // Main JFrame
        setTitle("BMR/TDEE Calculator");
        mainPanel = new JPanel();

        // All JPanel declarations
        menuBar = new JMenuBar();
        imgPanel = new JPanel();
        agePanel = new JPanel();
        genderPanel = new JPanel();
        heightPanel = new JPanel();
        weightPanel = new JPanel();
        weightHeightPanel = new JPanel(new BorderLayout());
        combinedGAHWpanel = new JPanel(new BorderLayout()); // Create a new panel used to combine
                                                            // genderPanel, agePanel, weightPanel, heightPanel below
        tdeeBMRPanel = new JPanel(new BorderLayout());
        tdeePanel = new JPanel();
        activityLevelPanel = new JPanel();
        bmrTDEEValuesPanel = new JPanel(new BorderLayout());
        bmrValuePanel = new JPanel();
        tdeeValuePanel = new JPanel();



        // Image panel declaration
        imgLabel = new JLabel(new ImageIcon("filesrc//mainlogo.png"));
        imgPanel.add(imgLabel);

        // JPanel layout managers
        agePanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
        genderPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));


        // Menu JComponents
        saveMenu = new JMenu("Save");
        optionMenu = new JMenu("Options");
        helpMenu = new JMenu("Help");
        menuBar.add(saveMenu);
        menuBar.add(optionMenu);
        menuBar.add(helpMenu);

        // Age JComponents
        ageLabel = new JLabel("Age:");
        yearsLabel = new JLabel("<html><i>years</i><html>");
        ageTextField = new JTextField(5);
        agePanel.add(ageLabel);
        agePanel.add(ageTextField);
        agePanel.add(yearsLabel);

        // Gender JComponents
        genderLabel = new JLabel("Gender:");
        genderMale = new JRadioButton("Male", true);
        genderFemale = new JRadioButton("Female");



        genderPanel.add(genderLabel);
        genderPanel.add(genderMale);
        genderPanel.add(genderFemale);


        ButtonGroup genderGroup = new ButtonGroup(); // groups male and female radio buttons together so that only one can be selected
        genderGroup.add(genderMale);
        genderGroup.add(genderFemale);


        genderMale.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
              String genderSelection = "M";   
            } 
        });

        genderFemale.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
              String genderSelection = "F";
            } 
        });

        // Height JComponents
        heightLabel = new JLabel("Height:");
        heightCMField = new JTextField(4);
        heightFTField = new JTextField(3);
        heightFTLabel = new JLabel("ft");
        heightINCHLabel = new JLabel("inch");
        heightINCHField = new JTextField(3);
        cmButton = new JToggleButton("cm", true);
        feetButton = new JToggleButton("feet");
        heightPanel.add(heightLabel);

        ButtonGroup heightGroup = new ButtonGroup();
        heightGroup.add(cmButton);
        heightGroup.add(feetButton);

        heightPanel.add(heightCMField);

        cmButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                  heightSelection = "cm";
                  heightPanel.remove(heightCMField);
                  heightPanel.remove(cmButton);
                  heightPanel.remove(feetButton);
                  heightPanel.remove(heightFTField);
                  heightPanel.remove(heightFTLabel);
                  heightPanel.remove(heightINCHField);
                  heightPanel.remove(heightINCHLabel);
                  heightPanel.add(heightCMField);
                  heightPanel.add(cmButton);
                  heightPanel.add(feetButton);
                  weightHeightPanel.add(heightPanel, BorderLayout.CENTER);
                  combinedGAHWpanel.add(weightHeightPanel, BorderLayout.SOUTH);
                  mainPanel.add(combinedGAHWpanel);
                  add(mainPanel);
                  setVisible(true);
                }
            });

        feetButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                  heightSelection = "feet";
                  heightPanel.remove(heightCMField);
                  heightPanel.remove(cmButton);
                  heightPanel.remove(feetButton);
                  heightPanel.add(heightFTField);
                  heightPanel.add(heightFTLabel);
                  heightPanel.add(heightINCHField);
                  heightPanel.add(heightINCHLabel);
                  heightPanel.add(cmButton);
                  heightPanel.add(feetButton);
                  weightHeightPanel.add(heightPanel, BorderLayout.CENTER);
                  combinedGAHWpanel.add(weightHeightPanel, BorderLayout.SOUTH);
                  mainPanel.add(combinedGAHWpanel);
                  add(mainPanel);
                  setVisible(true);
                }
            });

        heightPanel.add(cmButton);
        heightPanel.add(feetButton);


        // Weight JComponents
        weightLabel = new JLabel("Weight:");
        weightField = new JTextField(4);
        kgButton = new JToggleButton("kg", true);
        lbButton = new JToggleButton("lbs");

        weightPanel.add(weightLabel);
        weightPanel.add(weightField);
        weightPanel.add(kgButton);
        weightPanel.add(lbButton);

        ButtonGroup weightGroup = new ButtonGroup();
        weightGroup.add(kgButton);
        weightGroup.add(lbButton);


        kgButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                weightSelection = "kg"; 
            }
        });

        lbButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                weightSelection = "lb"; 
            }
        });


        // tdee JComponents
        tdeeQuestionLabel = new JLabel("Calculate TDEE Also?");
        tdeeYes = new JRadioButton("Yes");
        tdeeNo = new JRadioButton("No", true);

        ButtonGroup tdeeButton = new ButtonGroup();
        tdeeButton.add(tdeeYes);
        tdeeButton.add(tdeeNo);

        tdeePanel.add(tdeeQuestionLabel);
        tdeePanel.add(tdeeYes);
        tdeePanel.add(tdeeNo);


        // activitylevel JComponents

        activityLevelLabel = new JLabel("Activity Level: ");
        activityLevelBox = new JComboBox(activityLevels);
        activityLevelBox.setSelectedIndex(0);

        activityLevelPanel.add(activityLevelLabel);
        activityLevelPanel.add(activityLevelBox);
        activityLevelBox.setEnabled(false);

        tdeeYes.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                  tdeeSelection = "yes";
                  activityLevelBox.setEnabled(true);
                }
            });

        tdeeNo.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                  tdeeSelection = "no";
                  activityLevelBox.setEnabled(false);
                }
            });



        // tdee and BMR value components

        bmrLabel = new JLabel("BMR: ");
        tdeeLabel = new JLabel("TDEE: ");
        bmrLabel.setFont(new Font("Monotype Corsiva",1,20));
        tdeeLabel.setFont(new Font("Monotype Corsiva",1,20));


        bmrTDEEValuesPanel.add(new JButton("Calculate"), BorderLayout.NORTH);
        bmrTDEEValuesPanel.add(bmrLabel, BorderLayout.CENTER);
        bmrTDEEValuesPanel.add(tdeeLabel, BorderLayout.SOUTH);





        // Adding sub JPanels to main JPanel
        mainPanel.add(imgPanel);

        combinedGAHWpanel.add(agePanel, BorderLayout.NORTH); // Combine genderPanel and agePanel (which are both flowLayouts) into a
                                                             // single BorderLayout panel where agePanel is given the Northern spot and
                                                             // genderPanel is given the center spot in the panel
        weightHeightPanel.add(weightPanel, BorderLayout.NORTH); // Nested borderlayouts, the weightHeightPanel is another borderLayout which is nested
                                                                 // into the southern position of the combinedGAHW border layout.
        weightHeightPanel.add(heightPanel, BorderLayout.CENTER);
        weightHeightPanel.add(tdeeBMRPanel, BorderLayout.SOUTH);




        combinedGAHWpanel.add(genderPanel, BorderLayout.CENTER);
        combinedGAHWpanel.add(weightHeightPanel, BorderLayout.SOUTH);


        mainPanel.add(combinedGAHWpanel);


        // adding to tdeeBMRPanel
        tdeeBMRPanel.add(tdeePanel, BorderLayout.NORTH);
        tdeeBMRPanel.add(activityLevelPanel, BorderLayout.CENTER);


        tdeeBMRPanel.add(bmrTDEEValuesPanel, BorderLayout.SOUTH);


        // Adding main JPanel and menubar to JFrame
        setJMenuBar(menuBar);
        add(mainPanel);
    }


    public static void main(String[] args) {

        BmrCalcv2 gui = new BmrCalcv2("BMR/TDEE Calculator");

        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setVisible(true);
        gui.setSize(330, 500);
        gui.setResizable(false);

    }
}

enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

这是您对UI所做的更改(添加/删除标签/字段)以及BorderLayout的复合使用的组合。

当你在“feet”和“CM”之间切换时,你实际上不需要更改标签/字段,因为用户需要的所有信息都已经可用(以按钮的形式)。

我将摆脱heightFTFieldheightINCHField并使用JToggleButton的{​​{1}}状态将其设为单个字段,以确定如何计算值。我还会删除selectedheightFTLabel标签,它们对切换按钮已经在进行的UI添加的价值很小。

现在,完成后,您可以摆脱heightINCHLabelcmButton的{​​{1}},因为它们没有提供有用的操作。

将来,您可能会发现更改组件的可见性状态更容易。

基于评论,基本示例

这演示了使用单个字段feetButton来管理ActionListenerButtonGroup的选择以控制逻辑路径

JToggleButton