我正在学习Java,我打算创建一个计算器Java applet,它具有添加,分割,乘法和重置功能。得到一些警告信号:
应该以静态方式访问String类型的静态方法valueOf(double)
计算器正在工作。但请善意地了解如何避免这种警告标志以及我可以对编码做出哪些改进。
CalEngine类,包括监听器,数学函数。
public class CalEngine implements ActionListener{
Calculator parent;
double current=0;
double prevnum=0;
double totalnum=0;
int funct=0;
String equalnum;
public CalEngine(Calculator parent) {
this.parent = parent;
}
public void actionPerformed(ActionEvent e) {
JButton src = (JButton) e.getSource();
String cbuttonlabel = src.getText();
if(cbuttonlabel.equals("+"))
{ clearscreen();
funct = 1; }
else if(cbuttonlabel.equals("-"))
{ clearscreen();
funct = 2; }
else if(cbuttonlabel.equals("/"))
{ clearscreen();
funct = 3; }
else if(cbuttonlabel.equals("="))
{ equal(); }
else if(cbuttonlabel.equals("Reset"))
{ prevnum = 0;
current = 0;
totalnum =0;
clearscreen();
}
else
{
prevnum = current;
current = Double.parseDouble(cbuttonlabel);
parent.setScreen(cbuttonlabel);
}
}
private void equal() {
if(funct == 1)
{
add();
}
else if(funct == 2)
{
minus();
}
else if(funct == 3)
{
divide();
}
parent.setScreen(equalnum.valueOf(totalnum));
totalnum =0;
}
private void minus() {
totalnum = prevnum-current;}
private void divide() {
totalnum = prevnum/current; }
private void clearscreen() {
parent.setScreen(""); }
private void add() {
totalnum = current + prevnum;
}
}
Calculator UI
public class Calculator {
JPanel ova;
JPanel pan;
JTextField screen;
JButton b1;
JButton b2;
JButton b3;
JButton b4;
JButton b5;
JButton b6;
JButton b7;
JButton b8;
JButton b9;
JButton b0;
JButton bplus;
JButton bequal;
JButton bminus;
JButton bdivide;
JButton breset;
Calculator(){
ova = new JPanel();
BorderLayout bl = new BorderLayout();
ova.setLayout(bl);
screen = new JTextField(300);
ova.add("North",screen);
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
b0 = new JButton("0");
bplus = new JButton("+");
bequal = new JButton("=");
bminus = new JButton("-");
bdivide = new JButton("/");
breset = new JButton("Reset");
pan = new JPanel();
GridLayout gl = new GridLayout(5,3);
pan.setLayout(gl);
pan.add(b1);
pan.add(b2);
pan.add(b3);
pan.add(b4);
pan.add(b5);
pan.add(b6);
pan.add(b7);
pan.add(b8);
pan.add(b9);
pan.add(b0);
pan.add(bplus);
pan.add(bequal);
pan.add(bminus);
pan.add(bdivide);
pan.add(breset);
ova.add("Center",pan);
JFrame jf = new JFrame("Calculator");
jf.setContentPane(ova);
jf.setVisible(true);
jf.setSize(500,500);
CalEngine calcEngine = new CalEngine(this);
b0.addActionListener(calcEngine);
b1.addActionListener(calcEngine);
b2.addActionListener(calcEngine);
b3.addActionListener(calcEngine);
b4.addActionListener(calcEngine);
b5.addActionListener(calcEngine);
b6.addActionListener(calcEngine);
b7.addActionListener(calcEngine);
b8.addActionListener(calcEngine);
b9.addActionListener(calcEngine);
bplus.addActionListener(calcEngine);
bequal.addActionListener(calcEngine);
bminus.addActionListener(calcEngine);
bdivide.addActionListener(calcEngine);
breset.addActionListener(calcEngine);
}
public static void main(String[] args)
{
new Calculator();
}
public void setScreen(String wee) {
screen.setText(wee);
}
public String getScreen() {
return screen.getText();
}
}
答案 0 :(得分:5)
以下是有问题的代码行:
parent.setScreen(equalnum.valueOf(totalnum));
您在String对象(valueOf
)的实例上调用静态方法equalnum
。你应该在课堂上调用它。
parent.setScreen(String.valueOf(totalnum));