您好,登錄后才能下訂單哦!
這篇文章主要介紹“Java如何實現Windows計算器界面”,在日常操作中,相信很多人在Java如何實現Windows計算器界面問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java如何實現Windows計算器界面”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
前一段時間在學校簡單的學習了Java,對Java這門我第一次接觸的面向對象語言有了一定的了解。
一直沒時間整理寫的東西,今天打算整理下上傳下。
Java相對于C來說比較方便的就是用來寫界面(PS:但其實Java寫的界面也是十分的“有年代感”)
之前學到寫界面時的練手程序就是這個計算器界面
程序其實比較簡單,主要就是練習界面的排布等
界面:
代碼:
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class CalTest extends JFrame { //相關的二級容器和控件聲明一下 JTextField showtext;//文本框 JButton bt1; JButton bt2; JButton bt3; JButton bt4;//四個獨立的按鈕 JPanel panel1;//面板1(25個按鈕) JPanel panel2;//面板2(20個按鈕) JPanel panel3;//面板3(4個按鈕) JPanel panel4;//三個單選框 JButton[] bts1;//左邊25個按鈕的數組 JButton[] bts2;//右邊20個按鈕的數組 JButton[] bts3;//4個按鈕的數組 JRadioButton[] bts4;//3個單選 public CalTest() { init(); } //初始化窗體及控件的方法 private void init() { this.setSize(760,370); this.setLayout(null); this.setVisible(true); showtext=new JTextField(); showtext.setLocation(10, 10); showtext.setSize(720, 50); this.add(showtext); panel1=new JPanel(); panel1.setSize(360, 190); panel1.setLocation(10, 110); panel1.setLayout(new GridLayout(5,5,3,3));//設置面板1 網格布局,4行1列 bts1=new JButton[25]; String[] strs1={" ","Inv","In","(",")","Int","sinh","sin","x^2","n!","dms","cosh","cos","x^y","y√x","派","tanh","tan","x^3","3√x","F-E","Exp","Mod","log","10^x"}; for(int i=0;i<strs1.length;i++) { bts1[i]=new JButton(strs1[i]); panel1.add(bts1[i]); } this.add(panel1); panel2=new JPanel(); panel2.setSize(260, 190); panel2.setLocation(400, 70); panel2.setLayout(new GridLayout(5,4,3,3));//設置面板2 網格布局,5行4列 bts2=new JButton[20]; String[] strs2={"MC","MR","MS","M+","←","CE","C","+/","7","8","9","/","4","5","6","*","1","2","3","-"}; for(int i=0;i<strs2.length;i++) { bts2[i]=new JButton(strs2[i]); panel2.add(bts2[i]); } this.add(panel2); panel3=new JPanel(); panel3.setSize(55, 150); panel3.setLocation(675, 70); panel3.setLayout(new GridLayout(4,1,3,3));//設置面板3 網格布局,4行1列 bts3=new JButton[4]; String[] strs3={"M-","√","%","1/x"}; for(int i=0;i<strs3.length;i++) { bts3[i]=new JButton(strs3[i]); panel3.add(bts3[i]); } this.add(panel3); panel4=new JPanel(); panel4.setSize(50, 50); panel4.setLocation(20, 80); panel4.setLayout(new GridLayout(1,3,3,3));//設置面板3 網格布局,4行1列 bts4=new JRadioButton[3]; String[] strs4={"度","弧度","梯度"}; for(int i=0;i<strs4.length;i++) { bts4[i]=(JRadioButton) new JRadioButton(strs4[i]); panel4.add(bts4[i]); } this.add(panel4); bt1=new JButton("0"); bt1.setSize(127, 35); bt1.setLocation(400,263); this.add(bt1); bt2=new JButton("."); bt2.setSize(62, 35); bt2.setLocation(530,263); this.add(bt2); bt3=new JButton("+"); bt3.setSize(62, 35); bt3.setLocation(595,263); this.add(bt3); bt4=new JButton("="); bt4.setSize(55, 75); bt4.setLocation(675,223); this.add(bt4); } } public class Test { public static void main(String[] args) { CalTest cal=new CalTest(); } }
到此,關于“Java如何實現Windows計算器界面”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。