这篇文章主要介绍“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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。