Java界面-AWT-简易计算器
Java界面一AWT一简易计算器
描述:
学习完AWT的基本用法之后,编写一个简易的计算器用以巩固知识,增加熟练度。
这段代码主要涉及的是AWT的使用(面板、按钮、文本框、文本、布局管理)、事件监听等等;
实现了加减乘除的运算。
源代码:
package 实践;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class 简易计算器 {
public static void main(String[] args) {
new Calculator();
}
}
//计算器类
class Calculator extends Frame{
public Calculator(){
setTitle("calculation");
Panel panel = new Panel();
Panel panel1 = new Panel();
Panel panel2 = new Panel();
Panel panel3 = new Panel();
//加法
TextField textField = new TextField();
textField.setColumns(10);
TextField textField1 = new TextField();
textField1.setColumns(10);
TextField textField2 = new TextField();
textField2.setColumns(20);
//减法
TextField textFieldj = new TextField();
textFieldj.setColumns(10);
TextField textFieldj1 = new TextField();
textFieldj1.setColumns(10);
TextField textFieldj2 = new TextField();
textFieldj2.setColumns(20);
//乘法
TextField textFieldc = new TextField();
textFieldc.setColumns(10);
TextField textFieldc1 = new TextField();
textFieldc1.setColumns(10);
TextField textFieldc2 = new TextField();
textFieldc2.setColumns(20);
//除法
TextField textFieldchu = new TextField();
textFieldchu.setColumns(10);
TextField textFieldchu1 = new TextField();
textFieldchu1.setColumns(10);
TextField textFieldchu2 = new TextField();
textFieldchu2.setColumns(20);
//1个按钮
Button button = new Button("=");
button.addActionListener(new MyCulListenner(textField,textField1,textField2));
Button button1 = new Button("=");
button1.addActionListener(new MyCulListennerj(textFieldj,textFieldj1,textFieldj2));
Button button2 = new Button("=");
button2.addActionListener(new MyCulListennerc(textFieldc,textFieldc1,textFieldc2));
Button button3 = new Button("=");
button3.addActionListener(new MyCulListennerchu(textFieldchu,textFieldchu1,textFieldchu2));
//4个标签
Label label = new Label();
label.setText("+");
Label label1 = new Label();
label1.setText("-");
Label label2 = new Label();
label2.setText("*");
Label label3 = new Label();
label3.setText("/");
//布局
setLayout(new GridLayout(4,1));
panel.add(textField);
panel.add(label);
panel.add(textField1);
panel.add(button);
panel.add(textField2);
panel1.add(textFieldj);
panel1.add(label1);
panel1.add(textFieldj1);
panel1.add(button1);
panel1.add(textFieldj2);
panel2.add(textFieldc);
panel2.add(label2);
panel2.add(textFieldc1);
panel2.add(button2);
panel2.add(textFieldc2);
panel3.add(textFieldchu);
panel3.add(label3);
panel3.add(textFieldchu1);
panel3.add(button3);
panel3.add(textFieldchu2);
add(panel);
add(panel1);
add(panel2);
add(panel3);
setLocation(200,200);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//监听器类
class MyCulListenner implements ActionListener{ //加法
TextField t1;
TextField t2;
TextField t3;
public MyCulListenner(TextField t1,TextField t2,TextField t3) {
this.t1=t1;
this.t2=t2;
this.t3=t3;
}
@Override
public void actionPerformed(ActionEvent e) {
int num1=Integer.parseInt(t1.getText());
int num2 = Integer.parseInt(t2.getText());
int num3=num1+num2;
t3.setText(String.valueOf(num3));
t1.setText("");
t2.setText("");
}
}
class MyCulListennerj implements ActionListener{ //减法
TextField t1;
TextField t2;
TextField t3;
public MyCulListennerj(TextField t1,TextField t2,TextField t3) {
this.t1=t1;
this.t2=t2;
this.t3=t3;
}
@Override
public void actionPerformed(ActionEvent e) {
int num1=Integer.parseInt(t1.getText());
int num2 = Integer.parseInt(t2.getText());
int num3=num1-num2;
t3.setText(String.valueOf(num3));
t1.setText("");
t2.setText("");
}
}
class MyCulListennerc implements ActionListener{ //乘法
TextField t1;
TextField t2;
TextField t3;
public MyCulListennerc(TextField t1,TextField t2,TextField t3) {
this.t1=t1;
this.t2=t2;
this.t3=t3;
}
@Override
public void actionPerformed(ActionEvent e) {
int num1=Integer.parseInt(t1.getText());
int num2 = Integer.parseInt(t2.getText());
int num3=num1*num2;
t3.setText(String.valueOf(num3));
t1.setText("");
t2.setText("");
}
}
class MyCulListennerchu implements ActionListener{ //除法
TextField t1;
TextField t2;
TextField t3;
public MyCulListennerchu(TextField t1,TextField t2,TextField t3) {
this.t1=t1;
this.t2=t2;
this.t3=t3;
}
@Override
public void actionPerformed(ActionEvent e) {
double num1=Double.parseDouble(t1.getText());
double num2=Double.parseDouble(t2.getText());
double num3=num1/num2;
t3.setText(String.valueOf(num3));
t1.setText("");
t2.setText("");
}
}
以上是 Java界面-AWT-简易计算器 的全部内容, 来源链接: utcz.com/z/392550.html