import java.awt.*;
import java.awt.event.*;
//简单计算器加法运算//为方便操作,全写在一个frame类中的,
class frame
extends Frame {
//加了个LABEL显示数据
Label txt = new Label("");
Label slbl = new Label(" ");
Label wlbl = new Label(" ");
panel p = new panel();
public frame() {
setLayout(new BorderLayout());
add(txt, BorderLayout.NORTH);
add(p, BorderLayout.CENTER);
add(slbl, BorderLayout.SOUTH);
add(wlbl, BorderLayout.WEST);
setSize(300, 300);
show();
addWindowListener(new FrameAction());
}
//关闭事件
class FrameAction
extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
//加BUTTON面板
class panel
extends Panel {
Button NumButton[];
String Num[] = {
"1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "0", "="};
public panel() {
NumButton = new Button[Num.length];
setLayout(new GridLayout(4, 3));
for (int i = 0; i < Num.length; i++) {
NumButton[i] = new Button(Num[i]);
add(NumButton[i]);
NumButton[i].addActionListener(new NumButtonAction());
}
}
//BUTTON事件
class NumButtonAction
implements ActionListener {
int FirstNum;
int TwoNum;
int sum;
String jia="+";
String den="=";
public void actionPerformed(ActionEvent e) { if(jia.equals(e.getActionCommand()))
{
FirstNum=Integer.parseInt(txt.getText());
txt.setText("");
System.out.println(FirstNum);
} else if(den.equals(e.getActionCommand()))
{
/////////////////////////////////////////////////////////////////
TwoNum=Integer.parseInt(txt.getText()); System.out.println(TwoNum);
System.out.println(FirstNum+sum);//
txt.setText(String.valueOf(FirstNum+sum));//???????????这里怎么结果
////////////////////////////////////////////////////////////////////////////////
}
else
{
txt.setText(txt.getText()+e.getActionCommand());
}
}
}
}}
////////////////////////////////
public class StuCom {
public StuCom() {
} public static void main(String[] args) {
StuCom stuCom1 = new StuCom();
new frame();
}}
我一直用Swing做,awt根本没学
import java.awt.*;
import java.awt.event.*;
//简单计算器加法运算//为方便操作,全写在一个frame类中的,
class frame
extends Frame {
//加了个LABEL显示数据
Label txt;
public frame() {txt = new Label("");
Label slbl = new Label(" ");
Label wlbl = new Label(" ");
panel p = new panel();
setLayout(new BorderLayout());
add(txt, BorderLayout.NORTH);
add(p, BorderLayout.CENTER);
add(slbl, BorderLayout.SOUTH);
add(wlbl, BorderLayout.WEST);
setSize(300, 300);
show();
addWindowListener(new FrameAction());
}
//关闭事件
class FrameAction
extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
//加BUTTON面板
class panel
extends Panel {
Button NumButton[];
String Num[] = {
"1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "0", "="};
public panel() {
NumButton = new Button[Num.length];
setLayout(new GridLayout(4, 3));
for (int i = 0; i < Num.length; i++) {
NumButton[i] = new Button(Num[i]);
add(NumButton[i]);
NumButton[i].addActionListener(new NumButtonAction());
}
}
//BUTTON事件
class NumButtonAction
implements ActionListener {
int FirstNum;
int TwoNum;
int sum;
String jia="+";
String den="=";
public void actionPerformed(ActionEvent e) { if(jia.equals(e.getActionCommand()))
{
FirstNum=Integer.parseInt(txt.getText());
txt.setText("");
System.out.println(FirstNum);
} else if(den.equals(e.getActionCommand()))
{
/////////////////////////////////////////////////////////////////
TwoNum=Integer.parseInt(txt.getText()); System.out.println(TwoNum);
System.out.println(FirstNum+sum);//
txt.setText(String.valueOf(FirstNum+sum));//???????????这里怎么结果
////////////////////////////////////////////////////////////////////////////////
}
else
{
txt.setText(txt.getText()+e.getActionCommand());
}
}
}
}}
////////////////////////////////
public class StuCom {
public StuCom() {
} public static void main(String[] args) {
StuCom stuCom1 = new StuCom();
new frame();
}}