//import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Box; import java.awt.GridLayout;
import java.awt.BorderLayout; import java.awt.Dimension;
import java.awt.event.*; public class myCalculator{
public static void main(String[] args){
CalculatorJFrame calculatorJFrame = new CalculatorJFrame(); }
} /* window for the calculator */
class CalculatorJFrame extends JFrame{
CalculatorJPanel calculatorJPanel; CalculatorJFrame(){
calculatorJPanel = new CalculatorJPanel(); //add(calculatorJPanel,BorderLayout.NORTH); add(calculatorJPanel); setTitle("计算器");
//setResizable(false);
setBounds(100,100,300,500);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); }
} /* panel for the calculator*/
class CalculatorJPanel extends JPanel implements ActionListener{
static final int SPLIT_LEN = 8; Box mainBaseBox; Box upBaseBox;
Box upLeftVerticalBox;
Box upRightVerticalBox; Box downBaseBox;
JPanel downLeftJPanel;
Box downLeftVerticalBox;
Box downChildrenLeftHorizontalBox; Box downRightVerticalBox; //For up
JLabel[] label;
String[] labelStr = {"操作数一","操作类型","操作数二","计算结果",""};
JTextField[] textField; //For down
String[] buttonStr = {"7","8","9","/","4","5","6","*","1","2","3","-","0",".","+","C","="};
JButton[] btn; /* Override */
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
if( source instanceof JButton){
System.out.println(((JButton)source).getText());
}
} /* To initialize the upBaseBox*/
void InitializeUp(){
//create boxes
upBaseBox = Box.createHorizontalBox();
upLeftVerticalBox = Box.createVerticalBox();
upRightVerticalBox = Box.createVerticalBox(); //initialize and add to box
label = new JLabel[labelStr.length];
for(int i=0;i<labelStr.length;i++){
label[i] = new JLabel(labelStr[i]);
} textField = new JTextField[3];
for(int i=0;i<3;i++){
textField[i] = new JTextField(16);
} for(int i=0;i<(labelStr.length-1);i++){
upLeftVerticalBox.add(label[i]);
if(i != 3){
upLeftVerticalBox.add(Box.createVerticalStrut(SPLIT_LEN));
}
}
int posSplit = 2; upRightVerticalBox.add(textField[0]);
upRightVerticalBox.add(Box.createVerticalStrut(SPLIT_LEN + posSplit));
upRightVerticalBox.add(label[labelStr.length-1]);
upRightVerticalBox.add(Box.createVerticalStrut(SPLIT_LEN + posSplit));
upRightVerticalBox.add(textField[1]);
upRightVerticalBox.add(Box.createVerticalStrut(SPLIT_LEN + posSplit));
upRightVerticalBox.add(textField[2]); upBaseBox.add(upLeftVerticalBox);
upBaseBox.add(Box.createHorizontalStrut(SPLIT_LEN));
upBaseBox.add(upRightVerticalBox); } /* To initialize the downBaseBox*/
void InitializeDown(){
//create boxes
downBaseBox = Box.createHorizontalBox(); downLeftJPanel = new JPanel();
GridLayout gridLayout = new GridLayout(3,4,SPLIT_LEN,SPLIT_LEN);
downLeftJPanel.setLayout(gridLayout); downLeftVerticalBox = Box.createVerticalBox();
downChildrenLeftHorizontalBox = Box.createHorizontalBox();
downRightVerticalBox = Box.createVerticalBox(); btn = new JButton[buttonStr.length];
int basicWidth = 15;
int basicHeight = 30;
for(int i=0;i<buttonStr.length;i++){
btn[i] = new JButton(buttonStr[i]);
if(i < 12){
downLeftJPanel.add(btn[i]);
btn[i].setPreferredSize(new Dimension(basicWidth,basicHeight));
}
else if(i<15){
if( 12 == i){
//System.out.println(i);
//btn[i].setSize(100,50);
btn[i].setPreferredSize(new Dimension(basicWidth * 2,basicHeight));
}
else{
btn[i].setPreferredSize(new Dimension(basicWidth,basicHeight));
}
downChildrenLeftHorizontalBox.add(btn[i]);
if(i != 14){
downChildrenLeftHorizontalBox.add(Box.createHorizontalStrut(SPLIT_LEN));
}
}
else{
btn[i].setPreferredSize(new Dimension(basicWidth,basicHeight * 2));
downRightVerticalBox.add(btn[i]);
if(i != (buttonStr.length - 1) ){
downRightVerticalBox.add(Box.createVerticalStrut(SPLIT_LEN));
}
}
}
//btn[12].setPreferredSize(new Dimension(50,50));
//btn[12].setSize(50,50); //add to baseBox
downLeftVerticalBox.add(downLeftJPanel);
downLeftVerticalBox.add(Box.createVerticalStrut(SPLIT_LEN));
downLeftVerticalBox.add(downChildrenLeftHorizontalBox); downBaseBox.add(downLeftVerticalBox);
downBaseBox.add(Box.createHorizontalStrut(SPLIT_LEN));
downBaseBox.add(downRightVerticalBox);
} /* main initialize interface */
void Initialize(){
InitializeUp();
InitializeDown();
//add Children Boxes to mainBaseBox mainBaseBox = Box.createVerticalBox();
mainBaseBox.add(upBaseBox);
mainBaseBox.add(Box.createVerticalStrut(SPLIT_LEN));
mainBaseBox.add(downBaseBox); //add mainBaseBox to JPanel
add(mainBaseBox);
} /*Constructor*/
CalculatorJPanel(){
Initialize();
//validate();
}
}

