符合中小企业对网站设计、功能常规化式的企业展示型网站建设
本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...
商城网站建设因基本功能的需求不同费用上面也有很大的差别...
手机微信网站开发、微信官网、微信商城网站...
我也刚学,不太会,刚好老师也是布置的这个任务。你看看吧。 自己改改。
创新互联是一家集成都网站设计、成都网站建设、外贸网站建设、网站页面设计、网站优化SEO优化为一体的专业网站建设公司,已为成都等多地近百家企业提供网站建设服务。追求良好的浏览体验,以探求精品塑造与理念升华,设计最适合用户的网站页面。 合作只是第一步,服务才是根本,我们始终坚持讲诚信,负责任的原则,为您进行细心、贴心、认真的服务,与众多客户在蓬勃发展的市场环境中,互促共生。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.ActionEvent;
public class Jisuanqi extends JFrame implements ActionListener,KeyListener {
private JTextField result;
private JButton btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn0,btn00,
btnAC,btnAdd,btnSub,btnResult,btnD,btnMul,btnDiv,
btnAbout,btnCancel,btnBack;
private boolean add,sub,mul,div,end,s,c=true;
private String str;
private double num1,num2;
public Jisuanqi(String title){
super(title);
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
JPanel p5=new JPanel();
JPanel p6=new JPanel();
//***************菜单栏****************
JMenu optMenu = new JMenu("菜单(M)");
JMenuItem inforMenu = new JMenu("作者信息(I)");
JMenuItem nameItem = new JMenuItem("伍峰杰");
JMenuItem classItem = new JMenuItem("09-1班");
JMenuItem numItem = new JMenuItem("07号");
JMenuItem exitItem = new JMenuItem("退出");
inforMenu.add(nameItem);
inforMenu.add(classItem);
inforMenu.add(numItem);
optMenu.add(inforMenu);
optMenu.add(exitItem);
exitItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ee)
{
System.exit(0);
}
});
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
bar.add(optMenu);
//******快捷键
optMenu.setMnemonic('m');
inforMenu.setMnemonic('i');
//****************输出文本框*************
result =new JTextField("0.0",12);
result.setFont(new Font("Serif",Font.PLAIN,25));
result.setEditable(false);
result.setHorizontalAlignment(JTextField.RIGHT);
result.setForeground(Color.BLACK);
p1.add(result);
add(p1,BorderLayout.NORTH);
//****************0-9数字按钮小数点 ************
btn0=new JButton("0");
btn0.addActionListener(this);
btn0.addKeyListener(this);
btn1=new JButton("1");
btn1.addActionListener(this);
btn1.addKeyListener(this);
btn2=new JButton("2");
btn2.addActionListener(this);
btn2.addKeyListener(this);
btn3=new JButton("3");
btn3.addActionListener(this);
btn3.addKeyListener(this);
btn4=new JButton("4");
btn4.addActionListener(this);
btn4.addKeyListener(this);
btn5=new JButton("5");
btn5.addActionListener(this);
btn5.addKeyListener(this);
btn6=new JButton("6");
btn6.addActionListener(this);
btn6.addKeyListener(this);
btn7=new JButton("7");
btn7.addActionListener(this);
btn7.addKeyListener(this);
btn8=new JButton("8");
btn8.addActionListener(this);
btn8.addKeyListener(this);
btn9=new JButton("9");
btn9.addActionListener(this);
btn9.addKeyListener(this);
btnD=new JButton(".");
btnD.addActionListener(this);
btnD.addKeyListener(this);
btnD.setForeground(Color.RED);
btn00=new JButton("00");
btn00.addActionListener(this);
p2.add(btn7);
p2.add(btn8);
p2.add(btn9);
p2.add(btn4);
p2.add(btn5);
p2.add(btn6);
p2.add(btn1);
p2.add(btn2);
p2.add(btn3);
p2.add(btn0);
p2.add(btnD);
p2.add(btn00);
p2.setLayout(new GridLayout(4,3));
add(p2,BorderLayout.CENTER);
add(p3,BorderLayout.EAST);
add(p4,BorderLayout.SOUTH);
//*******************功能键区
btnAdd=new JButton("+");
btnSub=new JButton("-");
btnMul=new JButton("×");
btnDiv=new JButton("÷");
btnAC =new JButton("Ac");
btnBack= new JButton("←");
btnResult= new JButton("=");
btnAbout= new JButton("关于");
btnAdd.addActionListener(this);
btnSub.addActionListener(this);
btnMul.addActionListener(this);
btnDiv.addActionListener(this);
btnAC.addActionListener(this);
btnBack.addActionListener(this);
btnResult.addActionListener(this);
p3.add(btnAdd);
p3.add(btnSub);
p3.add(btnMul);
p3.add(btnDiv);
p3.add(btnAC);
p3.add(btnBack);
p3.add(btnResult);
p3.add(btnAbout);
btnAdd.setToolTipText("F1");
btnSub.setToolTipText("F2");
btnMul.setToolTipText("F3");
btnDiv.setToolTipText("F4");
btnAC.setToolTipText("DELETE");
btnBack.setToolTipText("BackSpace");
btnAbout.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null,"哈理工-计算机09-1班 7号 伍峰杰","作者",JOptionPane.INFORMATION_MESSAGE);
}
}
);
p3.setLayout(new GridLayout(4,2));
}
//*******数字按钮事件
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_1)
num(1);
else if (e.getKeyCode()==KeyEvent.VK_2)
num(2);
else if (e.getKeyCode()== KeyEvent.VK_3)
num(3);
else if (e.getKeyCode()== KeyEvent.VK_4)
num(4);
else if (e.getKeyCode()== KeyEvent.VK_5)
num(5);
else if (e.getKeyCode()== KeyEvent.VK_6)
num(6);
else if (e.getKeyCode()== KeyEvent.VK_7)
num(7);
else if (e.getKeyCode()== KeyEvent.VK_8)
num(8);
else if (e.getKeyCode()== KeyEvent.VK_9)
num(9);
else if (e.getKeyCode()== KeyEvent.VK_0)
num(0);
//*************小数点
else if (e.getKeyCode()== KeyEvent.VK_PERIOD){
if(c!=false){
str=result.getText();
str+=".";
result.setText(str);
btnD.setEnabled(false);
c=false;}}
//*************退格
else if (e.getKeyCode() ==KeyEvent.VK_BACK_SPACE){
str=result.getText();
if(!str.equals(""))
{str=str.substring(0,str.length()-1);
result.setText(str);
}
else btnBack.setEnabled(false);
}
//******加减乘除
else if (e.getKeyCode()== KeyEvent.VK_F1){
sign(1);
btnD.setEnabled(true);c=true;}
else if (e.getKeyCode()== KeyEvent.VK_F2){
sign(2);
btnD.setEnabled(true);c=true;}
else if (e.getKeyCode()== KeyEvent.VK_F3){
sign(3);
btnD.setEnabled(true);c=true;}
else if (e.getKeyCode()== KeyEvent.VK_F4){
sign(4);
btnD.setEnabled(true);c=true;}
//******等号
else if (e.getKeyCode()== KeyEvent.VK_ENTER){
btnD.setEnabled(true);
c=true;
num2=Double.parseDouble(result.getText());
if(add)
num1=num1+num2;
else if(sub)
num1=num1-num2;
else if(mul)
num1=num1*num2;
else if(div)
num1=num1/num2;
result.setText(String.valueOf(num1));
end=true;
}
//****** 空格全删除
else if (e.getKeyCode()== KeyEvent.VK_DELETE){
btnD.setEnabled(true);c=true;
result.setText("0.0");
num2=0;
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
//*************点击事件监听
public void actionPerformed(ActionEvent e){
if(e.getSource()==btn1)
num(1);
else if (e.getSource()==btn2)
num(2);
else if (e.getSource()== btn3)
num(3);
else if (e.getSource()== btn4)
num(4);
else if (e.getSource()== btn5)
num(5);
else if (e.getSource()== btn6)
num(6);
else if (e.getSource()== btn7)
num(7);
else if (e.getSource()== btn8)
num(8);
else if (e.getSource()== btn9)
num(9);
else if (e.getSource()== btn0)
num(0);
else if (e.getSource()== btn00)
num(100);
else if (e.getSource()== btnD){
str=result.getText();
str+=".";
result.setText(str);
btnD.setEnabled(false);
c=false;}
//******+-*%
else if (e.getSource()== btnAdd){
sign(1);
btnD.setEnabled(true);c=true;}
else if (e.getSource()== btnSub){
sign(2);
btnD.setEnabled(true);c=true;}
else if (e.getSource()== btnMul){
sign(3);
btnD.setEnabled(true);c=true;}
else if (e.getSource()== btnDiv){
sign(4);
btnD.setEnabled(true);c=true;}
else if (e.getSource()== btnAC){
btnD.setEnabled(true);c=true;
result.setText("0.0");
num2=0;
}
//***********=
else if (e.getSource()== btnResult){
btnD.setEnabled(true);
c=true;
num2=Double.parseDouble(result.getText());
if(add)
num1=num1+num2;
else if(sub)
num1=num1-num2;
else if(mul)
num1=num1*num2;
else if(div)
num1=num1/num2;
result.setText(String.valueOf(num1));
end=true;
//btnResult.setEnabled(false);
}
//***********Back键
else if (e.getSource() ==btnBack){
str=result.getText();
if(!str.equals(""))
{str=str.substring(0,str.length()-1);
result.setText(str);
}
else btnBack.setEnabled(false);
}
}
public void num(int i){
String s = null;
if(i!=100)
s=String.valueOf(i);
else s="00";
if(end){
result.setText("0.0");
end=false;
btnBack.setEnabled(true);
}
if((result.getText()).equals("0.0")){
result.setText(s);
btnBack.setEnabled(true);
}
else {
str=result.getText()+s;
result.setText(str);
btnBack.setEnabled(true);
}
}
public void sign(int s){
num1=Double.parseDouble(result.getText());
switch(s){
case 1:add=true;sub=false;mul=false;div=false;break;
case 2:add=false;sub=true;mul=false;div=false;break;
case 3:add=false;sub=false;mul=true;div=false;break;
case 4:
if(num1==0)result.setText("error");
else{add=false;sub=false;mul=false;div=true;break;}
}
end=true;
}
public static void main(String[] args){
Jisuanqi j=new Jisuanqi("计算器");
j.setSize(280,350);
j.setResizable(false);
j.setVisible(true);
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class Jisuanqi extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
Result result = new Result(); // 定义text的面板
Number_Key number_key = new Number_Key(); // 定义按钮面板
// 当点击按钮+、-、*、/时,com = true
boolean com = false;
// 当i=0时说明是我们第一次输入,字符串text不会累加
int i = 0;
// 存放text的内容
String text = "";
// 存放点击按钮+、-、*、/之前的数值
double defbutton = 0;
// +、-、*、/的代号分别为1,2,3,4
int symbol = 0;
// 构造函数
Jisuanqi() {
super("WangJiao"); // 设定标题
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设定关闭窗体时退出程序
JPanel pane = new JPanel(); // 定义主面板
pane.setLayout(new BorderLayout());
setBounds(380, 220, 30, 80); // 前两个参数是在屏幕上显示的坐标,后两个是大小
// 替换图标
ImageIcon icon = new ImageIcon("F:1.GIF");
// Jisuanqi.class.getResource("APPLE.GIF")
// );
setIconImage(icon.getImage());
pane.add(result, BorderLayout.NORTH);
pane.add(number_key, BorderLayout.CENTER);
pane.add(number_key.equal, BorderLayout.SOUTH);
number_key.one.addActionListener(this); // 对1按钮添加监听事件
number_key.two.addActionListener(this); // 对2按钮添加监听事件
number_key.three.addActionListener(this); // 对3按钮添加监听事件
number_key.four.addActionListener(this); // 对4按钮添加监听事件
number_key.five.addActionListener(this); // 对5按钮添加监听事件
number_key.six.addActionListener(this); // 对6按钮添加监听事件
number_key.seven.addActionListener(this); // 对7按钮添加监听事件
number_key.eight.addActionListener(this); // 对8按钮添加监听事件
number_key.nine.addActionListener(this); // 对9按钮添加监听事件
number_key.zero.addActionListener(this); // 对0按钮添加监听事件
number_key.ce.addActionListener(this); // 对置零按钮添加监听事件
number_key.plus.addActionListener(this); // 对+按钮添加监听事件
number_key.equal.addActionListener(this); // 对=按钮添加监听事件
number_key.sub.addActionListener(this); // 对-按钮添加监听事件
number_key.mul.addActionListener(this); // 对*按钮添加监听事件
number_key.div.addActionListener(this); // 对/按钮添加监听事件
number_key.point.addActionListener(this); // 对.按钮添加监听事件
setContentPane(pane);
pack(); // 初始化窗体大小为正好盛放所有按钮
}
// 各个按钮触发的事件
public void actionPerformed(ActionEvent e) {
/*
* 如果是点击数字按钮那么先要判断是否在此之前点击了+、-、*、/、=,如果是那么com=true 如果没有com=
* false;或者是否点击数字键,如果是i = 1,如果没有 i = 0;
*/
if (e.getSource() == number_key.one) {
if (com || i == 0) {
result.text.setText("1");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "1");
}
} else if (e.getSource() == number_key.two) {
if (com || i == 0) {
result.text.setText("2");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "2");
}
} else if (e.getSource() == number_key.three) {
if (com || i == 0) {
result.text.setText("3");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "3");
}
} else if (e.getSource() == number_key.four) {
if (com || i == 0) {
result.text.setText("4");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "4");
}
} else if (e.getSource() == number_key.five) {
if (com || i == 0) {
result.text.setText("5");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "5");
}
} else if (e.getSource() == number_key.six) {
if (com || i == 0) {
result.text.setText("6");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "6");
}
} else if (e.getSource() == number_key.seven) {
if (com || i == 0) {
result.text.setText("7");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "7");
}
} else if (e.getSource() == number_key.eight) {
if (com || i == 0) {
result.text.setText("8");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "8");
}
} else if (e.getSource() == number_key.nine) {
if (com || i == 0) {
result.text.setText("9");
com = false;
i = 1;
} else {
text = result.text.getText();
result.text.setText(text + "9");
}
}
/*
* 对于0这个按钮有一定的说法,在我的程序里不会出现如00000这样的情况,我加了判断条件就是
* 如果text中的数值=0就要判断在这个数值中是否有.存在?如果有那么就在原来数值基础之上添 加0;否则保持原来的数值不变
*/
else if (e.getSource() == number_key.zero) { // result.text.getText()是得到text里内容的意思
if (com || i == 0) {
result.text.setText("0");
com = false;
i = 1;
} else {
text = result.text.getText();
if (Float.parseFloat(text) 0 || Float.parseFloat(text) 0) { // Float.parseFloat(text)就是类型转换了,下面都是一样
result.text.setText(text + "0");
} else {
if (text.trim().indexOf(".") == -1) {
result.text.setText(text);
} else {
result.text.setText(text + "0");
}
}
}
} else if (e.getSource() == number_key.ce) {
result.text.setText("0");
i = 0;
com = true;
// text = "";
defbutton = 0;
}
/*
* 本程序不会让一个数值中出现2个以上的小数点.具体做法是:判断是否已经存在.存在就不添加, 不存在就添加.
*/
else if (e.getSource() == number_key.point) {
if (com || i == 0) {
result.text.setText("0.");
com = false;
i = 1;
} else {
text = result.text.getText();
if (text.trim().indexOf(".") == -1) {
result.text.setText(text + ".");
} else {
result.text.setText(text);
}
}
} // 获得点击+之前的数值
else if (e.getSource() == number_key.plus) {
com = true;
i = 0;
defbutton = Double.parseDouble(result.text.getText());
symbol = 1;
} // 获得点击-之前的数值
else if (e.getSource() == number_key.sub) {
com = true;
i = 0;
defbutton = Double.parseDouble(result.text.getText());
symbol = 2;
} // 获得点击*之前的数值
else if (e.getSource() == number_key.mul) {
com = true;
i = 0;
defbutton = Double.parseDouble(result.text.getText());
System.out.println(defbutton);
symbol = 3;
} // 获得点击/之前的数值
else if (e.getSource() == number_key.div) {
com = true;
i = 0;
defbutton = Double.parseDouble(result.text.getText());
symbol = 4;
} else if (e.getSource() == number_key.equal) {
switch (symbol) {
case 1: { // 计算加法
double ad = defbutton
+ Double.parseDouble(result.text.getText());
result.text.setText(ad + "");
i = 0;
text = "";
break;
}
case 2: { // 计算减法
double ad = defbutton
- Double.parseDouble(result.text.getText());
result.text.setText(String.valueOf(ad));
i = 0;
text = "";
break;
}
case 3: { // 计算乘法
double ad = defbutton
* Double.parseDouble(result.text.getText());
result.text.setText(ad + "");
i = 0;
text = "";
break;
}
case 4: { // 计算除法
double ad = defbutton
/ Double.parseDouble(result.text.getText());
result.text.setText(ad + "");
i = 0;
text = "";
break;
}
}
System.out.println(com);
}
System.out.println(result.text.getText());
}
@SuppressWarnings("deprecation")
public static void main(String[] args) {
Jisuanqi loveyou = new Jisuanqi();
loveyou.show();
}
}
// 计算器数字按钮定义面板
class Number_Key extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
JButton zero = new JButton("0"); // 数字键0
JButton one = new JButton("1"); // 数字键1
JButton two = new JButton("2"); // 数字键2
JButton three = new JButton("3"); // 数字键3
JButton four = new JButton("4"); // 数字键4
JButton five = new JButton("5"); // 数字键5
JButton six = new JButton("6"); // 数字键6
JButton seven = new JButton("7"); // 数字键7
JButton eight = new JButton("8"); // 数字键8
JButton nine = new JButton("9"); // 数字键9
JButton plus = new JButton("+");
JButton sub = new JButton("-");
JButton mul = new JButton("*");
JButton div = new JButton("/");
JButton equal = new JButton("=");
JButton ce = new JButton("清零"); // 置零键
JButton point = new JButton(".");
Number_Key() {
setLayout(new GridLayout(4, 4, 1, 1)); // 定义布局管理器为网格布局
setBackground(Color.blue); // 设置背景颜色
// 添加按钮
add(one);
add(two);
add(three);
add(four);
add(five);
add(six);
add(seven);
add(eight);
add(nine);
add(zero);
add(plus);
add(sub);
add(mul);
add(div);
add(point);
add(equal);
add(ce);
}
}
// 计算器显示结果的窗体
class Result extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
// text先是输入和结果
JTextField text = new JTextField("0");
@SuppressWarnings("deprecation")
Result() { // 讲输入的数字或得到的结果在text的右边显示
text.setHorizontalAlignment(SwingConstants.RIGHT);
text.enable(false); // 文本框不能编辑
setLayout(new BorderLayout()); // 设定布局管理器边框布局
add(text, BorderLayout.CENTER); // text放置在窗体的中间
}
}
直接复制 保存成Jisuanqi .java可以直接运行了
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
/**
* 我的计算器。Cheshi 继承于 JFrame,是计算器的界面
c*/
public class Cheshi extends JFrame {
private Border border = BorderFactory.createEmptyBorder(5, 5, 5, 5);
private JTextField textbox = new JTextField("0");
private CalculatorCore core = new CalculatorCore();
private ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton b = (JButton) e.getSource();
String label = b.getText();
String result = core.process(label);
textbox.setText(result);
}
};
public Cheshi(String title) throws HeadlessException {
super(title); // 调用父类构造方法
setupFrame(); // 调整窗体属性
setupControls(); // 创建控件
}
private void setupControls() {
setupDisplayPanel(); // 创建文本面板
setupButtonsPanel(); // 创建按钮面板
}
// 创建按钮面板并添加按钮
private void setupButtonsPanel() {
JPanel panel = new JPanel();
panel.setBorder(border);
panel.setLayout(new GridLayout(4, 5, 3, 3));
createButtons(panel, new String[]{
"7", "8", "9", "+", "C",
"4", "5", "6", "-", "CE",
"1", "2", "3", "*", "", // 空字符串表示这个位置没有按钮
"0", ".", "=", "/", ""
});
this.add(panel, BorderLayout.CENTER);
}
/**
* 在指定的面板上创建按钮
*
* @param panel 要创建按钮的面板
* @param labels 按钮文字
*/
private void createButtons(JPanel panel, String[] labels) {
for (String label : labels) {
// 如果 label 为空,则表示创建一个空面板。否则创建一个按钮。
if (label.equals("")) {
panel.add(new JPanel());
} else {
JButton b = new JButton(label);
b.addActionListener(listener); // 为按钮添加侦听器
panel.add(b);
}
}
}
// 设置显示面板,用一个文本框来作为计算器的显示部分。
private void setupDisplayPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBorder(border);
setupTextbox();
panel.add(textbox, BorderLayout.CENTER);
this.add(panel, BorderLayout.NORTH);
}
// 调整文本框
private void setupTextbox() {
textbox.setHorizontalAlignment(JTextField.RIGHT); // 文本右对齐
textbox.setEditable(false); // 文本框只读
textbox.setBackground(Color.white); // 文本框背景色为白色
}
// 调整窗体
private void setupFrame() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE); // 当窗体关闭时程序结束
this.setLocation(100, 50); // 设置窗体显示在桌面上的位置
this.setSize(300, 200); // 设置窗体大小
this.setResizable(false); // 窗体大小固定
}
// 程序入口
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Cheshi frame = new Cheshi("我的计算器");
frame.setVisible(true); // 在桌面上显示窗体
}
}
/**
* 计算器核心逻辑。这个逻辑只能处理 1~2 个数的运算。
*/
class CalculatorCore {
private String displayText = "0"; // 要显示的文本
boolean reset = true;
private BigDecimal number1, number2;
private String operator;
private HashMapString, Operator operators = new HashMapString, Operator();
private HashMapString, Processor processors = new HashMapString, Processor();
CalculatorCore() {
setupOperators();
setupProcessors();
}
// 为每种命令添加处理方式
private void setupProcessors() {
processors.put("[0-9]", new Processor() {
public void calculate(String command) {
numberClicked(command);
}
});
processors.put("\\.", new Processor() {
public void calculate(String command) {
dotClicked();
}
});
processors.put("=", new Processor() {
public void calculate(String command) {
equalsClicked();
}
});
processors.put("[+\\-*/]", new Processor() {
public void calculate(String command) {
operatorClicked(command);
}
});
processors.put("C", new Processor() {
public void calculate(String command) {
clearClicked();
}
});
processors.put("CE", new Processor() {
public void calculate(String command) {
clearErrorClicked();
}
});
}
// 为每种 operator 添加处理方式
private void setupOperators() {
operators.put("+", new Operator() {
public BigDecimal process(BigDecimal number1, BigDecimal number2) {
return number1.add(number2);
}
});
operators.put("-", new Operator() {
public BigDecimal process(BigDecimal number1, BigDecimal number2) {
return number1.subtract(number2);
}
});
operators.put("*", new Operator() {
public BigDecimal process(BigDecimal number1, BigDecimal number2) {
return number1.multiply(number2);
}
});
operators.put("/", new Operator() {
public BigDecimal process(BigDecimal number1, BigDecimal number2) {
return number1.divide(number2, 30, RoundingMode.HALF_UP);
}
});
}
// 根据命令处理。这里的命令实际上就是按钮文本。
public String process(String command) {
for (String pattern : processors.keySet()) {
if (command.matches(pattern)) {
processors.get(pattern).calculate(command);
break;
}
}
return displayText;
}
// 当按下 CE 时
private void clearErrorClicked() {
if (operator == null) {
number1 = null;
} else {
number2 = null;
}
displayText = "0";
reset = true;
}
// 当按下 C 时,将计算器置为初始状态。
private void clearClicked() {
number1 = null;
number2 = null;
operator = null;
displayText = "0";
reset = true;
}
// 当按下 = 时
private void equalsClicked() {
calculateResult();
number1 = null;
number2 = null;
operator = null;
reset = true;
}
// 计算结果
private void calculateResult() {
number2 = new BigDecimal(displayText);
Operator oper = operators.get(operator);
if (oper != null) {
BigDecimal result = oper.process(number1, number2);
displayText = result.toString();
}
}
// 当按下 +-*/ 时(这里也可以扩展成其他中间操作符)
private void operatorClicked(String command) {
if (operator != null) {
calculateResult();
}
number1 = new BigDecimal(displayText);
operator = command;
reset = true;
}
// 当按下 . 时
private void dotClicked() {
if (displayText.indexOf(".") == -1) {
displayText += ".";
} else if (reset) {
displayText = "0.";
}
reset = false;
}
// 当按下 0-9 时
private void numberClicked(String command) {
if (reset) {
displayText = command;
} else {
displayText += command;
}
reset = false;
}
// 运算符处理接口
interface Operator {
BigDecimal process(BigDecimal number1, BigDecimal number2);
}
// 按钮处理接口
interface Processor {
void calculate(String command);
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
class Normal{
double i,j;
public Normal(double num1,double num2){
i=num1;
j=num2;
}
public double puls(){
return i+j;
}
public double subtract(){
return i-j;
}
public double multiply(){
return i*j;
}
public double divide(){
return i/j;
}
public double surpuls(){
return i%j;
}
}
class scientific extends Normal{
public scientific(int num1, int num2) {
super(num1, num2);
}
}
public class calc extends JFrame{
public static void main(String[] args) {
viewNormal VN= new viewNormal("normal");
}
}
class viewNormal extends JFrame implements ActionListener{
JPanel jp1 = new JPanel(new GridLayout(4,3,5,5));
JPanel jp2 = new JPanel(new GridLayout(5,1,5,5));
JLabel jl;
JButton[] jb;
JButton jbs,jbo,jba,jbb,jbc,jby;
StringBuffer sb = new StringBuffer();
Normal normal;
int dot=0;
double fnum=0;
double lnum=0;
double result;
String sign=null;
public viewNormal(String title){
setTitle(title);
setLayout(null);
setVisible(true);
setBounds(200,200,305,350);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
jb= new JButton[12];
for(int i=0;i9;i++){
jb[i]=new JButton(""+(i+1));
jp1.add(jb[i]);
jb[i].addActionListener(this);
}
jb[9]=new JButton(".");
jb[10]=new JButton("0");
jb[11]=new JButton("=");
jb[9].addActionListener(this);
jb[10].addActionListener(this);
jb[11].addActionListener(this);
jp1.add(jb[9]);
jp1.add(jb[10]);
jp1.add(jb[11]);
jp1.setBounds(10, 100, 200, 200);
jbs= new JButton("+");jbo= new JButton("-");jba= new JButton("*");
jbb= new JButton("/");jby= new JButton("%");jbc= new JButton("C");
jbs.addActionListener(this);jbo.addActionListener(this);jba.addActionListener(this);
jbb.addActionListener(this);jby.addActionListener(this);jbc.addActionListener(this);
//jp2.add(jby);
jp2.add(jbs);jp2.add(jbo);jp2.add(jba);jp2.add(jbb);jp2.add(jbc);
jp2.setBounds(215, 100, 70, 200);
jl= new JLabel("0",JLabel.RIGHT);
jl.setFont(new Font("Batang",Font.BOLD, 20));
jl.setBorder(new LineBorder(Color.black,2));
jl.setBackground(Color.white);
jl.setBounds(10, 40, 275, 50);
jl.setOpaque(true);
add(jl);
add(jp1);
add(jp2);
}
//+
public void sum(){
lnum=Double.parseDouble(sb.toString());
normal=new Normal(fnum,lnum);
fnum=normal.puls();
result=fnum;
}
//-
private void sub() {
System.out.println(sb.toString());
lnum=Double.parseDouble(sb.toString());
normal=new Normal(fnum,lnum);
fnum=normal.subtract();
result=fnum;
}
//*
private void mul() {
lnum=Double.parseDouble(sb.toString());
normal=new Normal(fnum,lnum);
fnum=normal.multiply();
result=fnum;
}
// /
private void div() {
lnum=Double.parseDouble(sb.toString());
normal=new Normal(fnum,lnum);
fnum=normal.divide();
result=fnum;
}
//%
private void sur() {
lnum=Double.parseDouble(sb.toString());
normal=new Normal(fnum,lnum);
fnum=normal.surpuls();
result=fnum;
}
// =
private void same(){
if(sign.equals("+")){
sum();
}
if(sign.equals("-")){
sub();
}
if(sign.equals("*")){
mul();
}
if(sign.equals("/")){
div();
}
if(sign.equals("%")){
sur();
}
}
//result
public void Result(){
if(result%1!=0)
jl.setText(""+result);
else
{
int i=(int)result;
jl.setText(""+i);
}
}
@Override
public void actionPerformed(ActionEvent e) {
//System.out.println(sb.toString());
// 1~9
for(int i=0;i9;i++){
if(e.getSource()==jb[i]!sb.toString().equals("0")){
sb.append(jb[i].getText());
jl.setText(sb.toString());
}
else if(e.getSource()==jb[i]sb.toString().equals("0")){
int d=sb.length();
sb.delete(0, d);
sb.append(jb[i].getText());
jl.setText(sb.toString());
}
}
// 0
if(e.getSource()==jb[10]!sb.toString().equals("0")){
sb.append(jb[10].getText());
jl.setText(sb.toString());
}
// .
if(e.getSource()==jb[9]dot==0!sb.toString().equals("")){
dot++;
sb.append(jb[9].getText());
jl.setText(sb.toString());
}
// =
if(e.getSource()==jb[11]!sb.toString().equals("")){
same();
Result();
int d=sb.length();
sb.delete(0, d);
dot=0;
}
// +
if(e.getSource()==jbs!sb.toString().equals("")){
if(sign!="+"sign!=null)
same();
else
sum();
sign ="+";
Result();
int d=sb.length();
sb.delete(0, d);
dot=0;
}
//-
if(e.getSource()==jbo!sb.toString().equals("")){
if(fnum==0)
fnum=2*Double.parseDouble(sb.toString());
if(sign!="-"sign!=null)
same();
else
sub();
sign ="-";
Result();
int d=sb.length();
sb.delete(0, d);
dot=0;
}
//*
if(e.getSource()==jba!sb.toString().equals("")){
if(fnum==0)
fnum=1;
if(sign!="*"sign!=null)
same();
else
mul();
sign ="*";
Result();
int d=sb.length();
sb.delete(0, d);
dot=0;
}
// /
if(e.getSource()==jbb!sb.toString().equals("")){
if(fnum==0)
fnum=Math.pow(Double.parseDouble(sb.toString()),2);
if(sign!="/"sign!=null)
same();
else
div();
sign ="/";
Result();
int d=sb.length();
sb.delete(0, d);
dot=0;
}
//%
// if(e.getSource()==jby!sb.toString().equals("")){
// if(fnum==0){
// fnum=Double.parseDouble(sb.toString());
// result=fnum;
// }
// else {
// if(sign!="%"sign!=null)
// same();
// else{
// lnum=Double.parseDouble(sb.toString());
// normal=new Normal(fnum,lnum);
// fnum=normal.surpuls();
// result=fnum;
// }
// }
// sign ="%";
// Result();
// int d=sb.length();
// sb.delete(0, d);
// dot=0;
// }
//clear
if(e.getSource()==jbc){
int d=sb.length();
sb.delete(0, d);
jl.setText("0");
dot=0;
fnum=0;
lnum=0;
sign=null;
}
}
}
class viewScientific extends viewNormal{
public viewScientific(String title){
super(title);
setBounds(200,200,800,500);
}
}
//等号以后输入符号用不了, String转 double 本来就有错误,你可以用我的扩展成科学型的。
import java.awt.*;
import java.applet.*;
public class CalculatorDemo extends Applet
{
TextField answerText;
Button pointButton,equalButton,plusButton;
Button minusButton,clearButton,multiButton,divisionButton;
Button[] b=new Button[10];
String currentOp,preOp;
String foreText,backText;
boolean isFloat=false;
public void init()
{
Panel p1=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
currentOp=new String("");
preOp=new String("");
foreText=new String("");
backText=new String("");
answerText=new TextField(8);
setBackground(Color.lightGray);
setForeground(Color.blue);
for(int i=9;i=0;i--)
{
b[i]=new Button(Integer.toString(i));
p2.add(b[i]);
}
pointButton=new Button(".");
equalButton=new Button("=");
equalButton.setForeground(Color.red);
clearButton=new Button("清除");
clearButton.setForeground(Color.red);
divisionButton=new Button("/");
divisionButton.setForeground(Color.red);
multiButton=new Button("*");
multiButton.setForeground(Color.red);
minusButton=new Button("-");
minusButton.setForeground(Color.red);
plusButton=new Button("+");
plusButton.setForeground(Color.red);
setLayout(new FlowLayout());
p1.setLayout(new FlowLayout());
p2.setLayout(new GridLayout(4,3));
p3.setLayout(new GridLayout(4,1));
p1.add(answerText);
p1.add(clearButton);
p2.add(pointButton);
p2.add(equalButton);
p3.add(plusButton);
p3.add(minusButton);
p3.add(multiButton);
p3.add(divisionButton);
add(p1);
add(p2);
add(p3);
}
public boolean action(Event e,Object o)
{
String s=new String("");
for(int i=0;i10;i++)
{
if(e.target==b[i]||e.target==pointButton)
{
if(e.target !=pointButton)
{
s=(String)o;
doForeText(s);
}
if((e.target==pointButton)(!isFloat))
{
isFloat=true;
s=(String)o;
if(foreText.equals(""))
{
foreText +="0.";
}
else
{
doForeText(s);
}
}
}
}
if(e.target==clearButton)
{
doClear();
}
if((e.target==clearButton)||(e.target==divisionButton)
||(e.target==plusButton)||(e.target==minusButton))
{
if(foreText !="")
{
currentOp=((String)o);
doOperator();
}
else
{
preOp=((String)o);
}
}
if(e.target==equalButton)
{
doOperator();
}
return true;
}
public void doOperator()
{
double dFore,dBack;
Double d;
if(preOp.equals(""))
{
backText=foreText;
foreText="";
answerText.setText(backText);
}
else
{
dFore=(new Double(foreText)).doubleValue();
dBack=(new Double(backText)).doubleValue();
foreText="";
backText=answerText.getText();
if(preOp.equals("+"))
{
d=new Double((dBack+dFore));
answerText.setText(d.toString());
backText=d.toString();
}
if(preOp.equals("-"))
{
d=new Double((dBack-dFore));
answerText.setText(d.toString());
backText=d.toString();
}
if(preOp.equals("*"))
{
d=new Double((dBack*dFore));
answerText.setText(d.toString());
backText=d.toString();
}
if(preOp.equals("/"))
{
if(dFore==0)
{
answerText.setText("除数不能为0");
return;
}
d=new Double((dBack/dFore));
answerText.setText(d.toString());
backText=d.toString();
}
}
preOp=currentOp;
}
public void doForeText(String s)
{
foreText +=s;
answerText.setText(foreText);
}
public void doBackText(String s)
{
backText=foreText;
foreText="";
answerText.setText(foreText);
}
public void doClear()
{
currentOp="";
preOp="";
foreText="";
backText="";
isFloat=false;
answerText.setText("");
}
}