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的更多相关文章
随机推荐
- [ CodeVS冲杯之路 ] P1197
不充钱,你怎么AC? 题目:http://codevs.cn/problem/1197/ 密钥的字母可以全转换为小写字母,然后一一映射,a→0,b→1,c→2,依此类推 对于密文只需将每一位减去对应密 ...
- 设计模式——适配器模式(Adapter Pattern)
解决的问题: 适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本接口不匹配而无法在一起工作的两个类能够在一起工作.比如说我的hp笔记本,美国产品,人家美国的电压是110V的,而我们中国 ...
- 学习总结 JDBC
- Socket WSAAsyncSelect模型
::WSAAsyncSelect(sListen, hWnd, WM_SOCKET, FD_ACCEPT|FD_CLOSE); 自定义 WM_SOCKET消息 #include "../co ...
- JQ改变URL
看到搜索按钮可以把网址提供到URL里面 $('#search_submit').click(function(){ var keywords = $('#keywords').val(); locat ...
- 【Python】django安装
官方下载:https://www.djangoproject.com/download/ 报错 [root@test Django-]# python setup.py install Traceba ...
- 【MVC】自定义ASP.NET MVC Html辅助方法
在ASP.NET MVC中,Html辅助方法给我们程序员带来很多方便,其重要性也就不言自明.有时候,我们不想重复地写一些HTML代码,或者MS没有提供我们想要的那个HTML标签的Html辅助方法,那么 ...
- Tomcat 7优化前及优化后的性能对比
Tomcat 7在我们日常开发.测试.生产环境都会使用到,但对于大部分开发人员来说,对其性能还是没有多大了解.本文就对它做一次性能测试,对比优化前后的性能区别. 一.运行环境 CPU: Intel(R ...
- JS常用的设计模式(14)—— 备忘录模式
备忘录模式在js中经常用于数据缓存. 比如一个分页控件, 从服务器获得某一页的数据后可以存入缓存.以后再翻回这一页的时候,可以直接使用缓存里的数据而无需再次请求服务器. 实现比较简单,伪代码: var ...
- vim插件之SnipMate
SnipMate简介 snipMate一款功能强大的代码补齐插件,可自定义代码模板,并具备单词补齐的功能. vim插件snipMate下载地址 SnipMate安装 将snipMate.zip解压到~ ...