【发布时间】:2019-01-13 13:14:09
【问题描述】:
我必须在 java swing/awt 中创建一个计算不同函数的计算器。
例如,它需要计算 GCD。
首先我创建了 UI,然后是 actionPerformed 函数,但是当我在 GUI 中执行 gcd 函数时,屏幕冻结了。
calculate2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Get values from text fields
try {
int a = Integer.parseInt(input1.getText());
int b = Integer.parseInt(input2.getText());
while (a != b) {
if(a > b)
a = a - b;
else
a = b - a;
}
String result1 = "" + (b);
result.setText((result1));
} catch (Exception f) {
JOptionPane.showMessageDialog(rootPane, "ERROR: " + (f.getMessage()));
}
String aField = input1.getText();
String bField = input2.getText();
if (e.getSource() == calculate2) {
if ("".equals(aField) || "".equals(bField)) {
String emptyFieldWarning;
emptyFieldWarning = "One or more fields is/are empty!";
JOptionPane.showMessageDialog(rootPane, emptyFieldWarning);
}
}
}
});
【问题讨论】:
-
GUI 冻结是因为您在函数中的某个点卡住了。正如@JanDeKock 建议的那样,我认为您的 while 循环中有错误(请参阅他的回答)。