📄 jisuanqi.java
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Compute {
//声明组件
private static JFrame ff;
private static JPanel pan1;
JButton b1;
JButton b2;
JButton b3;
JButton b4;
JTextField text1;
JTextField text2;
JLabel l;
JLabel l1;
JLabel l2;
public Compute() //建立界面
{
//面板
pan1=new JPanel();
pan1.setSize(284, 182);
pan1.setBackground(Color.BLUE);
//加法按钮
b1=new JButton("+");
b1.setBounds(new Rectangle(216, 11, 55, 19));
b1.setBackground(Color.pink);
b1.addActionListener(new addAction());
//减法按钮
b2=new JButton("-");
b2.setBounds(new Rectangle(216, 38, 55, 19));
b2.setBackground(Color.pink);
b2.addActionListener(new jianAction());
//乘法按钮
b3=new JButton("*");
b3.setBounds(new Rectangle(216, 65, 55, 19));
b3.setBackground(Color.pink);
b3.addActionListener(new chengAction());
//除法按钮
b4=new JButton("/");
b4.setBounds(new Rectangle(216, 92, 55, 19));
b4.setBackground(Color.pink);
b4.addActionListener(new chuAction());
//文本框
text1=new JTextField();
text1.setBounds(new Rectangle(84, 26, 118, 22));
text2=new JTextField();
text2.setBounds(new Rectangle(84, 62, 118, 22));
//标签
l= new JLabel();
l.setBounds(new Rectangle(9, 98, 193, 22));
l.setText("");
l1=new JLabel();
l1.setBounds(new Rectangle(9, 26, 55, 22));
l1.setText("操作数1");
l2 = new JLabel();
l2.setBounds(new Rectangle(9, 62, 55, 22));
l2.setText("操作数2");
//添加组件
pan1.setLayout(null);
pan1.add(b1);
pan1.add(b2);
pan1.add(b3);
pan1.add(b4);
pan1.add(text1);
pan1.add(text2);
pan1.add(l);
pan1.add(l1);
pan1.add(l2);
}
public static void main(String args[]){
Compute c=new Compute(); //新建组件
ff=new JFrame("简易计算器"); //新建框架
ff.setSize(284, 182);
ff.setLocation(500,400);
ff.setLayout(null);
ff.getContentPane().add(pan1);
ff.setVisible(true);
ff.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{System.exit(0);} }); // 处理关闭事件的通常方法
}
//事件处理方法类
private class addAction implements ActionListener //加法事件
{
public void actionPerformed(ActionEvent e)
{
double temp=0.0;
double temp1=0.0;
double sum=0.0;
String s=new String();
temp =Double.parseDouble(text1.getText());
temp1 =Double.parseDouble(text2.getText());
sum =temp+temp1;
s=Double.toString(sum);
l.setText(s);
}
}
private class jianAction implements ActionListener //减法事件
{
public void actionPerformed(ActionEvent e)
{
double temp=0.0;
double temp1=0.0;
double sum=0.0;
String s=new String();
temp =Double.parseDouble(text1.getText());
temp1 =Double.parseDouble(text2.getText());
sum =temp-temp1;
s=Double.toString(sum);
l.setText(s);
}
}
private class chengAction implements ActionListener //乘法事件
{
public void actionPerformed(ActionEvent e)
{
double temp=0.0;
double temp1=0.0;
double sum=0.0;
String s=new String();
temp =Double.parseDouble(text1.getText());
temp1 =Double.parseDouble(text2.getText());
sum =temp*temp1;
s=Double.toString(sum);
l.setText(s);
}
}
private class chuAction implements ActionListener //除法事件
{
public void actionPerformed(ActionEvent e)
{
//建立 临时存储操作数1,2 和最终结果 的3个变量
double temp=0.0;
double temp1=0.0;
double sum=0.0;
String s=new String(); //临时转换的字符变量
temp =Double.parseDouble(text1.getText()); //获取操作数1 并转换成double型
temp1 =Double.parseDouble(text2.getText()); //获取操作数2 并转换成double型
if(temp1==0) l.setText("除数不能为0"); //判断操作数2是否为0
else
{
sum =temp/temp1;
s=Double.toString(sum);
l.setText(s); //显示结果的标签设置最终的结果
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -