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();
    }
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: