import javaawteventActionEvent;
import javaawteventActionListener;
import javaawtContainer;
import javaawtFlowLayout;
import javaxswingJFrame;
import javaxswingJTextField;
import javaxswingJButton;
public class Computer {
/**
* @param args
*/
private static int numClicks=;
public static void main(String[] args) {
// TODO Autogenerated method stub
final JFrame jf=new JFrame(計算器);
final Container c=jfgetContentPane();
csetLayout(new FlowLayout(FlowLayoutCENTER));
final JTextField f=new JTextField();
final JTextField f=new JTextField();
final JTextField f=new JTextField();
final JButton b=new JButton(+);//內部類如果要使用外部類的變量必須使用final修飾符
final JButton b=new JButton(=);
baddActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
numClicks++;
switch(numClicks%)
{
case :bsetText(+);break;
case :bsetText();break;
case :bsetText(*);break;
case :bsetText(/);break;
}
}
});
baddActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
int n=IntegerparseInt(fgetText());
int n=IntegerparseInt(fgetText());
int n=n+n;
switch(numClicks%)
{
case :n=n+n;break;
case :n=nn;break;
case :n=n*n;break;
case :n=n/n;break;
}
fsetText(StringvalueOf(n));
}
});
cadd(f);
cadd(b);
cadd(f);
cadd(b);
cadd(f);
jfsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
jfsetSize( );
jfsetVisible(true);
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26463.html