此版本只是设计了页面,还没有实现事件监听
 
代码:
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);


    }

}

 

 
效果图:
 
posted on 2016-06-01 17:24  王铭霞  阅读(195)  评论(0编辑  收藏  举报