간단한 계산기를 만들어서 실력을 높이고 싶었습니다. 내가 대신 콘솔에 오류 번호에 넣거나 점점 대답 필드의 텍스트를 설정하려고 할 때,홀수 오류를 반환하는 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("");
}
}
}
모든 아이디어를 얻을 수 있습니다.
Alrighty이 할 것 도움말 C 주셔서 감사합니다 :. –
을 난 그냥 말할 수, 다시 이에 대한보고 내가 좀 바보 같은 느낌이 –
SO'는 조언을 sillier : – Reimeus