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();
}
}
1 import java.awt.*; 这里的.*是干什么用的。
2 public class js extends JFrame implements ActionListener 这句话里面的extends JFrame implements ActionListener。
3 Button b2=new Button("+"); 这里的new是不是就是构造函数
2,extends JFrame implements ActionListener 是指继承 JFrame 类并且实现 ActionListener接口
3,new 是关键字,Button(Args) 才是构造函数
extends JFrame implements ActionListener 是继承窗体类和实现事件监听接口
new 就是分配一个内存对象,这里是有参数的构造一个按钮对象