0

我正在尝试创建一个最大公约数计算器。我必须为这个项目创建一个 GUI 界面,我认为我这样做是正确的,但是我无法测试它,因为我的计算有问题。

具体来说,我在编译器中遇到以下两行错误:

 int x =  xTextField.getText();
 int y = yTextField.getText();

错误是“类型不匹配,无法从 String 转换为 int”我尝试插入 int x = 0; 和 int x = 0; 在它之前没有运气。我也尝试过将这些从整数更改为字符串,但它会使一切变得更加混乱。我不确定如何获取我应该使用的文本。

这是我的代码的计算部分:

 public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();
    if (source == exitButton)
        System.exit(0);
    else if (source == calculateButton) {
        int x =  xTextField.getText();
        int y = yTextField.getText();
        int gcd = greatestCommonDivisor(
                 x, y);
        gcdTextField.getText();
    }

}
private int greatestCommonDivisor(int x, int y) {
    // TODO Auto-generated method stub

    while (x != y) {
        if (x > y) {
            x = x - y;
        } else {
            y = y - x;
        }
    }
    return y;
 }

}

这是我的整个代码:

   package chapt15;

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

 public class FutureValueApp {
public static void main(String[] args) {
    JFrame frame = new GcdFrame();
    frame.setVisible(true);
 }

 }
 class GcdFrame extends JFrame {
public GcdFrame() {
    setTitle("Greatest Common Divisor Finder");
    centerWindow(this);
    setSize(267, 200);
//  setResizable(false);
    setResizable(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new GcdPanel();
    this.add(panel);
}

private void centerWindow(Window w) {
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension d = tk.getScreenSize();
    setLocation((d.width - w.getWidth()) / 2,
            (d.height - w.getHeight()) / 2);
}
 }

 class GcdPanel extends JPanel implements ActionListener {
private JTextField xTextField, yTextField,
        gcdTextField;
private JLabel xLabel, yLabel, gcdLabel;
private JButton calculateButton, exitButton;

public GcdPanel() {
    // display panel
    JPanel displayPanel = new JPanel();
//   displayPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    displayPanel.setLayout(new GridLayout(4, 2));
    // payment label
    xLabel = new JLabel("X:");
    displayPanel.add(xLabel);

    // payment text field
    xTextField = new JTextField(10);
    displayPanel.add(xTextField);

    // rate label
    yLabel = new JLabel("Y:");
    displayPanel.add(yLabel);

    // rate text field
    yTextField = new JTextField(10);
    displayPanel.add(yTextField);


    // future value label
    gcdLabel = new JLabel("GCD:");
    displayPanel.add(gcdLabel);

    // future value text field
    gcdTextField = new JTextField(10);
    gcdTextField.setEditable(false);
    gcdTextField.setFocusable(false);
    displayPanel.add(gcdTextField);

    // button panel
    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

    // calculate button
    calculateButton = new JButton("Calculate");
    calculateButton.addActionListener(this);
    buttonPanel.add(calculateButton);

    // exit button
    exitButton = new JButton("Exit");
    exitButton.addActionListener(this);
    buttonPanel.add(exitButton);

    // add panels to main panel
    this.setLayout(new BorderLayout());
    this.add(displayPanel, BorderLayout.CENTER);
    this.add(buttonPanel, BorderLayout.SOUTH);
}

public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();
    if (source == exitButton)
        System.exit(0);
    else if (source == calculateButton) {
        int x =  xTextField.getText();
        int y = yTextField.getText();
        int gcd = greatestCommonDivisor(
                 x, y);
        gcdTextField.getText();
    }

}
private int greatestCommonDivisor(int x, int y) {
    // TODO Auto-generated method stub

    while (x != y) {
        if (x > y) {
            x = x - y;
        } else {
            y = y - x;
        }
    }
    return y;
 }

}
4

1 回答 1

1

用这个:

int x =  Integer.parseInt(xTextField.getText());

因为xTextField.getText()将返回 aString而您不能将 a 分配Stringint

因此,必须使用that 将String其解析为等效值并将其解析为.intInteger.parseIntStringint

情况也是如此int y = yTextField.getText();

此外,如果您希望在 gdc 文本字段中设置计算的 gdc,那么您应该使用:

int gcd = greatestCommonDivisor(x, y);
gcdTextField.setText(String.valueOf(gcd));
于 2012-11-26T05:26:12.543 回答