结对编程
一 设计思路:
刚开始创建一个窗体,用ActionEvent 类的对象传送给向它注册的临听器,
请它负责处理。Frame中点击菜单栏的一项,响应一个动作。点击另一处的button,
响应一个动作。两个事件我都是用actionlistener来监听,然后进行判断。
二 代码:
package oo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.FlowLayout;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Test extends JFrame implements ActionListener
{
private JComboBox op = null;
private JTextField nb1 = null;
private JTextField nb2 = null;
private JButton eq = null;
private JLabel result = null;
public Test(){
this.setLayout(new FlowLayout(FlowLayout.LEFT));
nb1 = new JTextField(4);
op = new JComboBox(new String[]{"+","-","×","÷"});
nb2 = new JTextField(4);
eq = new JButton("=");
eq.addActionListener(this);
result = new JLabel();
this.add(nb1);
this.add(op);
this.add(nb2);
this.add(eq);
this.add(result);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setLocationRelativeTo(null);
this.setTitle("简易计算器");
this.setResizable(false);
this.setVisible(true);
}
public static void main(String[] args) {
new Test();
}
@Override
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource() == eq){
double n1 = 0.0;
double n2 = 0.0;
try{
n1 = Double.parseDouble(nb1.getText());
}catch(NumberFormatException nfe){
JOptionPane.showMessageDialog(null, "数字1不是正确的数字格式");
}
try{
n2 = Double.parseDouble(nb2.getText());
}catch(NumberFormatException nfe){
JOptionPane.showMessageDialog(null, "数字2不是正确的数字格式");
}
int p = op.getSelectedIndex();
switch(p){
case 0:
result.setText(String.valueOf(n1+n2));
break;
case 1:
result.setText(String.valueOf(n1-n2));
break;
case 2:
result.setText(String.valueOf(n1*n2));
break;
case 3:
result.setText(String.valueOf(n1/n2));
break;
}
}
}
}
三 psp耗时:
四 测试:
五 和小伙伴的合照:
六 总结:
两个人进行结对编程有助于相互学习,相互弥补各自的不足之处,
我们做的代码不太完善,有的增量没做出来,但是我们依然会继续
努力不断地改进自我。