Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从IF语句在GUI中输出字符串_Java - Fatal编程技术网

Java 从IF语句在GUI中输出字符串

Java 从IF语句在GUI中输出字符串,java,Java,我是编程新手,我一直试图将这段字符串从if语句输出到GUI字段,当程序编译时,应答字段只输出最后一条语句,而另一个按钮工作正常。。。我已附上密码 else if附加到try语句,那些if语句要么没有运行,要么什么的,但它只输出final语句 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Assignment2 implements ActionListener { JLabel

我是编程新手,我一直试图将这段字符串从if语句输出到GUI字段,当程序编译时,应答字段只输出最后一条语句,而另一个按钮工作正常。。。我已附上密码

else if附加到try语句,那些if语句要么没有运行,要么什么的,但它只输出final语句

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Assignment2 implements ActionListener {
    JLabel ans, ans2;
    JTextField text1, text2;

    public void displayWindow(){
        JFrame frame= new JFrame ("BMI Calculator");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setLayout (new GridLayout (5,2,7,10));

        text1= new JTextField();
        text2= new JTextField();

        JButton buttonBMI= new JButton ("BMI (Body Mass Index)");
        buttonBMI.setActionCommand ("bmi");
        buttonBMI.addActionListener (this);

        JButton buttonH= new JButton ("Am I Healthy?");
        buttonH.setActionCommand ("health");
        buttonH.addActionListener (this);

        ans= new JLabel();
        ans2= new JLabel();

        frame.add (new JLabel ("Height (m)"));
        frame.add (text1);
        frame.add (new JLabel ("Weight (kg)"));
        frame.add (text2);
        frame.add (buttonBMI);
        frame.add (buttonH);
        frame.add (new JLabel ("Answer: "));
        frame.add (ans);
        frame.add (new JLabel ("Answer: "));
        frame.add (ans2);

        frame.pack();
        frame.setVisible(true);
    }

        public void actionPerformed (ActionEvent e){
            try {
                double a= Double.parseDouble (text1.getText());
                double b= Double.parseDouble (text2.getText());
                double answer=0;
                double height=0;
                String answer2= "";

                String command = e.getActionCommand();

                if (command.equals ("bmi")){
                    height = Math.pow (a,2);
                    answer = b / height;
                    ans.setText ("" +answer);
                }
                else if (command.equals ("health")){
                    if (answer > 30){
                        ans2.setText ("You are Obese");

                    }
                    else if (answer > 29.9 && answer < 24.9 ){
                        ans2.setText ("You are Overweight");

                    }
                    else if (answer > 25 && answer < 18.5){
                        ans2.setText ("You are of Normal Weight");

                    }
                    else if (answer < 18.5){
                        ans2.setText("You are Underweight");

                    }
                }
        }
        catch (Exception ex){
            ans.setText ("Please use Numbers");
        }
}
}

在代码中初始化应答的语句位于if语句中,该语句将命令与bmi进行比较。如果命令不是bmi,则不执行该语句。您需要将其移到if语句之外。

此if语句

else if (answer < 18.5){ ... }
你总是错的。答案值不能同时大于29.9和小于24.9

试试这个

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Assignment2 implements ActionListener {
    JLabel ans, ans2;
    JTextField text1, text2;
    double answer=0;
    double height=0;

    public void displayWindow(){
        JFrame frame= new JFrame ("BMI Calculator");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setLayout (new GridLayout (5,2,7,10));

        text1= new JTextField();
        text2= new JTextField();

        JButton buttonBMI= new JButton ("BMI (Body Mass Index)");
        buttonBMI.setActionCommand ("bmi");
        buttonBMI.addActionListener (this);

        JButton buttonH= new JButton ("Am I Healthy?");
        buttonH.setActionCommand ("health");
        buttonH.addActionListener (this);

        ans= new JLabel();
        ans2= new JLabel();

        frame.add (new JLabel ("Height (m)"));
        frame.add (text1);
        frame.add (new JLabel ("Weight (kg)"));
        frame.add (text2);
        frame.add (buttonBMI);
        frame.add (buttonH);
        frame.add (new JLabel ("Answer: "));
        frame.add (ans);
        frame.add (new JLabel ("Answer: "));
        frame.add (ans2);

        frame.pack();
        frame.setVisible(true);
    }

        public void actionPerformed (ActionEvent e){
            try {
                double a= Double.parseDouble (text1.getText());
                double b= Double.parseDouble (text2.getText());
                String answer2= "";

                String command = e.getActionCommand();

                if (command.equals ("bmi")){
                    height = Math.pow (a,2);
                    answer = b / height;
                    ans.setText ("" +answer);
                }
                else if (command.equals ("health")){
                    if (answer > 30){
                        ans2.setText ("You are Obese");

                    }
                    else if (answer > 24.9 && answer < 29.9 ){
                        ans2.setText ("You are Overweight");

                    }
                    else if (answer > 18.5&& answer < 25){
                        ans2.setText ("You are of Normal Weight");

                    }
                    else if (answer < 18.5){
                        ans2.setText("You are Underweight");

                    }
                }
        }
        catch (Exception ex){
            ans.setText ("Please use Numbers");
        }
}
}

你为什么要把text1的声明和初始化分开呢?我就是这么做的。我把答案=b/高度移到了if和ans之外。setText+答案现在只是说你肥胖了,我按照Flood2d的建议修改了if语句。变量答案从未在try块之外使用过,因此,将其作为实例变量是一种糟糕的样式。全局作用域是另一个。变量answer在if语句外部显式初始化为0。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Assignment2 implements ActionListener {
    JLabel ans, ans2;
    JTextField text1, text2;
    double answer=0;
    double height=0;

    public void displayWindow(){
        JFrame frame= new JFrame ("BMI Calculator");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setLayout (new GridLayout (5,2,7,10));

        text1= new JTextField();
        text2= new JTextField();

        JButton buttonBMI= new JButton ("BMI (Body Mass Index)");
        buttonBMI.setActionCommand ("bmi");
        buttonBMI.addActionListener (this);

        JButton buttonH= new JButton ("Am I Healthy?");
        buttonH.setActionCommand ("health");
        buttonH.addActionListener (this);

        ans= new JLabel();
        ans2= new JLabel();

        frame.add (new JLabel ("Height (m)"));
        frame.add (text1);
        frame.add (new JLabel ("Weight (kg)"));
        frame.add (text2);
        frame.add (buttonBMI);
        frame.add (buttonH);
        frame.add (new JLabel ("Answer: "));
        frame.add (ans);
        frame.add (new JLabel ("Answer: "));
        frame.add (ans2);

        frame.pack();
        frame.setVisible(true);
    }

        public void actionPerformed (ActionEvent e){
            try {
                double a= Double.parseDouble (text1.getText());
                double b= Double.parseDouble (text2.getText());
                String answer2= "";

                String command = e.getActionCommand();

                if (command.equals ("bmi")){
                    height = Math.pow (a,2);
                    answer = b / height;
                    ans.setText ("" +answer);
                }
                else if (command.equals ("health")){
                    if (answer > 30){
                        ans2.setText ("You are Obese");

                    }
                    else if (answer > 24.9 && answer < 29.9 ){
                        ans2.setText ("You are Overweight");

                    }
                    else if (answer > 18.5&& answer < 25){
                        ans2.setText ("You are of Normal Weight");

                    }
                    else if (answer < 18.5){
                        ans2.setText("You are Underweight");

                    }
                }
        }
        catch (Exception ex){
            ans.setText ("Please use Numbers");
        }
}
}