区域计算器不工作,不知道为什么

时间:2013-07-16 00:37:58

标签: java eclipse swing calculator windowbuilder

这是我在eclipse windowbuilder中构建的计算器的源代码,我试图让两个textFields读取输入并输出一个简单的矩形区域,我可以在框中键入数字但是数字不是计算,我不知道我可能做错了什么,请批评我的代码,让我知道它为什么不工作。谢谢

*编辑,我已删除并更改了一些内容,现在“应答”框显示0.0,其他内容

    import java.awt.EventQueue;


public class GUI {

private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                GUI window = new GUI();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public GUI() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 694, 499);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);

    JPanel panel = new JPanel();
    tabbedPane.addTab("New tab", null, panel, null);
    panel.setLayout(null);

    JLabel lblInputBase = new JLabel("Input Base");
    lblInputBase.setBounds(0, 0, 57, 39);
    panel.add(lblInputBase);

    textField = new JTextField();
    textField.setBounds(0, 28, 86, 20);
    panel.add(textField);
    textField.setColumns(10);
    double value = Double.parseDouble(textField.getText());
    JLabel lblInputHeight = new JLabel("Input Height");
    lblInputHeight.setBounds(0, 212, 68, 20);
    panel.add(lblInputHeight);


    textField_1 = new JTextField();
    textField_1.setBounds(0, 230, 86, 20);
    panel.add(textField_1);
    textField_1.setColumns(10);
    double value_1 = Double.parseDouble(textField_1.getText());

    double Area = value*value_1;
    String finalArea = Double.toString(Area);

    JLabel lblArea = new JLabel("Area:");
    lblArea.setBounds(244, 119, 75, 68);
    panel.add(lblArea);

    textField_2 = new JTextField();
    textField_2.setBounds(273, 143, 86, 20);
    panel.add(textField_2);
    textField_2.setColumns(10);
    textField_2.setEditable(false);
    textField_2.setText(finalArea);

    }
}

1 个答案:

答案 0 :(得分:1)

你的代码真的很混乱,但是我发现了一些问题:

double value = Double.parseDouble(textField.getText());

这是在用户有机会查看gui之前运行的。 textField.getText()将在此处返回一个空字符串,并且parseDouble方法将在程序运行时始终抛出NumberFormatException,因为它不会将空字符串视为有效的数字格式。

double Area = Double.parseDouble(value)*Double.parseDouble(value_1);

这里,变量value已经是一个double,不需要解析,变量value_1也不存在。

如果您要将actionListener分配给textField_1,则为textField指定了第二个。

总的来说,似乎所有的计算逻辑都在initialize()方法中运行,这意味着它只运行一次。您希望每当更新其中一个文本字段时运行数学运算。

你的字段名称非常令人困惑。您的textField变量名称应该提供其功能的一些指示,例如areaTextFieldbaseTextFieldheightTextField


以下是对您的代码的修改,它似乎可以按照您的意图运行。请注意添加一个calculate()方法,该方法由将查找该区域并显示该区域的动作侦听器调用,或者如果任一输入字段不是有效数字,则显示NaN(非数字)。除了重命名文本字段变量外,所有GUI代码都保持不变。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

public class GUI {
    private JFrame frame;
    private JTextField baseTextField;
    private JTextField heightTextField;
    private JTextField areaTextField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    GUI window = new GUI();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public GUI() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 694, 499);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);

        JPanel panel = new JPanel();
        tabbedPane.addTab("New tab", null, panel, null);
        panel.setLayout(null);

        JLabel lblInputBase = new JLabel("Input Base");
        lblInputBase.setBounds(0, 0, 57, 39);
        panel.add(lblInputBase);

        baseTextField = new JTextField();
        baseTextField.setBounds(0, 28, 86, 20);
        panel.add(baseTextField);
        baseTextField.setColumns(20);
        baseTextField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                calculate();
            }
        });

        JLabel lblInputHeight = new JLabel("Input Height");
        lblInputHeight.setBounds(0, 212, 68, 20);
        panel.add(lblInputHeight);

        heightTextField = new JTextField();
        heightTextField.setBounds(0, 230, 86, 20);
        panel.add(heightTextField);
        heightTextField.setColumns(20);
        heightTextField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                calculate();
            }
        });

        JLabel lblArea = new JLabel("Area:");
        lblArea.setBounds(244, 119, 75, 68);
        panel.add(lblArea);

        areaTextField = new JTextField();
        areaTextField.setBounds(273, 143, 86, 20);
        panel.add(areaTextField);
        areaTextField.setColumns(20);
        areaTextField.setEditable(false);
    }

    private void calculate() {
        try {
            double base = Double.parseDouble(baseTextField.getText());
            double height = Double.parseDouble(heightTextField.getText());
            double area = base * height;
            areaTextField.setText(String.format("%.4f", area));
        } catch (NumberFormatException e) {
            areaTextField.setText("NaN");
        }
    }
}

请注意,您需要在两个字段之一中按Enter键以更新区域字段。这就是为JTextField触发ActionListener所需的。