Java 将值从JTextField传递到方法的最佳方法?
目前正在使用BMR/TDEE计算器。此计算器包括用户的年龄、性别、体重和身高,并使用此信息计算BMR和TDEE值。我已经为此创建了一个GUI,最后一步是进行实际计算。(还有一些小的改动要做,例如更改高度面板,但这与此问题无关) 将实例变量传递到静态方法的最佳方式是什么?我的JTextField是类中的实例变量,我有两种方法分别计算BMR和TDEE(静态方法) 代码发布在下面 我需要使用的变量几乎都是textField变量,这些变量需要传递到calcBMR()和calcTDEE()。我最初的想法是使所有这些变量都是静态的,只需使用getText()并将这些值插入到我使用的公式中,其大致如下: 10倍体重(kg)+6.25倍身高(cm)-5倍年龄(y)+5 这种方法听起来可行吗?或者在我的代码中有更好的方法吗?任何关于如何有效地做到这一点的建议都将不胜感激Java 将值从JTextField传递到方法的最佳方法?,java,swing,user-interface,variables,calculator,Java,Swing,User Interface,Variables,Calculator,目前正在使用BMR/TDEE计算器。此计算器包括用户的年龄、性别、体重和身高,并使用此信息计算BMR和TDEE值。我已经为此创建了一个GUI,最后一步是进行实际计算。(还有一些小的改动要做,例如更改高度面板,但这与此问题无关) 将实例变量传递到静态方法的最佳方式是什么?我的JTextField是类中的实例变量,我有两种方法分别计算BMR和TDEE(静态方法) 代码发布在下面 我需要使用的变量几乎都是textField变量,这些变量需要传递到calcBMR()和calcTDEE()。我最初的想法是
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;
private JLabel activityLevelHelp;
// 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;
private JButton calculate;
// Default values for gender/weight/height and other variables
String[] activityLevels = {"Sedentary", "Lightly Active", "Moderately Active", "Very Active", "Extra Active"};
String genderSelection = "M";
String weightSelection = "kg";
String heightSelection = "cm";
String tdeeSelection = "no";
String ageValue;
String weightValue;
String heightValue;
static String bmrValue = "N/A";
static String tdeeValue = "N/A";
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"));
activityLevelHelp = new JLabel(new ImageIcon("filesrc//question-mark.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);
heightPanel.add(heightFTField);
heightPanel.add(heightFTLabel);
heightPanel.add(heightINCHField);
heightPanel.add(heightINCHLabel);
heightPanel.add(cmButton);
heightPanel.add(feetButton);
heightFTField.setVisible(false);
heightFTLabel.setVisible(false);
heightINCHField.setVisible(false);
heightINCHLabel.setVisible(false);
cmButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
heightSelection = "cm";
heightINCHField.setVisible(false);
heightFTField.setVisible(false);
heightFTLabel.setVisible(false);
heightINCHLabel.setVisible(false);
heightCMField.setVisible(true);
weightPanel.revalidate();
weightPanel.repaint();
}
});
feetButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
heightSelection = "feet";
heightINCHField.setVisible(true);
heightFTField.setVisible(true);
heightFTLabel.setVisible(true);
heightINCHLabel.setVisible(true);
heightCMField.setVisible(false);
weightPanel.revalidate();
weightPanel.repaint();
}
});
// 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);
activityLevelPanel.add(activityLevelHelp);
activityLevelBox.setEnabled(false);
activityLevelHelp
.setToolTipText("<html><b>Sedentary:</b> little or no exercise, deskjob<<br /><b>Lightly Active:</b> little exercise/sports 1-3 days/week<br /><b>Moderately active:</b> moderate exercise/sports 3-5 days/week<br /><b>Very active:</b> hard exercise or sports 6-7 days/week<br /><b>Extra active:</b> hard daily exercise or sports & physical labor job </html>");
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
bmrValue = calcBmr();
bmrLabel = new JLabel("<html><br /><br /><font size=4>You have a <i><font color=red>BMR</font></i> of: " + bmrValue + "<font></html>");
tdeeLabel = new JLabel("<html><br /><font size=4>You have a <i><font color=red>TDEE</font></i> of: " + tdeeValue + "<font></html>");
calculate = new JButton("Calculate");
bmrTDEEValuesPanel.add(calculate, BorderLayout.NORTH);
bmrTDEEValuesPanel.add(bmrLabel, BorderLayout.CENTER);
bmrTDEEValuesPanel.add(tdeeLabel, BorderLayout.SOUTH);
// Debugging panels for buttons (remove when complete)
calculate.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.println("Male:" + genderMale.isSelected());
System.out.println("Female:" + genderFemale.isSelected() + "\n");
System.out.println("Kg:" + kgButton.isSelected());
System.out.println("LB:" + lbButton.isSelected() + "\n");
System.out.println("CM:" + cmButton.isSelected());
System.out.println("Feet:" + feetButton.isSelected() + "\n");
System.out.println("TDEE Yes:" + tdeeYes.isSelected());
System.out.println("TDEE No:" + tdeeNo.isSelected());
System.out.println("-------------------------------------");
}
});
// 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 String calcBmr() {
return bmrValue;
}
public static String calcTDEE() {
return tdeeValue;
}
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);
}
}
公共类BmrCalcv2扩展了JFrame{
//框架和主面板
静态JFrame主机;
静态JPanel主面板;
静态JPanel combinedGAHWpanel;//组合genderPanel、agePanel、heightPanel、weightPanel-此
//是一个边界布局,而
//性别/年龄面板是流动布局。
静态JPanel weightHeightPanel;//将重量和高度面板组合到out flowlayout面板中,然后再组合到上面的borderlayout面板中
//图像组件
静态JPanel-imgPanel;
私人JLabel imgLabel;
私人JLabel activityLevelHelp;
//菜单栏组件
静态JMenuBar菜单栏;
静态JMenu保存菜单、选项菜单、帮助菜单;
//年龄构成
静态面板;
私人标签;
私人JLabel yearsLabel;
私有JTextField ageTextField;
//性别组成部分
静态JPanel-genderPanel;
私人JLabel性别标签;
私人JRadioButton genderMale;
私人JRadioButton性别女性;
//高度分量
静态JPanel高面板;
私人JLabel高度标签;
私有JTextField heightCMField;
私人JLabel heightFTLabel;
私人JLabel高度标签;
私人JTextField heightFTField;
私人JTextField heightINCHField;
专用JToggleButton cmButton;
私人JToggleButton feetButton;
//重量分量
静态JPanel称重面板;
私有JLabel权重标签;
私有JTextField-weightField;
私人JToggleButton kgButton;
私人JToggleButton lbButton;
//TDEE和BMR组件
静态JPanel tdeePanel;
静态JPanel tdeeBMRPanel;
静态JPanel活动面板;
静态JPanel bmrTDEEValuesPanel;
静态JPanel BMR右面板;
静态JPanel tdeeValuePanel;
私人JLabel-tdeeQuestionLabel;
私人JLabel活动标签;
私人JComboBox活动邮箱;
私人JRadioButton tdeeYes;
私人JRadioButton tdeeNo;
私人JLabel bmrLabel;
私人JLabel-tdeeLabel;
私有按钮计算;
//性别/体重/身高和其他变量的默认值
String[]ActivityLevel={“久坐”、“轻度活跃”、“中度活跃”、“非常活跃”、“极度活跃”};
字符串genderSelection=“M”;
字符串权重选择=“kg”;
字符串高度选择=“cm”;
字符串tdeeSelection=“否”;
字符串值;
字符串权重值;
字符串高度值;
静态字符串bmrValue=“N/A”;
静态字符串tdeeValue=“不适用”;
公共BmrCalcv2(字符串标题){
//主框架
setTitle(“BMR/TDEE计算器”);
mainPanel=新的JPanel();
//所有JPanel声明
menuBar=newjmenubar();
imgPanel=新的JPanel();
agePanel=newjpanel();
genderPanel=新的JPanel();
heightPanel=新的JPanel();
weightPanel=newjpanel();
weightHeightPanel=newJPanel(newBorderLayout());
combinedGAHWpanel=new JPanel(new BorderLayout());//创建用于组合的新面板
//性别面板、年龄面板、重量面板、高度面板如下
tdeeBMRPanel=newjpanel(newborderlayout());
tdeePanel=新的JPanel();
activityLevelPanel=newjpanel();
bmrTDEEValuesPanel=newjpanel(newborderlayout());
bmrValuePanel=newJPanel();
tdeeValuePanel=newjpanel();
//图像面板声明
imgLabel=newjlabel(newimageicon(“filesrc//mainlogo.png”);
activityLevelHelp=newjlabel(新的图像图标(“filesrc//问号.png”);
imgPanel.add(imgLabel);
//JPanel布局管理器
agePanel.setLayout(新的FlowLayout(FlowLayout.CENTER,5,5));
genderPanel.setLayout(新的FlowLayout(FlowLayout.CENTER,5,5));
//菜单组件
saveMenu=新建JMenu(“保存”);
optionMenu=新JMenu(“选项”);
帮助菜单=新JMenu(“帮助”);
菜单栏。添加(保存菜单);
菜单栏。添加(选项菜单);
菜单栏。添加(帮助菜单);
//年龄组分
ageLabel=新的JLabel(“年龄:”);
yearsLabel=新的JLabel(“年”);
ageTextField=新的JTextField(5);
agePanel.add(ageLabel);
agePanel.add(ageTextField);
agePanel.add(yearsLabel);
//性别构成
genderLabel=新的JLabel(“性别:”);
genderMale=新的JRadioButton(“男性”,正确);
性别女性=新的JRadioButton(“女性”);
genderPanel.add(genderLabel);
genderPanel.add(genderMale);
genderPanel.add(性别女性);
ButtonGroup genderGroup=new ButtonGroup();//将男性和女性单选按钮组合在一起,以便只能选择一个