用到的是Frame。
package 计算器;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class JiSuan extends Frame implements ActionListener {
static double x;
static double y;
static double add;
static double cha;
static double cheng;
static double chu;
static JiSuan f = new JiSuan ();
static Label lab1 = new Label("x");
static Label lab2 = new Label("y");
static Label lab3 = new Label("结果");
// static Label lab4 =new Label("");
static TextField txt1 = new TextField ();
static TextField txt2 = new TextField ();
static TextField txt3 = new TextField ();
static Button butt1 = new Button ("加");
static Button butt2 = new Button ("减");
static Button butt3 = new Button("乘");
static Button butt4 = new Button("除");
static Button butt5 = new Button("清除");
static Button butt6 = new Button("退出");
public static void main(String[] args) {
f.setTitle("计算器");
Font ft = new Font ("宋体",Font.BOLD,20);
f.setBounds(350,130,1000,80);
f.setFont(ft);
f.setBackground(Color.pink);
lab1.setBounds(30,40,20,30);
lab2.setBounds(140,40,20,30);
lab3.setBounds(250,40,50,30);
lab3.setFont(ft);
// lab4.setBounds(310,40,130,30);
// lab4.setBackground(Color.white);
// lab4.setForeground(Color.black);
txt1.setBounds(55,40,70,30);
txt1.setFont(ft);
txt2.setBounds(165,40,70,30);
txt2.setFont(ft);
txt3.setBounds(310,40,130,30);
txt3.setFont(ft);
butt1.setBounds(460,40,70,30);
butt2.setBounds(540,40,70,30);
butt3.setBounds(620,40,70,30);
butt4.setBounds(700,40,70,30);
butt5.setBounds(780,40,70,30);
butt6.setBounds(860,40,70,30);
f.add(txt1);
f.add(txt2);
f.add(txt3);
f.add(lab1);
f.add(lab2);
f.add(lab3);
// f.add(lab4);
f.add(butt1);
f.add(butt2);
f.add(butt3);
f.add(butt4);
f.add(butt5);
f.add(butt6);
f.setLayout(null);
f.setVisible(true);
butt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==butt1)
x=Double.parseDouble(txt1.getText());
y=Double.parseDouble(txt2.getText());
add = x+y;
txt3.setText(Double.toString(add));
}
});
butt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==butt2)
cha = x-y;
txt3.setText(Double.toString(cha));
}
});
butt3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==butt3)
cheng = x*y;
txt3.setText(Double.toString(cheng));
}
});
butt4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==butt4)
chu = x/y;
txt3.setText(Double.toString(chu));
}
});
butt5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
txt1.setText("");
txt2.setText("");
txt3.setText("");
}
});
butt6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);//关闭角
}
}