首页 技术 正文
技术 2022年11月17日
0 收藏 450 点赞 4,277 浏览 4288 个字
//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();
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295