afddaf
//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的更多相关文章
随机推荐
- 使用Spring的Property文件存储测试数据 - 初始化
本系列博客有一个前提:只使用Junit编写测试,不使用类似Cucumber这类BDD框架. 用Cucumber的时候,测试数据可以直接写在feature文件里,但是仅仅使用Junit(不要问我为什么只 ...
- [工具] 如何利用Notepad++去除重复行
问题: 需要去除重复数据, 例如: 解决方案: 1. 打开notepad++: 2. 如果没有找到"TextFx" 选项, 需要先安装该插件. 依次打开"插件" ...
- Adobe AIR and Flex - 实现堆栈容器
1.需求描述: 在对云平台的监控中,我们经常需要在一张图上可视化的描述集群下的物理机所虚拟的虚拟机使用情况,以及超卖情况.那么传统的chart图就不满足我们的需求了,那么要实现这样一个定制化的char ...
- ctags 文章
http://blog.csdn.net/wuziqi4/article/details/1709722
- 跟我学 NHibernate (三)
在使用 NHibernate 时,一定要将Mapping 映射文件,也就是 xml 文件的编译方式设置成 嵌入式,这是因为在 NHibernate 启动时,它会主动的到项目的启动目录中寻找 被设置为嵌 ...
- 过河-状压DP
http://www.luogu.org/problem/show?pid=1052 题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上 ...
- ajax和servlet交互
网上有比较多的教程来将如何实现ajax与servlet的交互了,这里和这里的教程可以参考参考,在此处我只简单说明一下,并记录一下我这次遇到的问题. 整个思路是:写个js函数,在里面使用XHR(ajax ...
- 监控系统 - check_mk_agent
系统级监控 cpu (system, user) memory (cache, buffer, use)(MB) load (cpu core) diskspace (used, inode)(GB) ...
- php中实现17种正则表达式
php中实现17种正则表达式 该教程来自:php教程网:http://php.662p.com "^\d+[ DISCUZ_CODE_1 ]quot; //非负整数(正整数 + 0) &qu ...
- C# 集合 — Hashtable 线程安全
基础知识重要吗?真的很重要. 就在笔者与同事聊天中突然同事提出一个问题,让笔都有点乱了手脚(有点夸张),题目是这样的: 问:Hashtable 是线程安全的吗? 答:…… (沉默中,Yes Or No ...