我是Java的新手,我试图制作一个计算器,但结果有问题。它总是" 0"。我在Swing中使用了eclipse。我的代码是这样的:
package calculator1;
import java.awt.BorderLayout;
public class MainForm extends JFrame {
private JPanel contentPane;
private JTextField textResult;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainForm frame = new MainForm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
double number1=0;
double number2=0;
String islem="";
/**
* Create the frame.
*/
public MainForm() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 509, 534);
contentPane = new JPanel();
contentPane.setBackground(Color.BLACK);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
textResult = new JTextField();
textResult.setEditable(false);
textResult.setBounds(5, 5, 486, 42);
textResult.setColumns(10);
JButton btn1 = new JButton("1");
btn1.setBounds(45, 105, 80, 80);
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(islem.equals(""));
textResult.setText("");
textResult.setText(textResult.getText()+"1");
}
});
JButton btn4 = new JButton("4");
btn4.setBounds(45, 185, 80, 80);
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(islem.equals(""));
textResult.setText("");
textResult.setText(textResult.getText()+"4");
}
});
JButton btn7 = new JButton("7");
btn7.setBounds(45, 265, 80, 80);
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(islem.equals(""));
textResult.setText("");
textResult.setText(textResult.getText()+"7");
}
});
JButton btn5 = new JButton("5");
btn5.setBounds(125, 185, 80, 80);
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(islem.equals(""));
textResult.setText("");
textResult.setText(textResult.getText()+"5");
}
});
JButton btn8 = new JButton("8");
btn8.setBounds(125, 265, 80, 80);
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(islem.equals(""));
textResult.setText("");
textResult.setText(textResult.getText()+"8");
}
});
JButton btn2 = new JButton("2");
btn2.setBounds(125, 105, 80, 80);
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(islem.equals(""));
textResult.setText("");
textResult.setText(textResult.getText()+"2");
}
});
JButton btn3 = new JButton("3");
btn3.setBounds(205, 105, 80, 80);
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(islem.equals(""));
textResult.setText("");
textResult.setText(textResult.getText()+"3");
}
});
JButton btn6 = new JButton("6");
btn6.setBounds(205, 185, 80, 80);
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(islem.equals(""));
textResult.setText("");
textResult.setText(textResult.getText()+"6");
}
});
JButton btn9 = new JButton("9");
btn9.setForeground(Color.BLACK);
btn9.setBounds(205, 265, 80, 80);
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(islem.equals(""));
textResult.setText("");
textResult.setText(textResult.getText()+"9");
}
});
JButton btn0 = new JButton("0");
btn0.setBounds(125, 345, 80, 80);
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(islem.equals(""));
textResult.setText("");
textResult.setText(textResult.getText()+"0");
}
});
JButton btncarp = new JButton("*");
btncarp.setFont(new Font("Tahoma", Font.PLAIN, 35));
btncarp.setBounds(382, 60, 80, 80);
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
islem="*";
number1=Double.parseDouble(textResult.getText());
}
});
JButton btnarti = new JButton("+");
btnarti.setFont(new Font("Tahoma", Font.PLAIN, 35));
btnarti.setBounds(285, 265, 80, 80);
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
islem="+";
number1=Double.parseDouble(textResult.getText());
}
});
JButton btneksi = new JButton("-");
btneksi.setFont(new Font("Tahoma", Font.PLAIN, 35));
btneksi.setBounds(285, 105, 80, 80);
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
islem="-";
number1=Double.parseDouble(textResult.getText());
}
});
JButton btnbolu = new JButton("/");
btnbolu.setFont(new Font("Tahoma", Font.PLAIN, 35));
btnbolu.setBounds(285, 185, 80, 80);
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
islem="/";
number1=Double.parseDouble(textResult.getText());
}
});
JButton btnesittir = new JButton("=");
btnesittir.setForeground(new Color(255, 153, 255));
btnesittir.setFont(new Font("Tahoma", Font.PLAIN, 35));
btnesittir.setBounds(285, 345, 80, 80);
btnesittir.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
number1=Double.parseDouble(textResult.getText());
number2=Double.parseDouble(textResult.getText());
**double result=0;**
switch (islem) {
case "*":
result=number1*number2;
break;
case "/":
result=number1/number2;
break;
case "+":
result=number1+number2;
break;
case "-":
result=number1-number2;
break;
default:
break;
} //end switch
**textResult.setText(String.valueOf(result));**
islem="";
}
});
contentPane.setLayout(null);
contentPane.add(textResult);
contentPane.add(btn1);
contentPane.add(btn4);
contentPane.add(btn7);
contentPane.add(btn5);
contentPane.add(btn8);
contentPane.add(btn2);
contentPane.add(btn3);
contentPane.add(btn6);
contentPane.add(btn9);
contentPane.add(btn0);
contentPane.add(btncarp);
contentPane.add(btnesittir);
contentPane.add(btnarti);
contentPane.add(btneksi);
contentPane.add(btnbolu);
}
}
我可以写什么而不是" double result = 0;" ?
或许我需要改变别的东西?
答案 0 :(得分:2)
你似乎有一些严重的逻辑问题,看看......
number1 = Double.parseDouble(textResult.getText());
number2 = Double.parseDouble(textResult.getText());
这两个数字如何不同,您从同一来源(textResult
)
每次用户按下数字键时,您都会将islem
设置为""
,因此,如果用户按 1 + 2 ,islem
现在是""
...
现在的问题是,如何修复它......
您需要更改逻辑,而不是等到用户按下 = 按钮,您可以在输入值时执行每个计算......
这意味着您需要某种方法来维持运行结果并执行所需的计算
例如......
protected void performOperation(double value) {
switch (islem) {
case "*":
result *= value;
break;
case "/":
result /= value;
break;
case "+":
result += value;
break;
case "-":
result -= value;
break;
default:
result = value;
} //end switch
islem = "";
textResult.setText(String.valueOf(result));
}
这样做,需要计算一个值,确定需要执行的计算(如果有的话),更新textResult
字段并重新计算修改器...
您可能需要更改所有数字按钮以使用适当的值调用此方法,例如......
JButton btn1 = new JButton("1");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
performOperation(1);
}
});
现在,在您的原始代码中,您的所有修饰符按钮都没有做任何事情......这是因为您继续将操作注册到btn0
,所以您需要修复它......
JButton btneksi = new JButton("-");
//...
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
islem="-";
number1=Double.parseDouble(textResult.getText());
}
});
他们现在应该设置他们需要使用的计算修饰符......
JButton btncarp = new JButton("*");
//...
btncarp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
islem = "*";
}
});
哪个应该解决您似乎遇到的核心问题......
您可能还需要查看How to Use Actions了解更多详情
答案 1 :(得分:1)
我只是想指出您的代码所具有的问题,以及 我相信还有其他更好的方法来解决计算器问题 更少的代码和更简洁
首 主要问题,您的变量命名为可怕
例如,您将*
按钮命名为btncarp
,这对于任何阅读您代码的人都没有意义。
第二次问题,您将动作监听器添加到button 0
,*
和/
操作中的-
像
JButton btncarp = new JButton("*"); <---- your button name is btncarp
....
....
btn0.addActionListener(new ActionListener() { <----you add action listener to btn0?!!!
public void actionPerformed(ActionEvent e) {
......
......
}
});
您从未分配过number1
值的第三次问题,因此所有时间都会变为零
试试这个
btnarti.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
islem = "+";
number1 = Double.parseDouble(textResult.getText());
textResult.setText(" ");
}
});
解释:您将读取number1
值并清除结果框并准备好您将在操作方法中读取的第二个值
第四:您可以在number2
操作ActionListener部分中读取您的第二个数字=
,无需在该地点阅读number1
像
btnesittir.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
number2=Double.parseDouble(textResult.getText());
....
....
}
}
请为您的变量选择一些合理的名称
注意:您的代码几乎没问题,除了我指出的问题,您在错误的地方阅读了number1
和number2
个变量