亲宝软件园·资讯

展开

Java计算器界面 Java实现Windows计算器界面

华墨1024 人气:0
想了解Java实现Windows计算器界面的相关内容吗,华墨1024在本文为您仔细讲解Java计算器界面的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java,计算器,下面大家一起来学习吧。

前一段时间在学校简单的学习了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();
    }
}

加载全部内容

相关教程
猜你喜欢
用户评论