2

好的,我对java有点陌生。我正在制作一个求解一步方程的程序。我在运行它时遇到了一些困难。这是我的主文件的代码Main.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Main extends JFrame implements ActionListener {
    private static final long serialVersionUID = 1L;
    Solve solve = new Solve();
    JButton add = new JButton("Add");
    JButton sub = new JButton("Subtract");
    JButton mult = new JButton("Multiply");
    JButton div = new JButton("Divide");
    JButton solv = new JButton("Solve!");
    JTextArea one = new JTextArea();
    JLabel two = new JLabel(" = ");
    JLabel three = new JLabel("X");
    JLabel four = new JLabel();
    JTextArea five = new JTextArea();
    JLabel solved = new JLabel();
    JPanel row1 = new JPanel();
    JPanel row2 = new JPanel();
    JPanel row3 = new JPanel();

    public double funct;

    public Main() {
        super("Solving a one step equation!");
        setSize(500, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        GridLayout layout = new GridLayout();
        setLayout(layout);

        FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER);

        row1.setLayout(layout1);
        row1.add(add);
        row1.add(sub);
        row1.add(mult);
        row1.add(div);
        row1.add(solv);
        add(row1);
        add.addActionListener(this);
        sub.addActionListener(this);
        mult.addActionListener(this);
        div.addActionListener(this);
        solv.addActionListener(this);

        GridLayout layout2 = new GridLayout(1, 1, 1, 1);
        row2.setLayout(layout2);
        row2.add(one, BorderLayout.CENTER);
        row2.add(two, BorderLayout.CENTER);
        row2.add(three, BorderLayout.CENTER);
        row2.add(four, BorderLayout.CENTER);
        row2.add(five);
        add(row2, BorderLayout.CENTER);

        GridLayout layout3 = new GridLayout(5, 5, 5, 5);
        row3.setLayout(layout3);
        row3.add(solved);
        add(row3);
    }

    public static void main(String[] args) {
        Main frame = new Main();
    }

    public void actionPerformed(ActionEvent evt) {
        Object source = evt.getSource();
        if(source == add)
        {
            four.setText(" + ");
            funct = 1;
        }
        else if(source == sub)
        {
            four.setText(" - ");
            funct = 2;
        }
        else if(source == mult)
        {
            four.setText(" * ");
            funct = 3;
        }
        else if(source == div)
        {
            four.setText(" / ");
            funct = 4;
        }
        if(source == solv)
        {
            if(funct == 1)
            {

                double Ones = Double.parseDouble(three.getText());
                double Twos = Double.parseDouble(three.getText());
                solved.setText("X = " + solve.Add(Ones, Twos));
            }
            else if(funct == 2)
            {
                double Ones = Double.parseDouble(three.getText());
                double Twos = Double.parseDouble(three.getText());
                solved.setText("X = " + solve.Sub(Ones, Twos));
            }
            else if(funct == 3)
            {
                double Ones = Double.parseDouble(three.getText());
                double Twos = Double.parseDouble(three.getText());
                solved.setText("X = " + solve.Mult(Ones, Twos));
            }
            else if(funct == 4)
            {
                double Ones = Double.parseDouble(three.getText());
                double Twos = Double.parseDouble(three.getText());
                solved.setText("X = " + solve.Div(Ones, Twos));
            }

        }
    }

}

这是我的另一个文件 Solve.java 的代码

public class Solve {
    public double Add(double One, double Two)
    {
        return One - Two;
    }

    public double Sub(double One, double Two)
    {
        return One + Two;
    }

    public double Mult(double One, double Two)
    {
        return One / Two;
    }

    public double Div(double One, double Two)
    {
        return One * Two;
    }
}

一些帮助将不胜感激。有人看到我做错了什么吗?

4

2 回答 2

5

单击一次NumberFormatException“解决”按钮。这似乎是一个复制/粘贴问题 - 您没有检索到正确的数字。您正在尝试将 'X' 字符串转换为双精度。最好给变量起有意义的名称。要修复异常,试试这个,替换:

double Ones = Double.parseDouble(three.getText());
double Twos = Double.parseDouble(three.getText());

和:

double Ones = Double.parseDouble(one.getText());
double Twos = Double.parseDouble(five.getText());

熟悉 Java代码约定,尤其是命名约定部分。

于 2012-06-16T18:45:45.337 回答
4

除了@Max 的有用答案,这里还有一些其他建议:

  • 将框架的布局设置为new GridLayout()默认为没有填充的单行和单列。作为替代方案,请考虑new GridLayout(0, 1, 5, 5),它在一列中产生任意数量的行,填充为 5x5。然后你可以专注于每一行的布局:

    row1.setLayout(new FlowLayout(FlowLayout.CENTER));
    row2.setLayout(new FlowLayout(FlowLayout.CENTER));
    row3.setLayout(new GridLayout(1, 1, 5, 5));
    
  • 将您的setVisible()调用移到框架构造函数的末尾:

    pack();
    setLocationRelativeTo(null);
    setVisible(true);
    
  • 考虑getRootPane().setDefaultButton(solv)Solve按钮设为默认按钮。

  • 考虑将添加设为默认值:

    private JLabel four = new JLabel("+");
    private int funct = 1; // add by default
    
  • 考虑JTextField用于数字输入:

    private JTextField one = new JTextField(10);
    private JTextField five = new JTextField(10);
    
于 2012-06-16T22:25:36.123 回答