/***************************************** 將UI20改成Applet與Application兩用版本 *************************************************/ import java.awt.*; import java.awt.event.*; import javax.swing.*; //由JFrame改成 JApplet public class ui40 extends JApplet implements ActionListener { JButton bot[]=new JButton[10]; JButton bot_dt=new JButton("."); JButton bot_a=new JButton("+"); JButton bot_s=new JButton("-"); JButton bot_m=new JButton("*"); JButton bot_d=new JButton("/"); JButton bot_e=new JButton("="); JButton bot_c=new JButton("C"); JButton bot_off=new JButton("OFF"); JTextField txt; Container c; JPanel jpl=new JPanel(); JScrollPane jscp=new JScrollPane(jpl); String name; double A=0,B=0; int op=0,dot=0; public void init() //由建構元改成 public void init() { //super("UI 版面配置"); 不用super了 int i; for (i=0;i<10;i=i+1) { bot[i]=new JButton(Integer.toString(i)); } txt=new JTextField(10); txt.setEditable(false);//設定成不能修改 txt.setHorizontalAlignment(JTextField.RIGHT);//靠右對齊 c=getContentPane(); c.setLayout(new BorderLayout());//設定為用Borderlayout jpl.setLayout(new GridLayout(5,4));//設定為用gridlayout c.add(txt,BorderLayout.NORTH); c.add(jscp,BorderLayout.CENTER);//用scrollpane //注意加入元件的順序 for (i=0;i<10;i=i+1) { jpl.add(bot[i]); bot[i].addActionListener(this); } jpl.add(bot_dt); bot_dt.addActionListener(this); jpl.add(bot_a); bot_a.addActionListener(this); jpl.add(bot_s); bot_s.addActionListener(this); jpl.add(bot_m); bot_m.addActionListener(this); jpl.add(bot_d); bot_d.addActionListener(this); jpl.add(bot_e); bot_e.addActionListener(this); jpl.add(bot_c); bot_c.addActionListener(this); jpl.add(bot_off); bot_off.addActionListener(this); //setSize(200,280); size不能set了,也不用show() //show(); } public void paint(Graphics g) { super.paint(g); } /***按鈕事件的傾聽方法****/ public void actionPerformed(ActionEvent e) //按鈕事件的處理方法 { int i; for (i=0;i<10;i=i+1) { if (e.getSource()==bot[i]) { if (txt.getText().equals("0")) //強化0的處理 txt.setText(Integer.toString(i)); else txt.setText(txt.getText()+Integer.toString(i)); try { A=Double.parseDouble(txt.getText()); } catch(NumberFormatException ee){ //處理按錯鍵 A=0; txt.setText("Err!"); } return; } } if (e.getSource()==bot_a) { txt.setText("0"); B=A; A=0; op=1;//add dot=0; } else if (e.getSource()==bot_s) { txt.setText("0"); B=A; A=0; op=2;//減 dot=0; } else if (e.getSource()==bot_m) { txt.setText("0"); B=A; A=0; op=3;//乘 dot=0; } else if (e.getSource()==bot_d) { txt.setText("0"); B=A; A=0; op=4;//除 dot=0; } else if (e.getSource()==bot_e) { txt.setText("="); if (op==0) A=B; else if (op==1) A=B+A; else if (op==2) A=B-A; else if (op==3) A=B*A; else if (op==4) A=B/A; txt.setText(Double.toString(A)); dot=0; } else if (e.getSource()==bot_dt) { txt.setText(txt.getText()+"."); dot=1; } else if (e.getSource()==bot_c) { txt.setText("0"); dot=0; A=0; B=0; } else if (e.getSource()==bot_off) { //System.exit(0); 不能offㄟ!因為Applet不能隨便跳出去 } } public static void main(String args[]) //程式起點 { ui40 app=new ui40(); //畫圖 JFrame f = new JFrame("兩用程式"); // To close the application: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(app);//把Applet 加進去 f.setSize(400,400); app.init();//呼叫init() app.start();//呼叫start() f.setVisible(true); } }