afddaf的更多相关文章

随机推荐

  1. SDUT 3311 数据结构实验之串三:KMP应用

    数据结构实验之串三:KMP应用 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 有n个小朋友 ...

  2. Flex对象的Clone & Copy浅析

    在flex中有时候会用到ObjectUtil.clone和ObjectUtil.copy方法.下面是官方API的注释. 克隆指定对象,并返回对该克隆的引用.该克隆使用本机序列化技术生成.这意味着在克隆 ...

  3. APP发布Xcode7

    一.准备工作 1>准备3.5寸.4寸.4.7寸.5.5寸的程序截图至少个1张,如果支持iPad,那么iPad截图也要有.这些截图尽量截取页面漂亮的,因为这些截图是要放在AppStore中展示的. ...

  4. 解决在 使用 AjaxFileUploder 插件时,不能获取返回的 json 结果数据

    在MVC  项目 中使用 AjaxFileUploader 这个插件时,在上传图片或文件时,在控制器中返回的是 json数据,可是在 ie,或 googleChrome 浏览器中却出现 返回的json ...

  5. Android WebView与JavaScript交互操作(Demo)

    应用场景: 为了使Android移动项目能够在较短的时间内完成开发,同时降低技术人员开发的成本投入,往往会采用Hybrid APP的开发模式.相关Hybrid APP(混合型应用)参看:http:// ...

  6. Scrapy安装、爬虫入门教程、爬虫实例(豆瓣电影爬虫)

    Scrapy在window上的安装教程见下面的链接:Scrapy安装教程 上述安装教程已实践,可行.(本来打算在ubuntu上安装Scrapy的,但是Ubuntu 磁盘空间太少了,还没扩展磁盘空间,所 ...

  7. 【Hibernate 8】Hibernate的调优方法:抓取策略

    在上一篇博客中,介绍了Hibernate的缓存机制.合理的配置缓存,可以极大程度上优化Hibernate的性能.这篇博客,介绍另外一个调优方式:抓取策略. 一.什么是抓取策略 抓取策略(fetchin ...

  8. Solaris从安装光盘安装软件

    系统安装完成后默认没有安装GCC 挂载系统盘后,所安装的软件就在/cdrom/sol-10-811-x86/Solaris_10/Product文件夹下可以查看下关于GCC的软件包 #ls -l | ...

  9. 著名的二分查找的BUG

    int binarySearch(int a[], int key, int length) { int low = 0; int high = length - 1; while (low < ...

  10. Count and Say [LeetCode 38]

    1- 问题描述 The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211 ...