2014-07-06 33 views
-4

我的代碼的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

嘗試格式化上面的代碼,它使其他人更容易幫助您。此外,「太長的錯誤」是由於'java.lang.NumberFormatException',具體而言,你正試圖將''「''轉換爲'Integer' – Tamer

+0

你正試圖比較對象使用'=='這不會工作,使用而不是.equals(...)'。這可能有所幫助:http://stackoverflow.com/questions/7520432/java-vs-equals-confusion –

+0

@maximus_de這不是問題。實際上,'getSource'返回點擊對象的引用,所以'=='是進行比較的正確方法。 –

回答

1

問題是,您正在構造函數中重新編寫JButton變量。

所以,初始化JButtons這樣的:

b1=new JButton("0"); 

,而不是

JButton b1=new JButton("0"); 

否則,actionPerformed方法getSource將無法​​正常工作,也不會在任何if進入條件,因爲這些按鈕不是最初聲明的。

+0

thanq這麼多...解決了我的大部分問題... :) –

+0

@ApurvaNagar很高興幫助。請標記答案已接受:) –