专业班级:02级计算机(2)班 学号: 2002374207 姓名:_洪伙彪 实验题目: 图形用户界面和事件驱动程序设计 成绩:_____________ *********************************************************************
import java.awt.*; import java.lang.*; import java.text.*; import javax.swing.*; import java.awt.event.*;
public class Counter implements ActionListener { //实现动作监听接口 JFrame frame; JTextField textShow;
JPanel conPanel, conPanel1,conPanel2,keyPanel; JMenuBar mainMenu;
JButton button[]; //数字按钮
JButton Dot,AddAndSub,Add,Sub,Mul,Div,Mod,Sqrt,Dao,Equal,buttonBk,buttonCe,buttonC; JMenu editMenu, viewMenu; //菜单
JMenuItem copyItem, pasteItem, tItem, sItem;
DecimalFormat prec; //用于设置实数的精度 boolean clickable; //用于控制当前能否按键
double vard, result; //用来保存double型数据的中间值(vard)和最后结果(result) int key = -1,prekey = -1; //key用来保存当前进行何种运算,prekey保存前一次进行的运算 String copy; //用于复制,粘贴
int t=0; //判断之前是否按了运算符 public Counter( ) { //构造函数 clickable = true; result = 0;
frame = new JFrame(\"计算器--Bill.Hong\"); prec=new DecimalFormat( ); textShow = new JTextField(15); textShow.setText(\"\");
textShow.setHorizontalAlignment(textShow.RIGHT); textShow.setEditable(false);
textShow.setBackground(Color.white); conPanel = new JPanel();
frame.getContentPane().add(conPanel); conPanel1 = new JPanel(); conPanel2 = new JPanel();
conPanel.setLayout(new BorderLayout()); mainMenu = new JMenuBar();
editMenu = new JMenu(\"编辑(E)\"); viewMenu = new JMenu(\"查看(V)\");
1
copyItem = new JMenuItem(\"复制(C) Ctrl+C\"); copyItem.addActionListener(this);
pasteItem= new JMenuItem(\"粘贴(P) Ctrl+V\"); pasteItem.addActionListener(this); editMenu.add(copyItem); editMenu.add(pasteItem);
tItem = new JMenuItem(\"●标准型\"); tItem.addActionListener(this);
sItem = new JMenuItem( \"科学型\"); sItem.addActionListener(this); viewMenu.add(tItem); viewMenu.add(sItem); mainMenu.add(editMenu); mainMenu.add(viewMenu);
conPanel.add(mainMenu, BorderLayout.NORTH); conPanel.add(textShow, BorderLayout.CENTER); conPanel.add(conPanel1,BorderLayout.SOUTH); conPanel1.setLayout(new BorderLayout()); buttonBk = new JButton(\"Backspace\"); buttonBk.setForeground(Color.red); buttonCe = new JButton(\"CE\"); buttonCe.setForeground(Color.red); buttonC = new JButton(\" C \"); buttonC.setForeground(Color.red); buttonBk.addActionListener(this); buttonCe.addActionListener(this); buttonC.addActionListener(this);
conPanel1.add(conPanel2, BorderLayout.NORTH);
conPanel2.setLayout(new FlowLayout(FlowLayout.RIGHT)); conPanel2.add(buttonBk); conPanel2.add(buttonCe); conPanel2.add(buttonC); keyPanel = new JPanel();
conPanel1.add(keyPanel, BorderLayout.CENTER); button = new JButton[10];
for (int i = 0; i < button.length; i++) {
button[i] = new JButton(Integer.toString(i)); button[i].setForeground(Color.blue); }
Dot = new JButton(\".\");
Dot.setForeground(Color.blue); AddAndSub = new JButton(\"+/-\");
AddAndSub.setForeground(Color.blue); Add = new JButton(\"+\");
2
Add.setForeground(Color.red); Sub = new JButton(\"-\");
Sub.setForeground(Color.red); Mul = new JButton(\"*\");
Mul.setForeground(Color.red); Div = new JButton(\"/\");
Div.setForeground(Color.red); Mod = new JButton(\"%\");
Mod.setForeground(Color.blue); Sqrt = new JButton(\"sqrt\"); Sqrt.setForeground(Color.blue); Dao = new JButton(\"1/x\");
Dao.setForeground(Color.blue); Equal = new JButton(\"=\");
Equal.setForeground(Color.red);
keyPanel.setLayout(new GridLayout(4,5,3,3));//网格布局 keyPanel.add(button[7]);
button[7].addActionListener(this); //将所有行为与监听绑定 keyPanel.add(button[8]);
button[8].addActionListener(this); keyPanel.add(button[9]);
button[9].addActionListener(this); keyPanel.add(Div);
Div.addActionListener(this); keyPanel.add(Sqrt);
Sqrt.addActionListener(this); keyPanel.add(button[4]);
button[4].addActionListener(this); keyPanel.add(button[5]);
button[5].addActionListener(this); keyPanel.add(button[6]);
button[6].addActionListener(this); keyPanel.add(Mul);
Mul.addActionListener(this); keyPanel.add(Mod);
Mod.addActionListener(this); keyPanel.add(button[1]);
button[1].addActionListener(this); keyPanel.add(button[2]);
button[2].addActionListener(this); keyPanel.add(button[3]);
button[3].addActionListener(this); keyPanel.add(Sub);
Sub.addActionListener(this);
3
keyPanel.add(Dao);
Dao.addActionListener(this); keyPanel.add(button[0]);
button[0].addActionListener(this); keyPanel.add(AddAndSub);
AddAndSub.addActionListener(this); keyPanel.add(Dot);
Dot.addActionListener(this); keyPanel.add(Add);
Add.addActionListener(this); keyPanel.add(Equal);
Equal.addActionListener(this);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); frame.setResizable(false); //设置框架大小不可改变 frame.pack(); frame.show(); }
//各个按钮添加事件处理功能
public void actionPerformed(ActionEvent event) { Object temp = event.getSource();
try { //若按下数字按钮,将按下的按钮代表的数据插入的当前文本框字符串之后 for (int i = 0; i <= 9; i++){
if (t==1 ) { textShow.setText(\"\"); t=0; } if (temp == button[i] && clickable == true)
textShow.setText(textShow.getText() + Integer.toString(i)); }
if (temp == Dot && clickable == true) { // 判断当前文本框内字符串中含不含'.' boolean isDot = false;
if (textShow.getText( ).length( ) = = 0) isDot = true;
for (int i = 0; i < textShow.getText().length(); i++) if ('.' == textShow.getText().charAt(i)) { isDot = true; break; }
if (isDot == false)
textShow.setText(textShow.getText() +\".\"); }
if((temp==Add||temp==Sub||temp==Mul||temp==Div)&&clickable==true){ t=1;
if (temp == Add) { //'+'操作 switch (prekey) {
case 0: result += Double.parseDouble(textShow.getText()); break;
case 1: result -= Double.parseDouble(textShow.getText());
4
break;
case 2:result *= Double.parseDouble(textShow.getText()); break;
case 3: if (Double.parseDouble(textShow.getText()) == 0) { textShow.setText(\"除数不能为零\"); clickable = false; }
else result /= Double.parseDouble(textShow.getText()); break;
default: result = Double.parseDouble(textShow.getText()); } textShow.setText(prec.format(result)); prekey = key = 0; }
if (temp == Sub) { //'-'操作 switch (prekey) {
case 0: result += Double.parseDouble(textShow.getText()); break;
case 1: result -= Double.parseDouble(textShow.getText()); break;
case 2: result *= Double.parseDouble(textShow.getText()); break;
case 3:if (Double.parseDouble(textShow.getText()) == 0) { textShow.setText(\"除数不能为零\"); clickable = false; }
else result /= Double.parseDouble(textShow.getText()); break;
default: result = Double.parseDouble(textShow.getText()); } textShow.setText(prec.format(result)); prekey = key = 1; }
if (temp == Mul) { //'*'操作 switch (prekey) {
case 0:result += Double.parseDouble(textShow.getText()); break;
case 1: result -= Double.parseDouble(textShow.getText()); break;
case 2: result *= Double.parseDouble(textShow.getText()); break;
case 3: if (Double.parseDouble(textShow.getText()) == 0) { textShow.setText(\"除数不能为零\"); clickable = false; }
else result /= Double.parseDouble(textShow.getText());
5
break;
default: result = Double.parseDouble(textShow.getText()); } textShow.setText(prec.format(result)); prekey = key = 2; }
if (temp == Div) { //'/'操作 switch (prekey) {
case 0: result += Double.parseDouble(textShow.getText()); break;
case 1: result -= Double.parseDouble(textShow.getText()); break;
case 2: result *= Double.parseDouble(textShow.getText()); break;
case 3: if (Double.parseDouble(textShow.getText()) == 0) { textShow.setText(\"除数不能为零\"); clickable = false; }
else result /= Double.parseDouble(textShow.getText()); break;
default: result = Double.parseDouble(textShow.getText()); } textShow.setText(prec.format(result)); prekey = key = 3; } }
if (temp == Equal && clickable == true) { //'='操作 if (prekey == 4) { //如果连续按'=',则进行连续运算 if (key == 0) { result += vard;
textShow.setText(prec.format(result)); }
if (key == 1) { result -= vard;
textShow.setText(prec.format(result)); }
if (key == 2) { result *= vard;
textShow.setText(prec.format(result)); }
if (key == 3) {
if (Double.parseDouble(textShow.getText()) == 0) { textShow.setText(\"除数不能为零\"); clickable = false; }
else { result /= vard;
textShow.setText(prec.format(result)); } }
6
} else {
vard = Double.parseDouble(textShow.getText()); if (key == 0) {
result += Double.parseDouble(textShow.getText()); textShow.setText(prec.format(result)); }
if (key == 1) {
result -= Double.parseDouble(textShow.getText()); textShow.setText(prec.format(result)); }
if (key == 2) {
result *= Double.parseDouble(textShow.getText()); textShow.setText(prec.format(result)); }
if (key == 3) {
if (Double.parseDouble(textShow.getText()) == 0) { textShow.setText(\"除数不能为零\"); clickable = false; }
else { result /= Double.parseDouble(textShow.getText()); textShow.setText(prec.format(result)); } } }
prekey=4; }
if (temp == Mod && clickable == true) { //'%'操作,对第二个操作数除以100 if (result == 0) textShow.setText(\"0.\"); else { //如果是int数,则按double数处理
double numb = Double.parseDouble(textShow.getText()); numb= numb / 100.0;
textShow.setText(Double.toString(numb)); } }
if (temp == Sqrt && clickable == true) { //开根号运算 String s = textShow.getText(); if (s.charAt(0) == '-') {
textShow.setText(\"负数不能开根号\"); clickable = false; } else
textShow.setText(Double.toString(Math.sqrt(Double.parseDouble(textShow.getText())))); }
7
if (temp==Dao && clickable==true) {//倒数运算
if (textShow.getText().charAt(0)=='0'&&textShow.getText().length()==1) { textShow.setText(\"零不能求倒数\"); clickable = false; } else {
String s = Double.toString(1 / Double.parseDouble(textShow.getText())); textShow.setText(s); } }
if (temp == AddAndSub && clickable == true) { //按下'+/-'按钮时处理 String s = textShow.getText();
//如果当前字符串首字母有'-'号,再按下时,则将首符号去掉 if (s.charAt(0) == '-') { textShow.setText(\"\");
for (int i = 1; i < s.length(); i++) { char a = s.charAt(i);
textShow.setText(textShow.getText() + a); }
} //如果当前字符串第一个字符不是符号,则添加一个符号 else textShow.setText('-' + s); }
//按下'Backspace'键,利用循环将当前字符串中的最后一个字母删除 if (temp == buttonBk && clickable == true) { String s = textShow.getText(); textShow.setText(\"\");
for (int i = 0; i < s.length() - 1; i++) { char a = s.charAt(i);
textShow.setText(textShow.getText() + a); } }
if (temp == buttonCe) { //按下'CE'按钮,将当前文本框内数据清除 textShow.setText(\"\"); clickable = true; }
if (temp == buttonC) {//按下'C'按钮,全部复位 vard = result = 0; textShow.setText(\"\"); clickable = true;
key = -1;prekey = -1; }
if (temp == copyItem) { //按下'复制'菜单栏 copy = textShow.getText(); }
8
if (temp == pasteItem) { //按下'粘贴'菜单栏 textShow.setText(copy); }
if (temp == sItem) {
JOptionPane.showMessageDialog(conPanel,\"很抱歉!!!\\n科学型计算器正在开发\\n --Bill\"); } }
catch (Exception e) { //输入中如果有操作非法,捕获异常 clickable = false; t=1; } } }
class Test{
public static void main(String args[]) { new Counter(); } }
程序界面截图
9
因篇幅问题不能全部显示,请点此查看更多更全内容