日期:2014-05-20 浏览次数:20736 次
<html>
<body>
<input id="allSum" value="0"></input>
<input type="button" value="1" width="60px" id="button1" onclick="one()"></input>
<input type="button" value="2" width="60px" id="button2"></input>
<input type="button" value="+" width="60px" id="button3"></input>
<input type="button" value="=" width="60px" id="button4"></input>
</body>
<script>
function one(){
var one=document.getElementById("button1").value;
var value=document.getElementById("allSum").value;
if(value==0){
document.getElementById("allSum").value=one ;
}else{
var result=value+one;
document.getElementById("allSum").value=result;
}
alert(one);
}
</script>
</html>
f()
{
one = new JButton("1");
two = new JButton("2");
t = new JTextField(10);
one.addActionListener(this);
two.addActionListener(this);
t.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
int result = 0;
if(e.getSource() == one)
{
t.setText("1");
}
else if(e.getSource() == two)
{
t.setText("2");
}
else if(e.getSource() == add)
{
result = Integer.parseInt(t.getText()) + result;
}
}
class f extends JFrame implements ActionListener
{
JButton add = new JButton("加");
JButton one, two;
JTextField t;
f()
{
one = new JButton("1");
two = new JButton("2");
t = new JTextField(10);
one.addActionListener(this);
two.addActionListener(this);
t.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
int result = 0;
if(e.getSource() == one)
{
t.setText("1");
}
else if(e.getSource() == two)
{
t.setText("2");
}
else if(e.getSource() == add)
{
result = Integer.parseInt(t.getText()) + result;
}
}
}