2013-08-22 2 views
0

간단한 계산기를 만들어서 실력을 높이고 싶었습니다. 내가 대신 콘솔에 오류 번호에 넣거나 점점 대답 필드의 텍스트를 설정하려고 할 때,홀수 오류를 반환하는 TextField.setText()

java.awt.TextField[textfield0,356,6,52x23,invalid,text=,selection=0-0] 

나는 전에 그런 문제가 없었어요, 그래서 필드에이를두고 나는 그 원인을 생각할 수 없다. 여기 코드가 있습니다.

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

public class aa extends JFrame implements ActionListener { 

static TextField num1 = new TextField(3); 
static TextField num2 = new TextField(3); 
int numA = 0; 
static TextField ans = new TextField(4); 
JButton addB = new JButton("+"); 
JButton subB = new JButton("-"); 
JButton mulB = new JButton("*"); 
JButton divB = new JButton("%"); 

public static void main(String[] args) { 
    aa app =new aa(); 

} 

public aa(){ 
    this.setVisible(true); 
    this.setLocationRelativeTo(null); 
    this.setSize(500, 400); 
    this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    JPanel content = new JPanel(); 
    this.setLayout(new FlowLayout()); 
    this.add(num1); 
    this.add(num2); 
    this.add(addB); 
     addB.addActionListener(this); 
    this.add(subB); 
     subB.addActionListener(this); 
    this.add(mulB); 
     divB.addActionListener(this); 
    this.add(divB); 
     divB.addActionListener(this); 
    this.add(ans); 
     ans.setEditable(false); 
} 

public void actionPerformed(ActionEvent e) { 
    if(e.getSource() == this.addB){ 
     ans.setText(""); 
     int x = Integer.parseInt(num1.getText()); 
     int y = Integer.parseInt(num2.getText()); 
     numA = x + y; 
     System.out.print(numA); 
     ans.setText(ans.toString()); 
    } 
    if(e.getSource() == this.subB){ 
     ans.setText(""); 
     int x = Integer.parseInt(num1.getText()); 
     int y = Integer.parseInt(num2.getText()); 
     numA = x - y; 
     System.out.print(numA); //these parts were to make sure that it was actually doing the math, which it was. 
     ans.setText(""); 

    } 
    if(e.getSource() == this.mulB){ 
     ans.setText(""); 
    } 
} 
} 

모든 아이디어를 얻을 수 있습니다.

답변

4

당신은 또한 아마 일관성을 위해 스윙의 JTextField를 사용하려면 당신은

ans.setText(Integer.toString(numA)); 

를 원하는 여기

ans.setText(ans.toString()); 

TextField#toString의 결과를보고있다.

+0

Alrighty이 할 것 도움말 C 주셔서 감사합니다 :. –

+0

을 난 그냥 말할 수, 다시 이에 대한보고 내가 좀 바보 같은 느낌이 –

+0

SO'는 조언을 sillier : – Reimeus

0

ans은 텍스트 필드입니다. 텍스트 필드의 속성을 인쇄하는 문자열로 텍스트 필드 객체를 변환하려고합니다. numA을 string으로 변환 해보세요. (즉 Integer.toString(numA));

+0

감사를 보았다 '. 나는 이것을 다음에 염두에 두겠다. C : –

관련 문제