版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、實驗四 GUI圖形界面設(shè)計一 實驗要求 1、掌握Awt與Swing的區(qū)別與聯(lián)系;掌握Swing常用圖像組件的使用;掌握主要的布局管理器的使用方法;2、了解事件處理機制;掌握Swing常用圖像組件的使用;掌握固定菜單和彈出式菜單的創(chuàng)建和使用;二 實驗內(nèi)容1、設(shè)計并實現(xiàn)一個類似于windows操作系統(tǒng)附件中自帶的計算器的一款簡單的計算器,要求界面美觀,設(shè)計合理;帶有常用的菜單并實現(xiàn)其功能;能完成加、減、乘、除等簡單的計算,在下面寫出其核心代碼。(1)源代碼package com.cal;import java.awt.*;import javax.swing.*;import java.awt.e
2、vent.*;import java.util.Stack;import java.awt.GridLayout;class Cal extends JFrame implements ActionListener JTextField show=null;JPanel jp1,jp2;JButton c,jia,jian,cheng,chu,equ,point;/按鈕 清空 加,減,乘 ,除,等號,小數(shù)點 JButton b = new JButton10;/按鈕數(shù)組 0-9 /棧控制標(biāo)記位 int flag=1; /結(jié)果標(biāo)記位 int reflag=0; public Cal() /創(chuàng)建組
3、件jp1=new JPanel();jp2=new JPanel();show=new JTextField(45);show.setFont(new Font("宋體",Font.PLAIN,16);jia=new JButton(" + ");jian=new JButton(" - ");cheng=new JButton(" * ");chu=new JButton(" / ");equ=new JButton(" = ");equ.setForeground(Col
4、or.RED);equ.setBackground(Color.GREEN);point=new JButton(" . ");b0=new JButton("0");b1=new JButton("1");b2=new JButton("2");b3=new JButton("3");b4=new JButton("4");b5=new JButton("5");b6=new JButton("6");b7=new JButton(&
5、quot;7");b8=new JButton("8");b9=new JButton("9");c=new JButton(" C ");c.setForeground(Color.RED);c.setBackground(Color.YELLOW);c.setFont(new Font("黑體",Font.PLAIN,15);/添加組件jp1.add(show);jp1.add(c);/show.setEditable(false);jp2.add(b7);jp2.add(b8);jp2.add(b9
6、);jp2.add(jia);jp2.add(b4);jp2.add(b5);jp2.add(b6);jp2.add(jian);jp2.add(b1);jp2.add(b2);jp2.add(b3);jp2.add(cheng);jp2.add(equ);jp2.add(b0);jp2.add(point);jp2.add(chu);jp2.setLayout(new GridLayout(4,4,3,3);/注冊監(jiān)聽show.addActionListener(this);jia.addActionListener(this);jian.addActionListener(this);ch
7、eng.addActionListener(this);chu.addActionListener(this);equ.addActionListener(this);point.addActionListener(this);c.addActionListener(this);for(int i=0;i<10;i+) bi.addActionListener(this);/設(shè)置布局管理器this.add(jp1,BorderLayout.NORTH);this.add(jp2);/設(shè)置窗體屬性this.setTitle(" 簡 易 計 算 器");this.setS
8、ize(500, 300);this.setLocation(500,200);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/顯示窗體this.setVisible(true);Overridepublic void actionPerformed(ActionEvent e)if(reflag = 1 && e.getSource()!=equ)show.setText("");if(e.getSource() = b0)show.setText(show.getText()+"0&qu
9、ot;);flag=0;reflag=0;else if(e.getSource() = b1)show.setText(show.getText()+"1");flag=0;reflag=0;else if(e.getSource() = b2)show.setText(show.getText()+"2");flag=0;reflag=0;else if(e.getSource() = b3)show.setText(show.getText()+"3");flag=0;reflag=0;else if(e.getSource()
10、 = b4)show.setText(show.getText()+"4");flag=0;reflag=0;else if(e.getSource() = b5)show.setText(show.getText()+"5");flag=0;reflag=0;else if(e.getSource() = b6)show.setText(show.getText()+"6");flag=0;reflag=0;else if(e.getSource() = b7)show.setText(show.getText()+"7&
11、quot;);flag=0;reflag=0;else if(e.getSource() = b8)show.setText(show.getText()+"8");flag=0;reflag=0;else if(e.getSource() = b9)show.setText(show.getText()+"9");flag=0;reflag=0;/操作符 處理else if(e.getSource() = jia)show.setText(show.getText()+"+");flag=1;reflag=0;else if(e.g
12、etSource() = jian)show.setText(show.getText()+"-");flag=1;reflag=0;else if(e.getSource() = cheng)show.setText(show.getText()+"*");flag=1;reflag=0;else if(e.getSource() = chu)show.setText(show.getText()+"/");flag=1;reflag=0;else if(e.getSource() = point) /小數(shù)點 只能按一次show.s
13、etText(show.getText()+".");flag=1;reflag=0;else if(e.getSource() = c) show.setText("");flag=1;reflag=0;/計算結(jié)果并輸出else String s= show.getText();if(s.charAt(s.length()-1)='/' |s.charAt(s.length()-1)='*'|s.charAt(s.length()-1)='+'|s.charAt(s.length()-1)='-&
14、#39;)show.setText("符號不能放最后");else if(s.charAt(s.length()-1)='.' && s.charAt(s.length()-2)='.')show.setText("points are boom");elseshow.setText("="+SetSum(s);reflag=1;flag=1;private double SetSum(String st)if(st.trim()="") return 0;/實例化用
15、來存放操作數(shù)和操作符的棧并清空Stack<Object> SNum=new Stack();Stack<Object> SOp=new Stack();SNum.removeAllElements();SOp.removeAllElements();String Snum=""double Inum=0;char a1 = st.charAt(st.length()-1);char b1 = st.charAt(st.length()-2);for(int i =0;i<st.length();i+)char c = st.charAt(i);
16、if(c='+'|c='-')Inum=Double.parseDouble(Snum);if(SOp.empty()SNum.push(Inum);elseswitch(char)SOp.peek()case '+':SNum.push(double)SNum.pop()+Inum);SOp.pop();break;case '-':SNum.push(double)SNum.pop()-Inum);SOp.pop();break;case '*':SNum.push(double)SNum.pop()*Inum
17、);SOp.pop();break;case '/':SNum.push(double)SNum.pop()/Inum);SOp.pop();break;/將符號壓入棧switch(c)case '+':SOp.push('+');break;case '-':SOp.push('-');break;Snum=""else if(c='*'|c='/')Inum=Double.parseDouble(Snum);if(SOp.empty()SNum.push(In
18、um);else if(a1='0' && b1='.') /如果是整數(shù),則去掉后面的小數(shù)點和0show.setText(String.valueOf(Math.round(Inum);switch(char)SOp.peek()case '*':SNum.push(double)SNum.pop()*Inum);SOp.pop();break;case '/':SNum.push(double)SNum.pop()*Inum);SOp.pop();break;default:SNum.push(Inum);bre
19、ak; /將符號壓入符號棧 switch (c) case '*': SOp.push('*'); break; case '/': SOp.push('/'); break; Snum="" else Snum=Snum+c; SNum.push(Double.parseDouble(Snum); /將表達(dá)式最后一個數(shù)入棧 double a,b; while(!SOp.empty() a=(double)SNum.pop(); b=(double)SNum.pop(); switch(char)SOp.pop() case '+': SNum.push(b+a); break; case '-': SNum.push(b-a); break; case '*': SNum.push(b*a); break; case '/': SNum.push(b/a); break; return (double)SNum.pop(); public class Calculter public
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 九年級化學(xué)下冊 8.3《金屬資源的利用和保護》教案 (新版)新人教版
- 八年級英語下冊 Unit 6 An old man tried to move the mountains說課稿(新版)人教新目標(biāo)版
- 高中數(shù)學(xué) 第三章 不等式 3.2.2 一元二次不等式的應(yīng)用教案 北師大版必修5
- 八年級地理下冊 9.2高原濕地-三江源地區(qū)教案設(shè)計 (新版)新人教版
- 學(xué)校紙張購銷合同模板
- 簽約合同模板建筑
- 糖漿加工合同模板
- 企業(yè)單位消防安全規(guī)范化管理指導(dǎo)手冊第8章
- 2023年中、大功率激光器資金申請報告
- 測繪資質(zhì)合同模板
- 滿堂腳手架安全技術(shù)交底
- 2023建筑業(yè)10項新技術(shù)
- 美甲藝術(shù)全套教學(xué)課件
- 小學(xué)生主題班會誠實守信言行一致
- 井蓋抬高施工方案
- 商場消防設(shè)計規(guī)范
- 小學(xué)數(shù)學(xué)蘇教三年級上《兩、三位數(shù)乘一位連續(xù)進位》教學(xué)反思
- 骨質(zhì)疏松骨質(zhì)疏松癥
- 語文學(xué)科思政元素
- 常用酸堿密度質(zhì)量分?jǐn)?shù)摩爾濃度對照表及換算方法
- 漢服店鋪商業(yè)計劃書
評論
0/150
提交評論