Java计算器1.0版

此版本只是设计了页面,还没有实现事件监听
 
代码:
package com.niit.javagui;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * @author: Annie
 * @date:2016年5月30日
 * @description:
 */
public class CalculatorDemo  extends JFrame{

    public static void main(String[] args) {
        new CalculatorDemo();

    }


    Box baseBox,box1 ,box2,box3,box4,box2and3,box5;
    GridLayout grid;
    JTextField textFileld;
    JTextArea textArea;
    JPanel jp;
    Button btn1,btn2,btn3,btn4,btn5,btn6;

    public CalculatorDemo(){
        setVisible(true);
        setSize(200, 200);
        setTitle("计算器");
        
        instantiationObject();
        addFunction();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public void  instantiationObject(){
        baseBox = Box.createVerticalBox();
        box1 = Box.createHorizontalBox();
        
        box2 = Box.createHorizontalBox();
        jp = new JPanel();
        
        grid = new GridLayout(4,3);
        jp.setLayout(grid);
        Button [][] btn = new Button[4][3];
        btn[0][0] = new Button("CE");
        btn[0][1] = new Button("/");
        btn[0][2] = new Button("*");
        jp.add(btn[0][0]);
        jp.add(btn[0][1]);
        jp.add(btn[0][2]);
        int i,j;
        for ( i = 1; i <4 ; i++) {
            for ( j = 0; j < 3; j++) {
                if(i == 1){
                    btn[i][j] = new Button(btn.length+3+j+"");
                    jp.add(btn[i][j]);
                    
                }if(i == 2){
                    btn[i][j] = new Button(btn.length+j+"");
                    jp.add(btn[i][j]);
                }
                if(i == 3){
                    btn[i][j] = new Button(btn.length-3+j+"");
                    jp.add(btn[i][j]);
                }
            }
            
           
        }
        
        box3 = Box.createHorizontalBox();
        btn1 = new Button("0");
        btn2 = new Button(".");
        
        box2and3 = Box.createVerticalBox();
        
        box4 = Box.createVerticalBox();
        btn3 = new Button("-");
        btn4 = new Button("+");
        btn5 = new Button("-");
        btn6 = new Button("=");
        
        box5 = Box.createHorizontalBox();
    }

    public void addFunction(){
        box1.add( new JTextField(10));
        box2.add(jp);    
        box3.add(btn1);
        box3.add(btn2);
        box2and3.add(box2);
        box2and3.add(box3);
        
        box4.add(btn3);
        box4.add(btn4);
        box4.add(btn5);
        box4.add(btn6);
        
        box5.add(box2and3);
        box5.add(box4);
        
        
        baseBox.add(box1);
        baseBox.add(box5);
        add(baseBox);


    }

}

 

 
效果图:
 

转载于:https://www.cnblogs.com/wangmingxia/p/5550368.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值