我的代码的GUI是完美的,但计算器不工作,即当我点击一个按钮时,它没有显示在文本字段中。这是我的代码: -
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();
}
}
我不能在这里输入错误,因为它太长了。
答案 0 :(得分:1)
问题在于您正在重构构造函数中的JButton
变量。
所以,像这样初始化你的JButtons
:
b1=new JButton("0");
而不是
JButton b1=new JButton("0");
否则,getSource
方法中的actionPerformed
无法正常工作,它无法进入任何if
条件,因为按钮不是{&1;}。最初申报的。