1.使用Java圖形界面組件設(shè)計軟件,界面如圖所示。

2.軟件能夠滿足基本的“加、減、乘、除”等運算要求。
3.程序代碼清晰,語法規(guī)范,結(jié)構(gòu)合理,邏輯正確。
效果圖:
先分析,計算器大概是由三個大部分組成的:菜單欄,顯示框,按鈕。
所以定義一個類cal繼承JFrame。
class cal extends JFrame {
private JPanel p1, p2;
private JTextArea show;
private String box ;
JMenuBar menubar;//菜單
JMenu menu1, menu2, menu3;//菜單
StringBuffer strA;//用來存放用戶輸入的第一個數(shù)字
StringBuffer strB;//用來存放用戶輸入的第二個數(shù)字
char oper ='~';//初始化操作符,可以隨便初始化一個特殊符號,這里只是用來區(qū)分的
double A;
double B;
private String[] text2 = {"C", "CE","%", "/",
"7", "8", "9", "*",
"4", "5", "6", "-",
"1", "2", "3", "+",
"DEL","0", ".", "="};//計算器按鈕面板
private JButton[] munButton = new JButton[text2.length];
}我們定義完后開始初始化。
class cal extends JFrame implements ActionListener {
private JPanel p1, p2;
private JTextArea show;
private String box ;
JMenuBar menubar;
JMenu menu1, menu2, menu3;
StringBuffer strA;
StringBuffer strB;
char oper ='~';
double A;
double B;
private String[] text2 = {"C", "CE","%", "/",
"7", "8", "9", "*",
"4", "5", "6", "-",
"1", "2", "3", "+",
"DEL","0", ".", "="};//計算器按鈕面板
private JButton[] munButton = new JButton[text2.length];
public cal() {
p1 = new JPanel();
p2 = new JPanel();
show = new JTextArea();
p1.setSize(600, 100);
menubar = new JMenuBar();
menu1 = new JMenu("查看(V)");
menu2 = new JMenu("編輯(E)");
menu3 = new JMenu("幫助(H)");
strB=new StringBuffer();
strA=new StringBuffer();
}
public void init() {//初始化
this.setTitle("計算器");//設(shè)置名稱
this.setBounds(200, 200, 320, 300);
this.setLayout(new BorderLayout());//設(shè)置布局
this.add(p1, BorderLayout.CENTER);
this.add(p2, BorderLayout.SOUTH);
menubar.add(menu1);
menubar.add(menu2);
menubar.add(menu3);
this.setJMenuBar(menubar);
this.setLocationRelativeTo(null);//放置在屏幕中央
this.setResizable(false);//固定大小,用戶不能調(diào)整大小
show.setPreferredSize(new Dimension(300, 100));
p1.add(show);
p2.setLayout(new GridLayout(5, 4, 2, 3));
//添加數(shù)字按鍵
for (int i = 0; i< text2.length; i++) {
munButton[i] = new JButton(text2[i] + " ");
p2.add(munButton[i]);
}
for (int i = 0; i< munButton.length; i++)
munButton[i].addActionListener(this);
this.setVisible(true);//窗體可視化
this.addWindowListener(new WindowAdapter() {//監(jiān)聽事件,當按下關(guān)閉按鈕時,結(jié)束程序
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}接著我們就開始進入到這個項目的最重要的部分了。
設(shè)置按鈕監(jiān)聽事件,通過獲取按鈕的信息來進行判斷運算。
在我們進行加減乘除計算的時候會出現(xiàn)一個特殊情況,除數(shù)為0,所以我們要預(yù)防出現(xiàn)異常影響程序的運行,我們就要進行異常的捕獲處理,這里我是自定義了一個異常類munber_Exception,然后我們利用try{}catch{}語句來進行異常捕獲和處理。
public double division(double x,double y)throws munber_Exception{
if(y==0){
throw new munber_Exception("除數(shù)不能為0!");
}
else{
return x/y;
}
}
@Override
public void actionPerformed(ActionEvent e) {
try { String act=e.getActionCommand();//這個方法返回的是事件源組件的“命令” , 這個“命令” 實際上就是事件源組件上的“Label(標簽)字符串”,即如果我按了“9”這個按鈕他就會返回一個“9的值”
char a=act.charAt(0);//取act這個字符串的首字符
if (a=='0' ||a=='1' || a=='2' ||a=='3'||a=='4'||a=='5'||a=='6'||a=='7'||a=='8'||a=='9'||a=='.') {
if(oper=='~'){//當oper=='~'時,則操作符為空
strA.append(a);
show.setText(String.valueOf(strA));
}else {
strB.append(a);
show.setText(String.valueOf(strA)+oper+String.valueOf(strB));
}
}
else if(a=='+'||a=='-'||a=='/'||a=='*'||a=='%'){
oper=a;
show.setText(String.valueOf(strA)+oper);
}
else if(a=='='){
A = Double.parseDouble(String.valueOf(strA));
B = Double.parseDouble(String.valueOf(strB));
double j;
int len1=strA.length();
int len2=strB.length();
if (oper == '+') {
j = A + B;
show.setText(Double.toString(j));
strA.delete(0,len1);
strB.delete(0,len2);
strA.append(j);
} else if (oper == '-') {
j = A - B;
show.setText(Double.toString(j));
strA.delete(0,len1);
strB.delete(0,len2);
strA.append(j);
} else if (oper == '*') {
j = A * B;
show.setText(Double.toString(j));
strA.delete(0,len1);
strB.delete(0,len2);
strA.append(j);
} else if (oper == '/') {
try{j= division(A, B);}catch(munber_Exception u){
show.setText(u.shows());
}
}
else if (oper == '%') {
j = A % B;
show.setText(Double.toString(j));
strA.delete(0,len1);
strB.delete(0,len2);
strA.append(j);
}
} else if (a=='C') {//清除
show.setText(" ");
oper='~';
int len1=strA.length();
int len2=strB.length();
strA.delete(0,len1);
strB.delete(0,len2);
} else if (a=='D'){//刪除
if(oper!='~'){
if(strB.length()>0){
strB.delete(strB.length()-1,strB.length());
show.setText(String.valueOf(strA)+oper+String.valueOf(strB));
}
else
show.setText("0");
}if(oper=='~'){
if(strA.length()>0){
strA.delete(strA.length()-1,strA.length());
show.setText(String.valueOf(strA));
}
}
} }catch(ArithmeticException m){
System.out.println("除數(shù)不能為0");
}
}完整代碼如下:class munber_Exception extends Exception{ //異常處理
String e;
public munber_Exception(){
}
public munber_Exception(String message){
this.e=message;
}
public String shows(){
return e;
}
}
class cal extends JFrame implements ActionListener {
private JPanel p1, p2;
private JTextArea show;
private String box ;
JMenuBar menubar;
JMenu menu1, menu2, menu3;
StringBuffer strA;
StringBuffer strB;
char oper ='~';
double A;
double B;
private String[] text2 = {"C", "CE","%", "/",
"7", "8", "9", "*",
"4", "5", "6", "-",
"1", "2", "3", "+",
"DEL","0", ".", "="};//計算器按鈕面板
private JButton[] munButton = new JButton[text2.length];
public cal() {
p1 = new JPanel();
p2 = new JPanel();
show = new JTextArea();
p1.setSize(600, 100);
menubar = new JMenuBar();
menu1 = new JMenu("查看(V)");
menu2 = new JMenu("編輯(E)");
menu3 = new JMenu("幫助(H)");
strB=new StringBuffer();
strA=new StringBuffer();
}
public void init() {//初始化
this.setTitle("計算器");
this.setBounds(200, 200, 320, 300);
this.setLayout(new BorderLayout());
this.add(p1, BorderLayout.CENTER);
this.add(p2, BorderLayout.SOUTH);
menubar.add(menu1);
menubar.add(menu2);
menubar.add(menu3);
this.setJMenuBar(menubar);
this.setLocationRelativeTo(null);
this.setResizable(false);
show.setPreferredSize(new Dimension(300, 100));
p1.add(show);
p2.setLayout(new GridLayout(5, 4, 2, 3));
//添加數(shù)字按鍵
for (int i = 0; i< text2.length; i++) {
munButton[i] = new JButton(text2[i] + " ");
p2.add(munButton[i]);
}
for (int i = 0; i< munButton.length; i++)
munButton[i].addActionListener(this);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public double division(double x,double y)throws munber_Exception{
if(y==0){
throw new munber_Exception("除數(shù)不能為0!");
}
else{
return x/y;
}
}
@Override
public void actionPerformed(ActionEvent e) {
try { String act=e.getActionCommand();
char a=act.charAt(0);
if (a=='0' ||a=='1' || a=='2' ||a=='3'||a=='4'||a=='5'||a=='6'||a=='7'||a=='8'||a=='9'||a=='.') {
if(oper=='~'){
strA.append(a);
show.setText(String.valueOf(strA));
}else {
strB.append(a);
show.setText(String.valueOf(strA)+oper+String.valueOf(strB));
}
}
else if(a=='+'||a=='-'||a=='/'||a=='*'||a=='%'){
oper=a;
show.setText(String.valueOf(strA)+oper);
}
else if(a=='='){
A = Double.parseDouble(String.valueOf(strA));
B = Double.parseDouble(String.valueOf(strB));
double j;
int len1=strA.length();
int len2=strB.length();
if (oper == '+') {
j = A + B;
show.setText(Double.toString(j));
strA.delete(0,len1);
strB.delete(0,len2);
strA.append(j);
} else if (oper == '-') {
j = A - B;
show.setText(Double.toString(j));
strA.delete(0,len1);
strB.delete(0,len2);
strA.append(j);
} else if (oper == '*') {
j = A * B;
show.setText(Double.toString(j));
strA.delete(0,len1);
strB.delete(0,len2);
strA.append(j);
} else if (oper == '/') {
try{j= division(A, B);}catch(munber_Exception u){
show.setText(u.shows());
}
}
else if (oper == '%') {
j = A % B;
show.setText(Double.toString(j));
strA.delete(0,len1);
strB.delete(0,len2);
strA.append(j);
}
} else if (a=='C') {
show.setText(" ");
oper='~';
int len1=strA.length();
int len2=strB.length();
strA.delete(0,len1);
strB.delete(0,len2);
} else if (a=='D'){
if(oper!='~'){
if(strB.length()>0){
strB.delete(strB.length()-1,strB.length());
show.setText(String.valueOf(strA)+oper+String.valueOf(strB));
}
else
show.setText("0");
}if(oper=='~'){
if(strA.length()>0){
strA.delete(strA.length()-1,strA.length());
show.setText(String.valueOf(strA));
}
}
} }catch(ArithmeticException m){
System.out.println("除數(shù)不能為0");
}
}
}你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
文章題目:java簡易計算器-創(chuàng)新互聯(lián)
路徑分享:http://www.chinadenli.net/article42/dgesec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、外貿(mào)網(wǎng)站建設(shè)、Google、網(wǎng)站維護、電子商務(wù)、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容