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的更多相关文章
随机推荐
- java springMVC生成二维码
Zxing是Google提供的工具,提供了二维码的生成与解析的方法,现在使用Java利用Zxing生成二维码 1),二维码的生成 将Zxing-core.jar 包加入到classpath下. 我的下 ...
- python中get、post数据
方法一:urllib2 参考:http://www.cnblogs.com/chenzehe/archive/2010/08/30/1812995.html post: #!/usr/bin/pyth ...
- 火箭18号秀光膀为父母割草(FW)
火箭18号秀光膀为父母割草 一夜致富不改本色 来源:新浪 2015年06月30日 分享到: 更多 收藏 分享 被浏览5次 <ignore_js_op id="r_article_i ...
- 让内层Div将外层Div撑开
在CSS排版中,如果一个层中的层使用了float浮动的话,那么就有可能会出现外层没有被内层撑开的情况,如以下代码所示: <div style="width:300px; "& ...
- .NET平台下,关于数据持久层框架
在.NET平台下,关于数据持久层框架非常多,本文主要对如下几种做简要的介绍并推荐一些学习的资源: 1.NHibernate 2.NBear 3.Castle ActiveRecord 4.iBATIS ...
- WP8_读写XML
/// <summary> /// WP手机,XML读写类 /// </summary> public class WPXmlRW { /// <summary> ...
- 4种kill某个用户所有进程的方法
在linux系统管理中,我们有时候需要kill掉某个用户的所有进程,初学者一般先查询出用户的所有pid,然后一条条kill掉,或者写好一个脚本,实际上方法都有现成的,这边有4种方法,我们以kill用户 ...
- 一、Struts2的概述
一.Struts2概述 是什么? Struts2是一个M(模型---域--范围模型)V(View视图)C(控制器)框架(模型2).框架都是一个半成品.提高开发效率. Struts1是一个MVC框架,非 ...
- 在Qt中使用AnyCAD三维建模控件
AnyCAD C++ SDK专为Qt框架增加了AnyPlatformQt.lib模块,在Qt中使用AnyCAD三维建模控件变得十分简单. 下载 Qt高速下载:http://pan.baidu.com/ ...
- 免费在线CAD文件转换
AnyCAD Exchange Cloud 提供在线的CAD文件转换服务,包括二维图纸和三维模型的数据转换. 支持的格式有: DWG/DGN/DXF 到 PDF, SVG, DAE等的转换 STEP/ ...