各位大神好,我想利用JTextFeild來輸入數字,再用JButton來進行程式運算,
再將計算結果顯示在另外一個JTextFeild上,但一直無法如我想像的寫出,
compiler有過,但一直無法順利進行,我的程式碼如下,可以幫我看看嗎 ˊ ˋ
感謝大神們!
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;
public class App7202 extends JApplet implements ActionListener {
JTextField number1,formula,number2,total;
JButton b1;
int n1=0;
int n2=0;
double answer=0;
String x;
DecimalFormat F = new DecimalFormat( "0.00" );
public void init(){
number1 = new JTextField(6);
formula = new JTextField(2);
number2 = new JTextField(6);
total = new JTextField(6);
total.setEditable( false );
b1 = new JButton("=(按我)");
b1.addActionListener(this);
Container c =getContentPane();
FlowLayout fl = new FlowLayout();
c.setLayout(fl);
c.add(number1);c.add(formula);c.add(number2);c.add(b1);c.add(total);
}
public void actionPerformed( ActionEvent e )
{
Fromula();
}
public void Fromula(){
x = formula.getText();
n1 = Integer.parseInt(number1.getText());
n2 = Integer.parseInt(number2.getText());
if (x=="+"){
answer=n1+n2;
total.setText( String.valueOf ((answer)) );
}
else if (x=="-"){
answer=n1-n2;
total.setText( String.valueOf ((answer)) );
}
else if (x=="*"){
answer=n1*n2;
total.setText( String.valueOf ((answer)) );
}
else if (x=="/"){
answer=(double)n1/n2;
total.setText( String.valueOf (F.format(answer)) );
}
else if (x=="^"){
answer=Math.pow(n1, n2);
total.setText( String.valueOf (F.format(answer)) );
}
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 36.238.182.69