计算器 awt swing 界面手写

小傅哥
Java领域优质创作者
博客专家认证
2013-11-24 10:23:22
今天给别人演示awt swing 写了下计算器的界面 和简单的运算。
处理好了一部分,共享下源码。待完善ing


package com.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextField;

public class MainView extends JFrame implements ActionListener{

public static void main(String[] args) {
new MainView();
}

/**
* handle event
*/
public void actionPerformed(ActionEvent e) {
//+ - * /
if(e.getSource() == butPlus){
Operational = "Plus";
}else if(e.getSource() == butMinus){
Operational = "Minus";
}else if(e.getSource() == butMultiply){
Operational = "Multiply";
}else if(e.getSource() == butExcept){
Operational = "Except";
}

//0...9
if(e.getSource() == butOne){
doOperational("1");
}else if(e.getSource() == butTwo){
doOperational("2");
}else if(e.getSource() == outThree){
doOperational("3");
}else if(e.getSource() == butFour){
doOperational("4");
}else if(e.getSource() == butFive){
doOperational("5");
}else if(e.getSource() == outSix){
doOperational("6");
}else if(e.getSource() == butSeven){
doOperational("7");
}else if(e.getSource() == butEight){
doOperational("8");
}else if(e.getSource() == butNine){
doOperational("9");
}else if(e.getSource() == butZero){
doOperational("0");
}
}

//判断计算
public void doOperational(String strNum){
//表示加法(+)计算
if("Plus" == Operational){
showTextField.setText(String.valueOf(Long.valueOf(showTextField.getText()) + Long.valueOf(strNum)));
}else if("Minus" == Operational){
showTextField.setText(String.valueOf(Long.valueOf(showTextField.getText()) - Long.valueOf(strNum)));
}else if("Multiply" == Operational){
showTextField.setText(String.valueOf(Long.valueOf(showTextField.getText()) * Long.valueOf(strNum)));
}else if("Except" == Operational){
showTextField.setText(String.valueOf(Long.valueOf(showTextField.getText()) / Long.valueOf(strNum)));
}else{
showTextField.setText(showTextField.getText() + strNum);
}

//清空运算标识符
Operational = "";
}

public MainView() {

//定义 MenuBar start
topBar = new JMenuBar();
topBar.setLocation(0, 0);
topBar.setSize(260, 25);

editorMenu = new JMenu("编辑(E)");
copyItem = new JMenuItem("复制(C) Ctrl+C");
stickItem = new JMenuItem("粘贴(P) Ctrl+P");
editorMenu.add(copyItem);
editorMenu.add(stickItem);
topBar.add(editorMenu);


viewMenu = new JMenu("查看(V)");
standardItem = new JMenuItem("标准型(T)");
scienceItem = new JMenuItem("科学型(S)");
viewMenu.add(standardItem);
viewMenu.add(scienceItem);
topBar.add(viewMenu);

helpMenu = new JMenu("帮助(H)");
themeItem = new JMenuItem("帮助主题(H)");
aboutItem = new JMenuItem("关于计算机(A)");
helpMenu.add(themeItem);
helpMenu.add(aboutItem);
topBar.add(helpMenu);

this.add(topBar);
//定义 MenuBar end

//定义JTextField start
showTextField = new JTextField();
showTextField.setLocation(5, 28);
showTextField.setSize(245, 25);
showTextField.setHorizontalAlignment(JTextField.RIGHT);
this.add(showTextField);
//定义JTextField end

//定义按钮 start
butSeven = new JButton("7");
butSeven.addActionListener(this);
butSeven.setBackground(Color.white);
butSeven.setForeground(Color.blue);
butSeven.setLocation(5, 55);
butSeven.setSize(45, 45);
this.add(butSeven);

butEight = new JButton("8");
butEight.addActionListener(this);
butEight.setBackground(Color.white);
butEight.setForeground(Color.blue);
butEight.setLocation(55, 55);
butEight.setSize(45, 45);
this.add(butEight);

butNine = new JButton("9");
butNine.addActionListener(this);
butNine.setBackground(Color.white);
butNine.setForeground(Color.blue);
butNine.setLocation(105, 55);
butNine.setSize(45, 45);
this.add(butNine);

butFour = new JButton("4");
butFour.addActionListener(this);
butFour.setBackground(Color.white);
butFour.setForeground(Color.blue);
butFour.setLocation(5, 105);
butFour.setSize(45, 45);
this.add(butFour);

butFive = new JButton("5");
butFive.addActionListener(this);
butFive.setBackground(Color.white);
butFive.setForeground(Color.blue);
butFive.setLocation(55, 105);
butFive.setSize(45, 45);
this.add(butFive);

outSix = new JButton("6");
outSix.addActionListener(this);
outSix.setBackground(Color.white);
outSix.setForeground(Color.blue);
outSix.setLocation(105, 105);
outSix.setSize(45, 45);
this.add(outSix);

butOne = new JButton("1");
butOne.addActionListener(this);
butOne.setBackground(Color.white);
butOne.setForeground(Color.blue);
butOne.setLocation(5, 155);
butOne.setSize(45, 45);
this.add(butOne);

butTwo = new JButton("2");
butTwo.addActionListener(this);
butTwo.setBackground(Color.white);
butTwo.setForeground(Color.blue);
butTwo.setLocation(55, 155);
butTwo.setSize(45, 45);
this.add(butTwo);

outThree = new JButton("3");
outThree.addActionListener(this);
outThree.setBackground(Color.white);
outThree.setForeground(Color.blue);
outThree.setLocation(105, 155);
outThree.setSize(45, 45);
this.add(outThree);

butZero = new JButton("0");
butZero.addActionListener(this);
butZero.setBackground(Color.white);
butZero.setForeground(Color.blue);
butZero.setLocation(5, 205);
butZero.setSize(45, 45);
this.add(butZero);

butNegative = new JButton("/-");
butNegative.addActionListener(this);
butNegative.setBackground(Color.white);
butNegative.setForeground(Color.blue);
butNegative.setLocation(55, 205);
butNegative.setSize(45, 45);
this.add(butNegative);

butPoint = new JButton(".");
butPoint.addActionListener(this);
butPoint.setBackground(Color.white);
butPoint.setForeground(Color.blue);
butPoint.setLocation(105, 205);
butPoint.setSize(45, 45);
this.add(butPoint);

butPlus = new JButton("+");
butPlus.addActionListener(this);
butPlus.setBackground(Color.white);
butPlus.setForeground(Color.red);
butPlus.setLocation(155, 55);
butPlus.setSize(45, 45);
this.add(butPlus);

butMinus = new JButton("-");
butMinus.addActionListener(this);
butMinus.setBackground(Color.white);
butMinus.setForeground(Color.red);
butMinus.setLocation(155, 105);
butMinus.setSize(45, 45);
this.add(butMinus);

butMultiply = new JButton("*");
butMultiply.addActionListener(this);
butMultiply.setBackground(Color.white);
butMultiply.setForeground(Color.red);
butMultiply.setLocation(155, 155);
butMultiply.setSize(45, 45);
this.add(butMultiply);

butExcept = new JButton("/");
butExcept.addActionListener(this);
butExcept.setBackground(Color.white);
butExcept.setForeground(Color.red);
butExcept.setLocation(155, 205);
butExcept.setSize(45, 45);
this.add(butExcept);

//清屏
butClear = new JButton("C");
butClear.addActionListener(this);
butClear.setBackground(Color.black);
butClear.setForeground(Color.white);
butClear.setLocation(205, 155);
butClear.setSize(45, 45);
this.add(butClear);

//倒数
butReciprocal = new JButton("1/");
butReciprocal.addActionListener(this);
butReciprocal.setBackground(Color.white);
butReciprocal.setForeground(Color.blue);
butReciprocal.setLocation(205, 55);
butReciprocal.setSize(45, 45);
this.add(butReciprocal);

//百分比
butPercentage = new JButton("%");
butPercentage.addActionListener(this);
butPercentage.setBackground(Color.white);
butPercentage.setForeground(Color.blue);
butPercentage.setLocation(205, 105);
butPercentage.setSize(45, 45);
this.add(butPercentage);

//求结果
butResult = new JButton("=");
butResult.addActionListener(this);
butResult.setBackground(Color.white);
butResult.setForeground(Color.red);
butResult.setLocation(205, 205);
butResult.setSize(45, 45);
this.add(butResult);
//定义按钮 end

this.setLayout(null);
this.setSize(260, 285);
this.setLocation(200, 50);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("恭喜发财|istack计算器");
}

//运算标记符
private String Operational = "";

//定义Menu
private JMenuBar topBar;
private JMenu editorMenu,viewMenu,helpMenu;
private JMenuItem copyItem,stickItem,standardItem,scienceItem,themeItem,aboutItem;


//定义显示框
private JTextField showTextField;

//定义按钮
private JButton butOne,butTwo,outThree,
butFour,butFive,outSix,
butSeven,butEight,butNine,
butZero,butPoint,butNegative,
butExcept,butMultiply,
butMinus,butPlus,butClear,
butPercentage,butReciprocal,butResult;


}
//更多学习资源来源www.itstack.org
...全文
211 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
MounteBank 2013-11-25
  • 打赏
  • 举报
回复
给别人演示,还有人用这个,有点奇葩

23,405

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