您好,登錄后才能下訂單哦!
這篇文章主要介紹“Java如何實(shí)現(xiàn)Windows計(jì)算器界面”,在日常操作中,相信很多人在Java如何實(shí)現(xiàn)Windows計(jì)算器界面問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Java如何實(shí)現(xiàn)Windows計(jì)算器界面”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
前一段時(shí)間在學(xué)校簡(jiǎn)單的學(xué)習(xí)了Java,對(duì)Java這門(mén)我第一次接觸的面向?qū)ο笳Z(yǔ)言有了一定的了解。
一直沒(méi)時(shí)間整理寫(xiě)的東西,今天打算整理下上傳下。
Java相對(duì)于C來(lái)說(shuō)比較方便的就是用來(lái)寫(xiě)界面(PS:但其實(shí)Java寫(xiě)的界面也是十分的“有年代感”)
之前學(xué)到寫(xiě)界面時(shí)的練手程序就是這個(gè)計(jì)算器界面
程序其實(shí)比較簡(jiǎn)單,主要就是練習(xí)界面的排布等
界面:
代碼:
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class CalTest extends JFrame { //相關(guān)的二級(jí)容器和控件聲明一下 JTextField showtext;//文本框 JButton bt1; JButton bt2; JButton bt3; JButton bt4;//四個(gè)獨(dú)立的按鈕 JPanel panel1;//面板1(25個(gè)按鈕) JPanel panel2;//面板2(20個(gè)按鈕) JPanel panel3;//面板3(4個(gè)按鈕) JPanel panel4;//三個(gè)單選框 JButton[] bts1;//左邊25個(gè)按鈕的數(shù)組 JButton[] bts2;//右邊20個(gè)按鈕的數(shù)組 JButton[] bts3;//4個(gè)按鈕的數(shù)組 JRadioButton[] bts4;//3個(gè)單選 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));//設(shè)置面板1 網(wǎng)格布局,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));//設(shè)置面板2 網(wǎng)格布局,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));//設(shè)置面板3 網(wǎng)格布局,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));//設(shè)置面板3 網(wǎng)格布局,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(); } }
到此,關(guān)于“Java如何實(shí)現(xiàn)Windows計(jì)算器界面”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。