【问题标题】:The GUI is perfect, but the calculator code does not work [closed]GUI是完美的,但计算器代码不起作用[关闭]
【发布时间】:2014-07-06 16:13:00
【问题描述】:

我的代码的图形用户界面很完美,但是计算器不工作,即当我单击一个按钮时,它没有显示在文本字段中。这是我的代码:-

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

class calc extends Frame implements ActionListener
{
int resulta=0;
int resultb=1;
JTextField tf;
int result;
private JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b16;

calc()
{
tf=new JTextField();
tf.setBounds(60,50,200,20);
JButton b1=new JButton("0");
b1.setBounds(60,100,30,30);

JButton b2=new JButton("1");
b2.setBounds(100,100,30,30);

JButton b3=new JButton("2");
b3.setBounds(140,100,30,30);

JButton b4=new JButton("3");
b4.setBounds(60,150,30,30);

JButton b5=new JButton("4");
b5.setBounds(100,150,30,30);

JButton b6=new JButton("5");
b6.setBounds(140,150,30,30);

JButton b7=new JButton("6");
b7.setBounds(60,200,30,30);

JButton b8=new JButton("7");
b8.setBounds(100,200,30,30);

JButton b9=new JButton("8");
b9.setBounds(140,200,30,30);

JButton b10=new JButton("9");
b10.setBounds(60,250,30,30);

JButton b11=new JButton("+");
b11.setBounds(100,250,30,30);

JButton b12=new JButton("-");
b12.setBounds(140,250,30,30);

JButton b13=new JButton("/");
b13.setBounds(60,300,30,30);

JButton b14=new JButton("*");
b14.setBounds(100,300,30,30);


JButton b16=new JButton("Result");
b16.setBounds(60,350,100,20);


b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);

b16.addActionListener(this);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(b10);
add(b11);
add(b12);
add(b13);
add(b14);

add(b16);
add(tf);
setSize(500,500);
setLayout(null);
setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
tf.setText(b1.getText());
else if(e.getSource()==b2)
tf.setText(b2.getText());
else if(e.getSource()==b3)
tf.setText(b3.getText());
else if(e.getSource()==b4)
tf.setText(b4.getText());
else if(e.getSource()==b5)
tf.setText(b5.getText());
else if(e.getSource()==b6)
tf.setText(b6.getText());
else if(e.getSource()==b7)
tf.setText(b7.getText());
else if(e.getSource()==b8)
tf.setText(b8.getText());
else if(e.getSource()==b9)
tf.setText(b9.getText());
else if(e.getSource()==b10)
tf.setText(b10.getText());
String v1=tf.getText();
int num1= Integer.parseInt(v1);

if(e.getSource()==b10||e.getSource()==b11||e.getSource()==b12||e.getSource()==b13||e.getSource()==b14)
{
tf.setText("");


Object op= e.getSource();

if(op == b11)
{
resulta=resulta+num1;
}
else if(op == b12)
{
resulta=num1-resulta;
}
else if(op == b13)
{
resultb=num1/resultb;
}
else if(op == b14)
{
resultb=num1*resultb;
}

if(op==b11||op==b12)
result=resulta;
else 
result=resultb;
if(op==b16)
JOptionPane.showMessageDialog(this,"Result"+result);
}
}
public static void main(String args[])
{
new calc();
}
}

我无法在此处输入错误,因为它太长了。

【问题讨论】:

  • 尝试格式化上面的代码,这样其他人更容易帮助你。 “太长的错误”也是由于java.lang.NumberFormatException,特别是您试图将"" 转换为Integer
  • 您正在尝试使用== 比较对象,这不起作用,请改用.equals(...)。这可能会有所帮助:stackoverflow.com/questions/7520432/java-vs-equals-confusion
  • @maximus_de 这不是问题。其实getSource返回的是被点击对象的引用,所以==才是正确的比较方式。
  • @HugoSousa 很好,在这种情况下我一直使用.equals(...),而从不使用==。此外== 没用……但我不否认我可能错了。

标签: java swing awt


【解决方案1】:

问题在于您在构造函数中重新定义了JButton 变量。

所以,像这样初始化你的JButtons

b1=new JButton("0");

而不是

JButton b1=new JButton("0");

否则,actionPerformed 方法中的getSource 将无法正常工作,它不会进入任何if 条件,因为按钮不是最初声明的。

【讨论】:

  • 非常感谢...解决了我的大部分问题... :)
  • @ApurvaNagar 很高兴它有帮助。请将答案标记为已接受:)
猜你喜欢
  • 2020-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多