我试图在我的程序中添加一个小数点。我设法添加按钮但问题是它没有出现在文本字段上,如果有人可以帮助谢谢。 PS。我不是很擅长在程序中添加评论,如果这困扰任何人,那就不好了。
import java.awt.*;
import java.awt.event.*;
import java.awt.Font;
public class Finals extends java.applet.Applet implements ActionListener {
TextField txtTotal = new TextField("");
Button button[] = new Button[10];
Button divide = new Button("/");
Button mult = new Button("*");
Button plus = new Button ("+");
Button minus = new Button("-");
Button isequalto = new Button("=");
Button clear = new Button("Clear");
double num ,numtemp ;
int counter;
String strnum = "",strnumtemp = "" ;
String op = "";
public void operation() {
counter ++;
if (counter == 1) {
numtemp = num;
strnum = "";
num = 0;
}else{
if (op == "+") numtemp += num;
else if (op == "-") numtemp -= num;
else if (op == "*") numtemp = numtemp * num;
else if (op == "/") numtemp = numtemp / num;
strnumtemp = Double.toString(numtemp);
txtTotal.setText(strnumtemp);
strnum = "";
num = 0;
}
}
public void init() {
setLayout(null);
setSize(268,318);
Font myFont = new Font("Arial",Font.BOLD,15);
Font bigFont = new Font("Arial",Font.BOLD,20);
// *color Background of components*
plus.setBackground(Color.black);
plus.setForeground(Color.white);
plus.setFont(myFont);
minus.setBackground(Color.black);
minus.setForeground(Color.white);
minus.setFont(bigFont);
divide.setBackground(Color.black);
divide.setForeground(Color.white);
divide.setFont(bigFont);
isequalto.setBackground(Color.black);
isequalto.setForeground(Color.white);
isequalto.setFont(myFont);
mult.setBackground(Color.black);
mult.setForeground(Color.white);
mult.setFont(bigFont);
clear.setBackground(Color.black);
clear.setForeground(Color.white);
clear.setFont(myFont);
txtTotal.setBackground(Color.darkGray);
txtTotal.setForeground(Color.white);
txtTotal.setFont(bigFont);
for(int i = 0;i <= 9; i ++) {
button[i] = new Button(String.valueOf(i));
button[i].setBackground(Color.lightGray);
button[i].setForeground(Color.black);
button[i].setFont(myFont);
}
button[1].setBounds(0,159,67,53);
button[2].setBounds(67,159,67,53);
button[3].setBounds(134,159,67,53);
button[4].setBounds(0,106,67,53);
button[5].setBounds(67,106,67,53);
button[6].setBounds(134,106,67,53);
button[7].setBounds(0,53,67,53);
button[8].setBounds(67,53,67,53);
button[9].setBounds(134,53,67,53);
for (int i = 1;i <= 9; i ++) {
add(button[i]);
}
txtTotal.setBounds(0,0,267,53);
add(txtTotal);
plus.setBounds(201,212,67,106);
add(plus);
button[0].setBounds(0,212,134,53);
add(button[0]);
minus.setBounds(201,159,67,53);
add(minus);
mult.setBounds(201,106,67,53);
add(mult);
divide.setBounds(201,53,67,53);
add(divide);
isequalto.setBounds(134,212,67,53);
add(isequalto);
clear.setBounds(0, 265, 201, 53);
add(clear);
}
public void start() {
for(int i = 0;i <= 9; i ++) {
button[i].addActionListener(this);
}
plus.addActionListener(this);
minus.addActionListener(this);
divide.addActionListener(this);
mult.addActionListener(this);
isequalto.addActionListener(this);
clear.addActionListener(this);
}
public void stop() {
for(int i = 0;i <= 9; i ++) {
button[i].addActionListener(null);
}
plus.addActionListener(null);
minus.addActionListener(null);
divide.addActionListener(null);
mult.addActionListener(null);
isequalto.addActionListener(null);
clear.addActionListener(null);
}
public void actionPerformed(ActionEvent e) {
for(int i = 0;i <= 9; i++) {
if (e.getSource() == button[i]) {
play(getCodeBase(),i + ".au");
strnum += Integer.toString(i);
txtTotal.setText(strnum);
num = Double.valueOf(strnum).doubleValue();
}
}
if (e.getSource() == plus) {
operation();
op = "+";
}
if (e.getSource() == minus) {
operation();
op = "-";
}
if (e.getSource() == divide) {
operation();
op = "/";
}
if (e.getSource() == mult) {
operation();
op = "*";
}
if (e.getSource() == isequalto) {
if (op == "+") numtemp += num;
else if (op == "-") numtemp -= num;
else if (op == "*") numtemp = numtemp * num;
else if (op == "/") numtemp = numtemp / num;
strnumtemp = Double.toString(numtemp);
txtTotal.setText(strnumtemp);
strnumtemp = "";
numtemp = 0;
strnum = "";
num = 0;
counter = 0;
}
if (e.getSource() == clear) {
txtTotal.setText("0");
strnumtemp = "";
numtemp = 0;
strnum = "";
num = 0;
counter = 0;
}
}
}