日期:2014-05-20 浏览次数:20669 次
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class js extends JFrame implements ActionListener
{
Button b1=new Button("*");
Button b2=new Button("+");
Button b3=new Button("/");
Button b4=new Button("-");
Label l1=new Label("操作数1");
Label l2=new Label("操作数2");
Label l3=new Label("结果");
TextField t1=TextField(6);
TextField t2=new TextField(6);
TextField t3=new TextField(6);
JPanel jp=new JPanel();
JPanel jp1=new JPanel();
public js()
{
this.setTitle("calculator");
this.setLayout(new FlowLayout());
jp.setLayout(new GridLayout(3,2));
jp1.setLayout(new GridLayout(4,1));
this.setBounds(0,0,200,200);
this.add(jp);
this.add(jp1);
jp.add(l1);
jp.add(t1);
jp.add(l2);
jp.add(t2);
jp.add(l3);
jp.add(t3);
this.setVisible(true);
jp1.add(b1);
jp1.add(b2);
jp1.add(b3);
jp1.add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
double a=Double.parseDouble(t1.getText());
double b=Double.parseDouble(t2.getText());
double c;
if(e.getActionCommand()=="*")
{
c=a*b;
t3.setText(Double.toString(c));
}
if(e.getActionCommand()=="+")
{
c=a+b;
t3.setText(Double.toString(c));
}
if(e.getActionCommand()=="-")
{
c=a-b;
t3.setText(Double.toString(c));
}
if(e.getActionCommand()=="/")
{
c=a/b;
t3.setText(Double.toString(c));
}
}
public static void main(String asgs[])
{
js co=new js();
}
}