java applet实现加减乘除计数器
1. 整数计数器。编写一个小应用程序,要求该小应用程序能够接受整数的输入,并能进行整数的加减乘除运算。
import java.awt.FlowLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JComboBox;
public class calcu extends JApplet implements ActionListener
{
JTextField []a={new JTextField(2),new JTextField(2),new JTextField(2)};
String []s={" + "," - "," * "," / "};
JComboBox cb=new JComboBox(s);
public void init()
{
Container c=getContentPane();
c.setLayout(new FlowLayout());
a[0].setEditable(true);
a[1].setEditable(true);
JButton k=new JButton("=");
c.add(a[0]);
int n=4;
for(int i=0;i
{c.add(cb);}
c.add(a[1]);
c.add(k);
c.add(a[2]);
k.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
switch(cb.getSelectedIndex())
{
case 0:
a[2].setText( Integer.parseInt(a[0].getText())+ Integer.parseInt(a[1].getText())+"" );
break;
case 1:
a[2].setText( Integer.parseInt(a[0].getText())- Integer.parseInt(a[1].getText())+"" );
break;
case 2:
a[2].setText( Integer.parseInt(a[0].getText())* Integer.parseInt(a[1].getText())+"" );
break;
case 3:
a[2].setText( Integer.parseInt(a[0].getText())/ Integer.parseInt(a[1].getText())+"" );
break;
}
}
}
计数器
程序结果: