ava面向对象编程
实验报告
题 目:数组与图形化界面 专 业:计算机科学与技术 班 级: 1120541 学 号: 41 姓 名: 汪智
太原工业学院计算机工程系
2014年 05月23日
一、实验目的与要求
1、掌握Java编程中的数组的应用
2、掌握Java GUI编程的应用以及API的查阅
3、掌握Java GUI 窗体容器、布局管理器、基本组件、事件的注册 实验内容
Java GUI实现如下图简单计算器
J三、实施步骤
①CalculatorDemo类: public class CalculatorDemo extends JFrame {
/*结果输入框*/
private JTextField result; /*操作键之前缓存*/
private StringBuilder beforeCache = new StringBuilder(); /*操作键之后缓存*/
private StringBuilder afterCache = new StringBuilder(); /*清除键*/
private JButton clean; /*数字键*/
private JButton[] numberButs = new JButton[10]; /*操作键*/
private Map
private boolean operFlag = false; public void setOperFlag(boolean operFlag) { this.operFlag = operFlag;
}
public CalculatorDemo(){ initComp(); initMainWindow(); addListenerHandler();
}
②CleanButtonActionListenerImpl 类:
public class CleanButtonActionListenerImpl implements ActionListener { private CalculatorDemo cal;
public CleanButtonActionListenerImpl(CalculatorDemo c){ this.cal = c;
}
@Override
public void actionPerformed(ActionEvent arg0) { JTextField result = cal.getResult(); /*获取输入文本*/
String resultText= result.getText(); if(!resultText.equals(\)){ //文本內容,重置為空 result.setText(\);
//緩存清空
StringBuilder beforeCache = cal.getBeforeCache(); StringBuilder afterCache = cal.getAfterCache(); beforeCache.delete(0, beforeCache.length()); afterCache.delete(0, afterCache.length());
cal.setOperFlag(false);
} }
}
③MainWindowListenerImpl 类:
public class MainWindowListenerImpl implements WindowListener { @Override
public void windowActivated(WindowEvent arg0) { }
@Override
public void windowClosed(WindowEvent arg0) {
}
/***
* 当主窗体关闭时,推出整个程序 * */ @Override
public void windowClosing(WindowEvent arg0) { System.out.println(\退出计算器程序\); System.exit(0);
}
@Override
public void windowDeactivated(WindowEvent arg0) { }
@Override
public void windowDeiconified(WindowEvent arg0) {
}
@Override
public void windowIconified(WindowEvent arg0) {
}
@Override
public void windowOpened(WindowEvent arg0) {
System.out.print(\窗体打开\); }
}
④MouseListenerImpl 类:
public class MouseListenerImpl implements MouseListener{ @Override
public void mouseClicked(MouseEvent arg0) {
}
@Override
public void mouseEntered(MouseEvent event) { JButton bt = (JButton)event.getSource(); bt.setForeground(Color.RED);
}
@Override
public void mouseExited(MouseEvent event) { JButton bt = (JButton)event.getSource();
bt.setForeground(Color.BLACK);
}
@Override
public void mousePressed(MouseEvent arg0) { }
@Override
public void mouseReleased(MouseEvent arg0) { }
}
⑤NumKeyActionListenerImpl 类:
public class NumKeyActionListenerImpl implements ActionListener { private CalculatorDemo cal;
public NumKeyActionListenerImpl(CalculatorDemo c){ this.cal = c; }
@Override
public void actionPerformed(ActionEvent event) { /*获取当前操作的数字键对象*/
JButton but =(JButton)event.getSource(); /*
* 1.在没有按下操作键之前, * 所有的数字键的值被缓存到beforeCache *显示到输入框中
*/
/*获取数字键对应的输入值*/ String numVaue=but.getText(); if(!cal.isOperFlag()){ cal.getBeforeCache().append(numVaue);
cal.getResult().setText(cal.getBeforeCache().toString()); }else{
cal.getAfterCache().append(numVaue);
cal.getResult().setText(cal.getAfterCache().toString());
}
}
}
⑥OperKeyActionListenerImpl 类:
public class OperKeyActionListenerImpl implements ActionListener {
private CalculatorDemo cal;
public OperKeyActionListenerImpl(CalculatorDemo c){ this.cal = c;
} @Override
public void actionPerformed(ActionEvent event) { /*获取操作键*/ JButton but = (JButton)event.getSource(); //置空结果显示
cal.getResult().setText(\); cal.setOperFlag(true); }
}
⑦Calculator 类:
public class Calculator {
private Stack
private Map
//初始化优先级约定(可根据计算的复杂程度扩展) public CalculatorIn()
{
priorityMap.put(\,0); priorityMap.put(\,0); priorityMap.put(\, 1); priorityMap.put(\, 1); }
public int getPriority(String op)//得到一个操作符的优先级 {
return priorityMap.get(op); }
public boolean highPriority(String op) {
int opPri=getPriority(op); if(!operatorStack.empty()) {
for(String s:operatorStack) {
int priority=getPriority(s); if(opPri return true; } public String expToIpn() { int index=0; int end=0; String Ipn=\; for(int i=0;i String temps=String.valueOf(expression.charAt(i)); if(temps.matches(\)) { end++; } else { String tempOperand=expression.substring(index,end);//得到操作数 Ipn+=tempOperand+\; String tempOperator=expression.substring(end,++end);//得到操作符 if(tempOperator.equals(\))//假如到表达式的最后将操作符 全部弹出 { while(!operatorStack.empty()) { Ipn+=operatorStack.pop()+\; } } else { if(highPriority(tempOperator))/ { operatorStack.push(tempOperator); } else { while(!operatorStack.empty())// { Ipn+=operatorStack.pop()+\; } operatorStack.push(tempOperator); } //System.out.println(tempOperand+\ index=end; } } } return Ipn; } public double calculateIpn(String[] Ipn)//计算逆波兰式 { for(int i=0;i // System.out.println(Ipn[i]); if(Ipn[i].matches(\))//正则表达式判断是数字 { operandStack.push(Double.parseDouble(Ipn[i])); } else { popOperand(Ipn[i]); } } return result; } public void popOperand(String operator) { } public Stack getOperandStack() { return operandStack; } public void setOperandStack(Stack operandStack) { this.operandStack = operandStack; } public Stack getOperatorStack() { return operatorStack; } public void setOperatorStack(Stack operatorStack) { this.operatorStack = operatorStack; } public String getexpression_r() { return expression; } public void setexpression_r(String expression) { this.expression = expression; } public double getResult() { return result; } public void setResult(double result) { this.result = result; } } 四、实验结果 五、出现的问题及解决的方法 解决办法: 项目buildpath?configbuildpath?libraries 改为javaSE-1.6(jre6)
Java实验报告三数组与图形用户界面
![](/skin/haowen/images/icon_star.png)
![](/skin/haowen/images/icon_star.png)
![](/skin/haowen/images/icon_star.png)
![](/skin/haowen/images/icon_star.png)