直接上源碼:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//<APPLET CODE="Calculator.class" WIDTH="200" HEIGHT="200">
//</APPLET>
 
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
 
public class Calculator extends java.applet.Applet implements ActionListener
{
	//界面組件
	private Button m_btnNum[]=new Button[10];
	private Button m_btnAdd=new Button("+");
	private Button m_btnSub=new Button("-");
	private Button m_btnMul=new Button("*");
	private Button m_btnDiv=new Button("/");
	private Button m_btnEqu=new Button("=");
	private Button m_btnClr=new Button("clr");
	private TextField m_textDisplay=new TextField(20);
	private String m_strInfo=new String();
	private int i;
 
	//界面布局,加入事件監聽器
	public void init()
	{
		setName("計算器");
		for(i=0;i<=9;i++)
		{
			m_btnNum[i]=new Button(/*"%d",i*/Integer.toString(i));
		}
		setLayout(new BorderLayout());
		add("North",m_textDisplay);
		m_textDisplay.setEditable(false);
		Panel panelBody=new Panel();
		panelBody.setLayout(new GridLayout(4,4));
		for(i=1;i<=9;i++)
		{
			panelBody.add(m_btnNum[i]);
			m_btnNum[i].addActionListener(this);
		} 
		panelBody.add(m_btnNum[0]);m_btnNum[0].addActionListener(this);
		panelBody.add(m_btnClr);m_btnClr.addActionListener(this);
		panelBody.add(m_btnEqu);m_btnEqu.addActionListener(this);
		panelBody.add(m_btnAdd);m_btnAdd.addActionListener(this);
		panelBody.add(m_btnSub);m_btnSub.addActionListener(this);
		panelBody.add(m_btnMul);m_btnMul.addActionListener(this);
		panelBody.add(m_btnDiv);m_btnDiv.addActionListener(this);
		add("Center",panelBody);
		setSize(200,200);
 
	}
	public void start()
	{
 
	}
 
	//將按鈕的信息傳到顯示面板,響應按鈕命令
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==m_btnClr)
			m_strInfo="";
		else if(e.getSource()==m_btnAdd)
			m_strInfo+="+";         
		else if(e.getSource()==m_btnSub)
			m_strInfo+="-";
		else if(e.getSource()==m_btnMul)
			m_strInfo+="*";
		else if(e.getSource()==m_btnDiv)
			m_strInfo+="/";
		else if(e.getSource()==m_btnEqu)
		{
			HandleEqu();
			return;
		}
		else 
		{
			String strTemp=e.getSource().toString();
			int nLen=strTemp.lastIndexOf("=");
			m_strInfo+=strTemp.charAt(nLen+1);
		}
		m_textDisplay.setText(m_strInfo);
 
	}
 
	//按下等號後的處理事件
	public void HandleEqu()
	{
		try
		{
			String strTemp;
			strTemp=ToTailString(m_strInfo);
			strTemp=CalTailString(strTemp);
			m_strInfo+="=";
			m_strInfo+=strTemp;
			m_textDisplay.setText(m_strInfo);
			m_strInfo="";
		}
		catch(Exception e)
		{
			m_strInfo="";
			System.out.println(e.toString());
			m_textDisplay.setText("ERROR");
		}
 
	}
 
	//轉化為後綴表達式
	private String ToTailString (String strOrignal)throws Exception
	{
 
		int top=-1;
		char []Exp=new char[200];
		char []St=new char[200];
		char ch;
		int i=0,t=0;
		boolean bFlag=true;
		int nLen=strOrignal.length();
		if(nLen==0)
			return "";
 
		ch=strOrignal.charAt(i);
		i++;
		while(i<=nLen&&bFlag)
		{
 
			switch(ch)
			{
 
			case '+':
			case '-':
				while(top!=-1)
				{
					Exp[t]=St[top];
					top--;
					t++;
				}
				top++;
				St[top]=ch;
				break;
			case '*':
			case '/':
				while(top!=-1 && (St[top]=='*' || St[top]=='/') )
				{
					Exp[t]=St[top];
					top--;
					t++;
				}
				top++;
				St[top]=ch;
				break;
			default:
				while(ch>='0' && ch<='9')
				{
					Exp[t]=ch; 
					t++;
					if(i>=nLen){bFlag=false;break;}
					ch=strOrignal.charAt(i);
					i++;
				}
				i--;
				Exp[t]='#';
				t++;                
			}
			ch=strOrignal.charAt(i); 
			i++;
			if(i>nLen){bFlag=false;break;}
		}
 
		while(top!=-1)
		{
			Exp[t]=St[top];
			t++;
			top--;
		}
 
		String str1="";
		for(int k=0;k<t;k++)str1+=Exp[k];
		return str1;
	}
 
	//根據後綴表達式求解相應的值
	private String CalTailString(String strTail)throws Exception
	{
		float St[]=new float[200];
		int top=-1,nLen=strTail.length();
		char ch;
		int t=0;
		boolean bFlag=true;
		ch=strTail.charAt(t);
		t++;
		while(t<=nLen&&bFlag)
		{
			switch(ch)
			{
			case '+':
 
				St[top-1]=St[top-1]+St[top]; 
				top--;
				break;
			case '-':
				St[top-1]=St[top-1]-St[top];
				top--;
				break;
			case '*':
				St[top-1]=St[top-1]*St[top];
				top--;
				break;
			case '/':
				St[top-1]=St[top-1]/St[top];
				top--;
				break;
			default:
				int d=0;
				while(ch>='0' && ch<='9')
				{
					d=10*d+(int)(ch-'0');
					if(t>=nLen){bFlag=false;break;}
					ch=strTail.charAt(t);       
					t++;            
				}
				top++;
				St[top]=d;
				break;
			}
			if(t>=nLen)
			{
				bFlag=false;
				break;
			}
			ch=strTail.charAt(t); 
			t++;
 
		}
		return Float.toString(St[0]);
	}
}

Ubuntu8.04 + Sun JAVA6環境
編譯:
javac Calculator.java

運行結果:
appletviewer Calculator.java

感言:
Java圖形界面只能用純代碼來寫,比較痛苦。另外本程序計算後綴表達式的算法在《數據結構》課程中已經練習過,雖然我還是不太熟。。。